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

Justin Brewer jzb0012@auburn.edu
Tue Mar 21 14:35:00 GMT 2017


On 03/21/2017 05:55 AM, 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?

Not that I've been able to find. In terms of compiler-defined macros, 
there is no way to detect that pedantic mode is enabled. The following 
command gives no output, for any gcc or clang version I have on hand:

$ diff <($CC -E -dM - </dev/null) <($CC -E -dM -pedantic - </dev/null)

clang does have a specific warning flag for this 
(-Wgnu-statement-expression), so we can inject _Pragma calls into the 
macro to silence it. The following achieves the desired effect in clang, 
but throws pramga warnings in gcc. If you actually want to go down this 
path, I can fix the gcc warnings and update my patch.

--- a/assert.h    2017-03-12 22:30:17.299019029 -0400
+++ b/assert.h    2017-03-21 10:06:48.253673891 -0400
@@ -85,19 +85,22 @@
  /* When possible, define assert so that it does not add extra
     parentheses around EXPR.  Otherwise, those added parentheses would
     suppress warnings we'd expect to be detected by gcc's 
-Wparentheses.  */
-# if !defined __GNUC__ || defined __STRICT_ANSI__
+# if !(defined __clang__ || (defined __GNUC__ && __GNUC_PREREQ(4,8))) 
|| defined __STRICT_ANSI__
  #  define assert(expr)                            \
      ((expr)                                \
       ? __ASSERT_VOID_CAST (0)                        \
       : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION))
  # else
  #  define assert(expr)                            \
+  _Pragma("clang diagnostic push");                    \
+  _Pragma("clang diagnostic ignored \"-Wgnu-statement-expression\"");    \
      ({                                    \
        if (expr)                                \
          ; /* empty */                            \
        else                                \
          __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION);    \
-    })
+    });                                    \
+  _Pragma("clang diagnostic pop")
  # endif

  # ifdef    __USE_GNU



More information about the Libc-alpha mailing list