[libc-commits] [libc] [libc] Add `returns_twice` attribute to `setjmp(3)` (PR #124370)

Alan Zhao via libc-commits libc-commits at lists.llvm.org
Fri Jan 24 15:53:25 PST 2025


https://github.com/alanzhao1 created https://github.com/llvm/llvm-project/pull/124370

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).

>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] [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 b5a23c5765f4d8..58006a1ffc24fc 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 2c4f7fb6dfcc70..5fbb9eb2a47e5b 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



More information about the libc-commits mailing list