Skip to content

Conversation

@michaelrj-google
Copy link
Contributor

Previously the assert macro took one argument named "e", but this led to
possible errors if the caller had commas in their input. C23 changed the
definition of assert to use __VA_ARGS__ to ensure comma cases are
handled properly. This patch doesn't introduce the enforcement function
mentioned in the standard update, though that may be done in a followup.

Fixes #136184

Previously the assert macro took one argument named "e", but this led to
possible errors if the caller had commas in their input. C23 changed the
definition of assert to use `__VA_ARGS__` to ensure comma cases are
handled properly. This patch doesn't introduce the enforcement function
mentioned in the standard update, though that may be done in a followup.

Fixes llvm#136184
@llvmbot llvmbot added the libc label Apr 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 25, 2025

@llvm/pr-subscribers-libc

Author: Michael Jones (michaelrj-google)

Changes

Previously the assert macro took one argument named "e", but this led to
possible errors if the caller had commas in their input. C23 changed the
definition of assert to use __VA_ARGS__ to ensure comma cases are
handled properly. This patch doesn't introduce the enforcement function
mentioned in the standard update, though that may be done in a followup.

Fixes #136184


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

1 Files Affected:

  • (modified) libc/include/assert.h.def (+3-3)
diff --git a/libc/include/assert.h.def b/libc/include/assert.h.def
index d5ae14a1cd810..2c27712251b17 100644
--- a/libc/include/assert.h.def
+++ b/libc/include/assert.h.def
@@ -19,14 +19,14 @@
 
 #undef assert
 #ifdef NDEBUG
-#define assert(e) (void)0
+#define assert(...) ((void)0)
 #else
 #ifdef __cplusplus
 extern "C"
 #endif
 _Noreturn void __assert_fail(const char *, const char *, unsigned, const char *) __NOEXCEPT;
-#define assert(e)  \
-  ((e) ? (void)0 : __assert_fail(#e, __FILE__, __LINE__, __PRETTY_FUNCTION__))
+#define assert(...)  \
+  ((__VA_ARGS__) ? ((void)0) : __assert_fail(#__VA_ARGS__, __FILE__, __LINE__, __PRETTY_FUNCTION__))
 #endif
 
 %%public_api()

@lntue lntue merged commit 9124e96 into llvm:main Apr 28, 2025
18 checks passed
@michaelrj-google michaelrj-google deleted the libcAssertC23 branch May 5, 2025 20:35
@michaelrj-google
Copy link
Contributor Author

Thanks for merging this for me!

IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Previously the assert macro took one argument named "e", but this led to
possible errors if the caller had commas in their input. C23 changed the
definition of assert to use `__VA_ARGS__` to ensure comma cases are
handled properly. This patch doesn't introduce the enforcement function
mentioned in the standard update, though that may be done in a followup.

Fixes llvm#136184
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
Previously the assert macro took one argument named "e", but this led to
possible errors if the caller had commas in their input. C23 changed the
definition of assert to use `__VA_ARGS__` to ensure comma cases are
handled properly. This patch doesn't introduce the enforcement function
mentioned in the standard update, though that may be done in a followup.

Fixes llvm#136184
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.

[libc][C23] N2829: Make assert() macro user friendly for C and C++

4 participants