[PATCH] Tweak assert -DNDEBUG for the new -Wunused-but-set-{variable,parameter} warnings

Jakub Jelinek jakub@redhat.com
Thu Apr 8 10:22:00 GMT 2010


Hi!

I've yesterday committed to GCC trunk a patch to support
new -Wunused-but-set-{variable,parameter} warnings where GCC
warns about variables that have been just set but not really
used.

This can create unwanted warnings with current definition of assert
and -DNDEBUG - people often do something like:

void
foo (void)
{
  int r;
  r = bar ();
  assert (r != 6);
}

The variable is set and used without -DNDEBUG, but with it if
assert expands just to (void) 0 then it is just set but not used.

The following patch shouldn't result in worse code generation (not
even with -O0 I believe) with -DNDEBUG, should quiet that warning
and also allow some checking of expr.  With -DNDEBUG assert will
happily accept even syntax errors within its arguments etc., which
would only show up without -DNDEBUG, this patch ought to cure that
too.  Alternative to the 0 && (expr) could be sizeof (expr) or
something else where the expression isn't evaluated, but is parsed.

2010-04-08  Jakub Jelinek  <jakub@redhat.com>

	* assert/assert.h [NDEBUG] (assert, assert_perror): Include
	the argument after "0 &&".

--- libc/assert/assert.h.jj	2009-05-16 19:23:29.000000000 +0200
+++ libc/assert/assert.h	2010-04-08 12:10:06.000000000 +0200
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991,1992,1994-2001,2003,2004,2007
+/* Copyright (C) 1991,1992,1994-2001,2003,2004,2007,2010
    Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
@@ -49,7 +49,7 @@
 
 #ifdef	NDEBUG
 
-# define assert(expr)		(__ASSERT_VOID_CAST (0))
+# define assert(expr)		(__ASSERT_VOID_CAST (0 && (expr)))
 
 /* void assert_perror (int errnum);
 
@@ -58,7 +58,7 @@
    (This is a GNU extension.) */
 
 # ifdef	__USE_GNU
-#  define assert_perror(errnum)	(__ASSERT_VOID_CAST (0))
+#  define assert_perror(errnum)	(__ASSERT_VOID_CAST (0 && (errnum)))
 # endif
 
 #else /* Not NDEBUG.  */


	Jakub



More information about the Libc-hacker mailing list