Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2002:
[Freeciv-Dev] Re: unused variables when compiling with NDEBUG
Home

[Freeciv-Dev] Re: unused variables when compiling with NDEBUG

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: jdorje@xxxxxxxxxxxx
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: unused variables when compiling with NDEBUG
From: "Ross W. Wetmore" <rwetmore@xxxxxxxxxxxx>
Date: Sun, 29 Sep 2002 22:39:19 -0400

The 3rd solution is by far the ugliest.

Anything with the do ... while(0) construct is particularly lame since
this breaks the macro/function equivalence paradigm. Macros written so
they may be easily replaced by functions with a one-line header change
are what all macros should strive for.

If you are plagued by minor nits, use #2, or simply remove all the
is_real elements leaving the normalize_map_pos() to just normalize
as #4. 

Lastly something try this.

#ifdef NDEBUG
#  define PARANOID_NORMLIZE(x,y) normalize_map_pos(x,y)
#else
#  define PARANOID_NORMLIZE(x,y) assert(normalize_map_pos(x,y))
#endif

Cheers,
RossW
=====

At 03:33 AM 02/09/28 -0500, Jason Dorje Short wrote:
>There is a lot of code of the form
>
>   int is_real = normalize_map_pos(&x, &y);
>   assert(is_real);
>
>The problem is that when compiling with NDEBUG, this gives a compiler 
>warning since is_real is unused.  This is at the least ugly, and hurtful 
>to developers who compile with -Werror (like me).
>
>There are several possibilities:
>
>1.  Leave it like it is.
>
>2.  Change it to:
>
>   int is_real;
>   is_real = normalize_map_pos(&x, &y);
>   assert(is_real);
>
>which simply confuses the compiler (gcc, anyway) into thinking the 
>variable is used.
>
>3.  Do something clever.  This could relate to the recent discussions 
>about better error detection without failure.  It could be something 
>simple like:
>
>#ifdef NDEBUG
>#  define CHECK(x) ((void) (x))
>#else
>#  define CHECK(x) do { int is = (x); assert(is); } while(0)
>#endif
>
>so that the code would then become
>
>   CHECK(normalize_map_pos(&x, &y));
>
>and would be simpler throughout.
>
>
>IMO the third solution is the least ugly.
>
>jason
>
>
>



[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] Re: unused variables when compiling with NDEBUG, Ross W. Wetmore <=