public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Rich Felker <dalias@libc.org>
To: Florian Weimer <fweimer@redhat.com>
Cc: Paul Pluzhnikov via Libc-alpha <libc-alpha@sourceware.org>,
	Paul Pluzhnikov <ppluzhnikov@google.com>,
	Jonathan Wakely <jwakely@redhat.com>
Subject: Re: [patch] Use __builtin_FILE and __builtin_LINE in assert implementation in C++
Date: Tue, 24 Jan 2023 06:23:07 -0500	[thread overview]
Message-ID: <20230124112307.GE3298@brightrain.aerifal.cx> (raw)
In-Reply-To: <878rhsgoxn.fsf@oldenburg.str.redhat.com>

On Tue, Jan 24, 2023 at 12:19:32PM +0100, Florian Weimer wrote:
> * Rich Felker:
> 
> > On Mon, Jan 23, 2023 at 03:26:04PM +0100, Florian Weimer via Libc-alpha wrote:
> >> * Paul Pluzhnikov via Libc-alpha:
> >> 
> >> > 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)                                                            \
> >> 
> >> I think __builtin_FILE and __builtin_LINE are farily recent GCC/Clang
> >> additions, so they need compiler version checks or a __has_builtin gate.
> >> 
> >> Ideally, we would use <source_location> here, but I don't think we can
> >> include that from <assert.h>.
> >> 
> >> Thanks,
> >> Florian
> >
> > Why not instead:
> >
> > +static char *__file_for_assert = __FILE__;
> > ...
> > -      : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
> > +      : __assert_fail (#expr, __file_for_assert, __LINE__, __ASSERT_FUNCTION))
> >
> > ??
> 
> __FILE__ expansion needs to be delayed, otherwise it refers to assert.h.
> 
> I think the builtins also have the advantage that they avoid ODR
> violations because the definition is the same no matter what the file is
> called.  This issue is not restricted to modules.  But maybe the right
> answer is to blame the programmer and just add a diagnostic?
> 
> Jonathan, I think the libstdc++ headers have the same issue.  Thoughts?

Arguably use of assert() in inline functions subject to this rule is a
programming error, since there's no guarantee it expands to the same
sequence of tokens as required. My leaning is that no action is
required here.

(Also, C++ programmers should stop putting so much code in inline
functions in headers. That's why C++ programs are so gigantic and take
ridiculous amounts of time to build. If it's big enough to merit
assertions it's almost surely too big to belong in an inline.)

Rich

  reply	other threads:[~2023-01-24 11:23 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-17 19:28 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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230124112307.GE3298@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=fweimer@redhat.com \
    --cc=jwakely@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=ppluzhnikov@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).