Skip to content

[libc] Use __attribute__((__noreturn__)) for _Noreturn in C < 11 #121252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2025

Conversation

frobtech
Copy link
Contributor

When in modes like C99, the _Noreturn keyword is not available in
C. But GNU-compatible compilers have a noreturn attribute with
the same effect on function declarations.

When in modes like C99, the _Noreturn keyword is not available in
C.  But GNU-compatible compilers have a `noreturn` attribute with
the same effect on function declarations.
@frobtech frobtech requested a review from Caslyn December 28, 2024 07:10
@frobtech frobtech marked this pull request as ready for review December 28, 2024 07:11
@llvmbot llvmbot added the libc label Dec 28, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 28, 2024

@llvm/pr-subscribers-libc

Author: Roland McGrath (frobtech)

Changes

When in modes like C99, the _Noreturn keyword is not available in
C. But GNU-compatible compilers have a noreturn attribute with
the same effect on function declarations.


Full diff: https://github.com/llvm/llvm-project/pull/121252.diff

1 Files Affected:

  • (modified) libc/include/__llvm-libc-common.h (+10)
diff --git a/libc/include/__llvm-libc-common.h b/libc/include/__llvm-libc-common.h
index d54ee7b9f91f32..d9d70aff771c08 100644
--- a/libc/include/__llvm-libc-common.h
+++ b/libc/include/__llvm-libc-common.h
@@ -52,6 +52,16 @@
 #undef __restrict
 #define __restrict restrict // C99 and above support the restrict keyword.
 
+#undef _Noreturn
+#if __STDC_VERSION__ >= 201112L
+// In C11 and later, _Noreturn is a keyword.
+#elif defined(__GNUC__)
+// GNU-compatible compilers have an equivalent attribute.
+#define _Noreturn __attribute__((__noreturn__))
+#else
+#define _Noreturn
+#endif
+
 #undef __NOEXCEPT
 #ifdef __GNUC__
 #define __NOEXCEPT __attribute__((__nothrow__))

@frobtech frobtech requested a review from fabio-d December 28, 2024 07:16
@frobtech frobtech merged commit 4010e0c into llvm:main Jan 3, 2025
15 checks passed
@frobtech frobtech deleted the p/libc-noreturn branch January 3, 2025 00:57
@@ -52,6 +52,16 @@
#undef __restrict
#define __restrict restrict // C99 and above support the restrict keyword.

#undef _Noreturn
#if __STDC_VERSION__ >= 201112L
// In C11 and later, _Noreturn is a keyword.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woah, so I was familiar with the keyword, but for some reason I had a hunch there was also a function attribute using the C23 [[]] syntax...turns out there is! lol

So there's BOTH they keyword / "function specifier" _Noreturn, AND the function attribute [[noreturn]] (sounds like [[_Noreturn]] is supported but "obsolescent." See footnote 175 of n3220) (and the GNU function attribute syntax). Looks like C23 also added __has_c_attribute preprocessor directive, but I'm guessing that's not yet widely supported by many versions of gcc+clang.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants