From patchwork Wed Jul 16 19:12:03 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Meyering X-Patchwork-Id: 2096 Received: (qmail 15168 invoked by alias); 16 Jul 2014 19:12:39 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 15123 invoked by uid 89); 16 Jul 2014 19:12:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham version=3.3.2 X-HELO: hx.meyering.net From: Jim Meyering To: libc-alpha@sourceware.org Cc: Roland McGrath Subject: [PATCH] assert.h: allow gcc to detect assert(a = 1) errors Date: Wed, 16 Jul 2014 12:12:03 -0700 Message-Id: <1405537923-28692-1-git-send-email-jim@meyering.net> From: Jim Meyering * 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) \