public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc] Use __builtin_FILE instead of __FILE__ in assert in C++.
@ 2023-02-10 17:13 Paul Pluzhnikov
  0 siblings, 0 replies; 3+ messages in thread
From: Paul Pluzhnikov @ 2023-02-10 17:13 UTC (permalink / raw)
  To: glibc-cvs

The branch 'master' was updated to point to:

 e42ec82219... Use __builtin_FILE instead of __FILE__ in assert in C++.

It previously pointed to:

 8757659b3c... Use __builtin_FILE instead of __FILE__ in assert in C++.

Diff:

!!! WARNING: THE FOLLOWING COMMITS ARE NO LONGER ACCESSIBLE (LOST):
-------------------------------------------------------------------

  8757659... Use __builtin_FILE instead of __FILE__ in assert in C++.


Summary of changes (added commits):
-----------------------------------

  e42ec82... Use __builtin_FILE instead of __FILE__ in assert in C++.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [glibc] Use __builtin_FILE instead of __FILE__ in assert in C++.
@ 2023-02-10 17:13 Paul Pluzhnikov
  0 siblings, 0 replies; 3+ messages in thread
From: Paul Pluzhnikov @ 2023-02-10 17:13 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=e42ec822190056895e55e5140ce2304e67e34445

commit e42ec822190056895e55e5140ce2304e67e34445
Author: Paul Pluzhnikov <ppluzhnikov@google.com>
Date:   Fri Feb 10 16:14:30 2023 +0000

    Use __builtin_FILE instead of __FILE__ in assert in C++.
    
    Likewise use __builtin_LINE instead of __LINE__.
    
    When building C++, inline functions are required to have the exact same
    sequence of tokens in every translation unit. But __FILE__ token, when
    used in a header file, does not necessarily expand to the exact same
    string literal, and that may cause compilation failure when C++ modules
    are being used. (It would also cause unpredictable output on assertion
    failure at runtime, but this rarely matters in practice.)
    
    For example, given the following sources:
    
      // a.h
      #include <assert.h>
      inline void fn () { assert (0); }
    
      // a.cc
      #include "a.h"
    
      // b.cc
      #include "foo/../a.h"
    
    preprocessing a.cc will yield a call to __assert_fail("0", "a.h", ...)
    but b.cc will yield __assert_fail("0", "foo/../a.h", ...)

Diff:
---
 assert/assert.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/assert/assert.h b/assert/assert.h
index 72209bc5e7..62670e4bbb 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)								\

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [glibc] Use __builtin_FILE instead of __FILE__ in assert in C++.
@ 2023-02-10 16:25 Paul Pluzhnikov
  0 siblings, 0 replies; 3+ messages in thread
From: Paul Pluzhnikov @ 2023-02-10 16:25 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=8757659b3cee57c1defee7772c3ee687d310b076

commit 8757659b3cee57c1defee7772c3ee687d310b076
Author: Paul Pluzhnikov <ppluzhnikov@google.com>
Date:   Fri Feb 10 16:14:30 2023 +0000

    Use __builtin_FILE instead of __FILE__ in assert in C++.
    
    Likewise use __builtin_LINE instead of __LINE__.
    
    When building C++, inline functions are required to have the exact same
    sequence of tokens in every translation unit. But __FILE__ token, when
    used in a header file, does not necessarily expand to the exact same
    string literal, and that may cause compilation failure when C++ modules
    are being used. (It would also cause unpredictable output on assertion
    failure at runtime, but this rarely matters in practice.)
    
    For example, given the following sources:
    
      // a.h
      #include <assert.h>
      inline void fn () { assert (0); }
    
      // a.cc
      #include "a.h"
    
      // b.cc
      #include "foo/../a.h"
    
    preprocessing a.cc will yield a call to __assert_fail("0", "a.h", ...)
    but b.cc will yield __assert_fail("0", "foo/../a.h", ...)

Diff:
---
 assert/assert.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-02-10 17:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-10 17:13 [glibc] Use __builtin_FILE instead of __FILE__ in assert in C++ Paul Pluzhnikov
  -- strict thread matches above, loose matches on Subject: below --
2023-02-10 17:13 Paul Pluzhnikov
2023-02-10 16:25 Paul Pluzhnikov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).