public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Jim Meyering <jim@meyering.net>
To: libc-alpha@sourceware.org
Cc: Roland McGrath <roland@hack.frob.com>
Subject: [PATCH] assert.h: allow gcc to detect assert(a = 1) errors
Date: Wed, 16 Jul 2014 19:12:00 -0000	[thread overview]
Message-ID: <1405537923-28692-1-git-send-email-jim@meyering.net> (raw)

From: Jim Meyering <meyering@fb.com>

* 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 the parentheses added by assert.
The new definition uses "if (expr) ; else __assert_fail...", so
gcc -Wall will now detect that type of error in an assert, too.
---
 assert/assert.h | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/assert/assert.h b/assert/assert.h
index 2661391..ffcf60c 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 (__STRING(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 (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))
+# else
+#  define assert(expr)							\
+    ({									\
+      if (expr)								\
+        ; /* empty */							\
+      else								\
+        __assert_fail (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION);\
+    })
+# endif

 # ifdef	__USE_GNU
 #  define assert_perror(errnum)						\
-- 
2.0.1

             reply	other threads:[~2014-07-16 19:12 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-16 19:12 Jim Meyering [this message]
2014-07-16 20:15 ` Roland McGrath
2014-07-16 20:44   ` Jim Meyering
2016-11-24  2:22     ` Jim Meyering
2016-11-24  2:59       ` Zack Weinberg
2016-11-24 13:13         ` Joseph Myers
2016-11-26  6:36           ` Jim Meyering
2016-11-24  7:36       ` Florian Weimer
2016-11-26  6:15         ` Jim Meyering
2016-12-09  3:21           ` Jim Meyering
2016-12-14  5:28             ` Jim Meyering
2016-12-14  9:13           ` Florian Weimer
2016-12-15  0:25             ` Jim Meyering
2016-12-16 12:15               ` Florian Weimer
2016-12-18  9:53                 ` Jim Meyering
2016-12-27 17:47               ` Andreas Schwab
2016-12-28 12:50                 ` Florian Weimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1405537923-28692-1-git-send-email-jim@meyering.net \
    --to=jim@meyering.net \
    --cc=libc-alpha@sourceware.org \
    --cc=roland@hack.frob.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).