[patch] Use __builtin_FILE and __builtin_LINE in assert implementation in C++

Paul Pluzhnikov ppluzhnikov@google.com
Mon Feb 6 16:25:24 GMT 2023


On Sun, Feb 5, 2023 at 10:01 PM Florian Weimer <fweimer@redhat.com> wrote:

> Missing spaces before '(', sorry.

Done.

Rest looks okay if we can assume that
> __builtin_FILE and __builtin_LINE are implemented at the same time.

GCC has added these so long ago, we don't care (I think).
LLVM commit https://reviews.llvm.org/D37035 implemented both at the same time.

-- 
Paul Pluzhnikov
-------------- next part --------------
diff --git a/assert/assert.h b/assert/assert.h
index 72209bc5e7..63197b819c 100644
--- a/assert/assert.h
+++ b/assert/assert.h
@@ -86,10 +86,21 @@ __END_DECLS
    parentheses around EXPR.  Otherwise, those added parentheses would
    suppress warnings we'd expect to be detected by gcc's -Wparentheses.  */
 # if defined __cplusplus
+#  if defined __has_builtin
+#   if __has_builtin (__builtin_FILE)
+#    define __ASSERT_FILE __builtin_FILE ()
+#    define __ASSERT_LINE __builtin_LINE ()
+#   endif
+#  endif
+#  if !defined(__ASSERT_FILE)
+#   define __ASSERT_FILE __FILE__
+#   define __ASSERT_LINE __LINE__
+#  endif
 #  define assert(expr)							\
      (static_cast <bool> (expr)						\
       ? void (0)							\
-      : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
+      : __assert_fail (#expr, __ASSERT_FILE, __ASSERT_LINE,             \
+                       __ASSERT_FUNCTION))
 # elif !defined __GNUC__ || defined __STRICT_ANSI__
 #  define assert(expr)							\
     ((expr)								\


More information about the Libc-alpha mailing list