From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1624) id 80A113858C5F; Fri, 10 Feb 2023 16:25:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 80A113858C5F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676046300; bh=oAd5lrpoDNeZ1sJfk1I5JP6NqqzWFd3/oKc7xawJq9Q=; h=From:To:Subject:Date:From; b=yBZSRXH4z6DB2Vl3YlwzuYE0+RW5MLTqHcN5MfhqF8/20bS2mRdLKJKYGZrK4GHap SgLSs8+G8v6yHKywtMLok39Vp2/WH5WB9hxXwBLFjwaePSq8eTFpRuTiJhA0D4n5mp ItO1RzO29Lcmj9PeVhLMrl8yTbbtPAeyvEVMySwY= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Paul Pluzhnikov To: glibc-cvs@sourceware.org Subject: [glibc] Use __builtin_FILE instead of __FILE__ in assert in C++. X-Act-Checkin: glibc X-Git-Author: Paul Pluzhnikov X-Git-Refname: refs/heads/master X-Git-Oldrev: 63550530d98db6e9c30dc96a3ea08411b873b23e X-Git-Newrev: 8757659b3cee57c1defee7772c3ee687d310b076 Message-Id: <20230210162500.80A113858C5F@sourceware.org> Date: Fri, 10 Feb 2023 16:25:00 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=8757659b3cee57c1defee7772c3ee687d310b076 commit 8757659b3cee57c1defee7772c3ee687d310b076 Author: Paul Pluzhnikov 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 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 (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) \