public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [patch] Use __builtin_FILE and __builtin_LINE in assert implementation in C++
@ 2023-01-17 19:28 Paul Pluzhnikov
  2023-01-23 14:26 ` Florian Weimer
  0 siblings, 1 reply; 22+ messages in thread
From: Paul Pluzhnikov @ 2023-01-17 19:28 UTC (permalink / raw)
  To: GLIBC Devel

Greetings,

Attached patch changes assert in C++ mode from using __FILE__ and
__LINE__ to using __builtin_FILE() and __builtin_LINE().
These are supported by GCC since ~2012.

Motivation: C++20 modules.

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.

For example:

// a.h
#include <assert.h>
inline void fn () { assert (0); }

// a.cc
#include "a.h"

// b.cc
#include "foo/../a.h"

Preprocessing a.cc and b.cc produces:
   (static_cast <bool> (0) ? void (0) : __assert_fail ("0", "a.h", 2,
__extension__ __PRETTY_FUNCTION__));
and
   (static_cast <bool> (0) ? void (0) : __assert_fail ("0",
"foo/../a.h", 2,  __extension__ __PRETTY_FUNCTION__));
respectively.

Without C++ modules, this results in unpredictable output, and
technically there is an ODR violation.

When C++ modules are involved, the mismatched sequence of tokens may
be diagnosed and result in a build failure.

P.S. I am only aware of the __FILE__ expansion causing problems, but I
am changing __LINE__ as well for symmetry.
-- 
Paul Pluzhnikov

diff --git a/assert/assert.h b/assert/assert.h
index 72209bc5e7..4e0303db8d 100644
--- a/assert/assert.h
+++ b/assert/assert.h
@@ -89,7 +89,8 @@ __END_DECLS
 #  define assert(expr)                                                 \
      (static_cast <bool> (expr)
         \
       ? void (0)                                                       \
-      : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
+      : __assert_fail (#expr, __builtin_FILE (), __builtin_LINE (),     \
+                       __ASSERT_FUNCTION))
 # elif !defined __GNUC__ || defined __STRICT_ANSI__
 #  define assert(expr)                                                 \
     ((expr)                                                            \

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

end of thread, other threads:[~2023-02-08 21:44 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-17 19:28 [patch] Use __builtin_FILE and __builtin_LINE in assert implementation in C++ Paul Pluzhnikov
2023-01-23 14:26 ` Florian Weimer
2023-01-24 11:10   ` Rich Felker
2023-01-24 11:19     ` Rich Felker
2023-01-24 11:19     ` Florian Weimer
2023-01-24 11:23       ` Rich Felker
2023-01-24 11:53         ` Jonathan Wakely
2023-01-24 12:08           ` Florian Weimer
2023-01-24 12:17             ` Jonathan Wakely
2023-01-24 15:07               ` Szabolcs Nagy
2023-01-24 15:51                 ` Jonathan Wakely
2023-01-26 19:18                   ` Paul Pluzhnikov
2023-01-27 15:43                     ` Jonathan Wakely
2023-02-05 18:39                       ` Paul Pluzhnikov
2023-02-05 20:08                         ` Florian Weimer
2023-02-05 21:51                           ` Paul Pluzhnikov
2023-02-05 22:34                             ` Florian Weimer
2023-02-05 22:55                               ` Paul Pluzhnikov
2023-02-06  6:01                                 ` Florian Weimer
2023-02-06 16:25                                   ` Paul Pluzhnikov
2023-02-08 21:43                                     ` Paul Pluzhnikov
2023-01-25 20:50           ` 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).