This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Fix assert() warning in gcc < 4.8 [BZ# 21242]
- From: Justin Brewer <jzb0012 at auburn dot edu>
- To: Florian Weimer <fweimer at redhat dot com>, <libc-alpha at sourceware dot org>
- Date: Tue, 21 Mar 2017 10:35:13 -0400
- Subject: Re: [PATCH] Fix assert() warning in gcc < 4.8 [BZ# 21242]
- Authentication-results: sourceware.org; auth=none
- Authentication-results: sourceware.org; dkim=none (message not signed) header.d=none;sourceware.org; dmarc=none action=none header.from=auburn.edu;
- References: <6619e7e2-248e-08bc-6dc5-7b4d11bea3df@auburn.edu> <925e8a81-86f9-78a6-1578-660c04e39e3c@redhat.com>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:99
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