[libc-commits] [libc] [libc] Add `returns_twice` attribute to `setjmp(3)` (PR #124370)
Alan Zhao via libc-commits
libc-commits at lists.llvm.org
Wed Feb 5 13:33:30 PST 2025
https://github.com/alanzhao1 updated https://github.com/llvm/llvm-project/pull/124370
>From 5cc318716fd50e1acd685f5731d75acfb02292b7 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Fri, 24 Jan 2025 15:49:10 -0800
Subject: [PATCH 1/3] [libc] Add `returns_twice` attribute to `setjmp(3)`
This is to work around a clang bug where clang will incorrectly add a
tail call optimization to `setjmp(3)` if `-fno-builtins` is set
(#122840).
---
libc/include/__llvm-libc-common.h | 6 ++++++
libc/include/setjmp.yaml | 2 ++
2 files changed, 8 insertions(+)
diff --git a/libc/include/__llvm-libc-common.h b/libc/include/__llvm-libc-common.h
index b5a23c5765f4d85..58006a1ffc24fc0 100644
--- a/libc/include/__llvm-libc-common.h
+++ b/libc/include/__llvm-libc-common.h
@@ -45,6 +45,9 @@
#define __NOEXCEPT throw()
#endif
+#undef _Returns_twice
+#define _Returns_twice [[gnu::returns_twice]]
+
#else // not __cplusplus
#undef __BEGIN_C_DECLS
@@ -80,6 +83,9 @@
#define __NOEXCEPT
#endif
+#undef _Returns_twice
+#define _Returns_twice __attribute__((returns_twice))
+
#endif // __cplusplus
#endif // LLVM_LIBC_COMMON_H
diff --git a/libc/include/setjmp.yaml b/libc/include/setjmp.yaml
index 2c4f7fb6dfcc70f..5fbb9eb2a47e5b8 100644
--- a/libc/include/setjmp.yaml
+++ b/libc/include/setjmp.yaml
@@ -17,5 +17,7 @@ functions:
standards:
- stdc
return_type: int
+ attributes:
+ - _Returns_twice
arguments:
- type: jmp_buf
>From 3b19f8b8770aaa51c592a4a961fc3320a8619f48 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Mon, 27 Jan 2025 11:22:27 -0800
Subject: [PATCH 2/3] add attribute to setjmp_impl.h
---
libc/src/setjmp/setjmp_impl.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libc/src/setjmp/setjmp_impl.h b/libc/src/setjmp/setjmp_impl.h
index d035409e5819545..c89d6bc07c900b5 100644
--- a/libc/src/setjmp/setjmp_impl.h
+++ b/libc/src/setjmp/setjmp_impl.h
@@ -29,7 +29,8 @@ namespace LIBC_NAMESPACE_DECL {
#ifdef LIBC_COMPILER_IS_GCC
[[gnu::nothrow]]
#endif
-int setjmp(jmp_buf buf);
+[[gnu::returns_twice]] int
+setjmp(jmp_buf buf);
} // namespace LIBC_NAMESPACE_DECL
>From 5047b8f4ee90cdbfeca7a571994dc48dab2f2346 Mon Sep 17 00:00:00 2001
From: Alan Zhao <azhao101 at gmail.com>
Date: Wed, 5 Feb 2025 13:33:21 -0800
Subject: [PATCH 3/3] remove [[]] syntax
Co-authored-by: Nick Desaulniers <nick.desaulniers at gmail.com>
---
libc/include/__llvm-libc-common.h | 3 ---
1 file changed, 3 deletions(-)
diff --git a/libc/include/__llvm-libc-common.h b/libc/include/__llvm-libc-common.h
index 58006a1ffc24fc0..d3f8a4e63268a5e 100644
--- a/libc/include/__llvm-libc-common.h
+++ b/libc/include/__llvm-libc-common.h
@@ -45,9 +45,6 @@
#define __NOEXCEPT throw()
#endif
-#undef _Returns_twice
-#define _Returns_twice [[gnu::returns_twice]]
-
#else // not __cplusplus
#undef __BEGIN_C_DECLS
More information about the libc-commits
mailing list