Skip to content

Conversation

saturn691
Copy link
Contributor

@saturn691 saturn691 commented Aug 14, 2025

Fixes issue with <csetjmp> which requires _Returns_twice but in C++ mode

Fixes issue with <csetjmp> which requires _Returns_twice but in
C++ mode
@llvmbot llvmbot added the libc label Aug 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 14, 2025

@llvm/pr-subscribers-libc

Author: William Huynh (saturn691)

Changes

Fixes issue with &lt;csetjmp&gt; which requires _Returns_twice but in C++ mode


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

1 Files Affected:

  • (modified) libc/include/__llvm-libc-common.h (+3)
diff --git a/libc/include/__llvm-libc-common.h b/libc/include/__llvm-libc-common.h
index c6fd33a55532c..b376b05df1678 100644
--- a/libc/include/__llvm-libc-common.h
+++ b/libc/include/__llvm-libc-common.h
@@ -47,6 +47,9 @@
 #define __NOEXCEPT throw()
 #endif
 
+#undef _Returns_twice
+#define _Returns_twice [[gnu::returns_twice]]
+
 // This macro serves as a generic cast implementation for use in both C and C++,
 // similar to `__BIONIC_CAST` in Android.
 #undef __LLVM_LIBC_CAST

Comment on lines 50 to 52
#undef _Returns_twice
#define _Returns_twice [[gnu::returns_twice]]

Copy link
Contributor

Choose a reason for hiding this comment

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

given that this is on both sides of the #ifdef __cplusplus branch is there a way we can unify it instead of having two copies? Also is there a reason you used the gnu attribute instead of the generic one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. returns_twice needs to be namespaced (https://en.cppreference.com/w/cpp/language/attributes.html)
❯ clang test.c                                                               
test.c:3:3: warning: unknown attribute 'returns_twice' ignored [-Wunknown-attributes]
    3 | [[returns_twice]] int f() {
      |   ^~~~~~~~~~~~~
1 warning generated.
  1. [[attribute]] syntax is only supported in C++11. It seems to compile for older versions and this has even been back-ported to C code in clang. I'll push an update to unify this.

Copy link
Member

@petrhosek petrhosek Aug 17, 2025

Choose a reason for hiding this comment

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

The [[attribute]] syntax was only introduced in C20 so we shouldn't be using it in C code since we want our headers are compatible with older C standards.

Copy link
Contributor Author

@saturn691 saturn691 Aug 18, 2025

Choose a reason for hiding this comment

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

Good point, I've settled with preferring to generate the new attribute syntax if the compiler supports it (C++11) onwards.

@saturn691 saturn691 requested a review from petrhosek August 18, 2025 10:53
@saturn691 saturn691 merged commit 0c622d7 into llvm:main Aug 19, 2025
19 checks passed
@saturn691 saturn691 deleted the libc-returns-twice branch August 19, 2025 08:28
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