diff options
author | Jim Meyering <meyering@fb.com> | 2014-06-05 10:42:05 -0700 |
---|---|---|
committer | Jim Meyering <meyering@fb.com> | 2016-12-18 01:30:51 -0800 |
commit | e077349ce589466eecd47213db4fae6b80ec18c4 (patch) | |
tree | 3f8dc06410c398f69426bf115e643284a133436b /assert | |
parent | 009ba649b4999ea269de8d0b301e659f075df495 (diff) | |
download | glibc-e077349ce589466eecd47213db4fae6b80ec18c4.zip glibc-e077349ce589466eecd47213db4fae6b80ec18c4.tar.gz glibc-e077349ce589466eecd47213db4fae6b80ec18c4.tar.bz2 |
assert.h: allow gcc to detect assert(a = 1) errors
* assert/assert.h (assert): Rewrite assert's definition so that
a s/==/=/ typo, e.g., assert(errno = ENOENT) is not hidden from
gcc's -Wparentheses by assert-added parentheses. The new definition
uses "if (expr) /* empty */; else __assert_fail...", so
gcc -Wall will now detect that type of error in an assert, too.
The __STRICT_ANSI__ disjunct is to make this work also with both
-ansi and -pedantic, which would reject the use of ({...}).
I would have preferred to use __extension__ to mark that, but
doing so would mistakenly suppress warnings about any extension
in the user-supplied "expr".
E.g., "assert ( ({1;}) )" must continue to evoke a warning.
Diffstat (limited to 'assert')
-rw-r--r-- | assert/assert.h | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/assert/assert.h b/assert/assert.h index 729edeb..0f25131 100644 --- a/assert/assert.h +++ b/assert/assert.h @@ -82,10 +82,23 @@ extern void __assert (const char *__assertion, const char *__file, int __line) __END_DECLS -# define assert(expr) \ - ((expr) \ - ? __ASSERT_VOID_CAST (0) \ - : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION)) +/* 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__ +# define assert(expr) \ + ((expr) \ + ? __ASSERT_VOID_CAST (0) \ + : __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION)) +# else +# define assert(expr) \ + ({ \ + if (expr) \ + ; /* empty */ \ + else \ + __assert_fail (#expr, __FILE__, __LINE__, __ASSERT_FUNCTION); \ + }) +# endif # ifdef __USE_GNU # define assert_perror(errnum) \ |