This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Fix assert() warning in gcc < 4.8 [BZ# 21242]

On 03/21/2017 05:21 PM, Joseph Myers wrote:
> On Tue, 21 Mar 2017, Florian Weimer wrote:
>> On 03/19/2017 10:44 PM, Justin Brewer wrote:
>>> -# if !defined __GNUC__ || defined __STRICT_ANSI__
>>> +# if !(defined __GNUC__ && __GNUC_PREREQ(4,8)) || defined __STRICT_ANSI__
>> I'm not happy how this disables the useful = warning for clang altogether.
>> Do we have a better option?  Is there a way for check for the -pedantic flag?
> By design, warning options such as -pedantic are not meant to affect code 
> generation, so should not have any way to check for them within the code.
> (Preferably __extension__ should be used, but that runs into the issues 
> with disabling it for the user's expression as discussed earlier for this 
> code <>.)

I think we can use __extension__ if we also expand the expression
without __extension__ in an unevaluated context.  The tricky part is to
find one that is independent of GNU extensions.
Perhaps this would work?

#  define assert(expr)                                           \
  ((void) sizeof ((expr) == 0), __extension__ ({                 \
      if (expr)                                                  \
        ; /* empty */                                            \
      else                                                       \
        __assert_fail (#expr, __FILE__, __LINE__, __FUNCTION__); \

sizeof suppresses the evaluation of the first occurrence of expr.  The
comparison is needed because sizeof cannot be applied to function
pointers and bitfields.  C11 says that expr is compared to zero, so the
(expr) == 0 expression is well-formed.

What do you think?  Should we make this change?


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]