This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH] Fix assert() warning in gcc < 4.8 [BZ# 21242]
- From: Justin Brewer <jzb0012 at auburn dot edu>
- To: <libc-alpha at sourceware dot org>
- Date: Sun, 19 Mar 2017 17:44:31 -0400
- Subject: [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;
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:99
When compiling with gcc-4.7 and earlier, with -pedantic but without -ansi, a
warning is produced due to the braced group in the assert macro. This
warning
also occurs in the latest clang. In gcc >= 4.8, no warning is produced.
Therefore, further restrict the usage of the braced group version of the
macro
to gcc 4.8 and later. This patch also fixes the warning in clang, since
clang-3.9 sets the __GNUC_* version macros to mimic gcc 4.2.1
main.c:
#include <assert.h>
int main() { assert(1); }
Without patched assert.h:
$ gcc-4.8 -pedantic main.c
$ gcc-4.7 -pedantic main.c
main.c: In function ‘main’:
main.c:2:14: warning: ISO C forbids braced-groups within expressions
[-pedantic]
$ gcc-4.4 -pedantic main.c
main.c: In function ‘main’:
main.c:2: warning: ISO C forbids braced-groups within expressions
$ clang-3.9 -pedantic main.c
main.c:2:14: warning: use of GNU statement expression extension
[-Wgnu-statement-expression]
int main() { assert(1); }
^
/usr/include/assert.h:95:6: note: expanded from macro 'assert'
({ \
^
1 warning generated.
With patched assert.h:
$ gcc-4.8 -pedantic main.c
$ gcc-4.7 -pedantic main.c
$ gcc-4.4 -pedantic main.c
$ clang-3.9 -pedantic main.c
---
assert/assert.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/assert/assert.h b/assert/assert.h
index 22f019537c..88dde76414 100644
--- a/assert/assert.h
+++ b/assert/assert.h
@@ -85,7 +85,7 @@ __END_DECLS
/* 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 __GNUC__ && __GNUC_PREREQ(4,8)) || defined __STRICT_ANSI__
# define assert(expr) \
((expr) \
? __ASSERT_VOID_CAST (0) \
--
2.12.0