bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: assert.h replacement (was: Re: __func__)


From: Eric Blake
Subject: Re: assert.h replacement (was: Re: __func__)
Date: Tue, 4 Mar 2008 15:38:10 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Simon Josefsson <simon <at> josefsson.org> writes:

> Ah, ok, I thought you talked about the existing 'assert' module in
> gnulib, which looks unrelated to what you are proposing.

I'm actually thinking that that it would be better to make the existing assert 
module serve the two orthogonal purposes (provide ./configure --disable-assert, 
and make assert() guarantee that __func__ is included in the output), rather 
than creating a new module.  And note that while it is easy to test for 
__func__ support, there are platforms with __func__ support but a broken assert 
(such as cygwin 1.5.x), so you would need an AC_RUN_IFELSE test to detect it.

> 
> Couldn't a replacement assert.h look like:
> 
> #include <stdio.h>
> #include "progname.h"
> 
> #ifdef NDEBUG
> # define assert(e) ((void) 0)
> #else
> # define assert(e)                                            \
>   if (!(e)) {                                                 \
>     fprintf (stderr, "%s: %s: %s: %s: Assertion failed.\n",   \
>            program_name, __FILE__, __LINE__, __func__);       \

Not quite.  Using if(){} will break if assert occurs as the sole statement 
inside another if-else pair - use while(){} or ?: instead.  You also need to 
use #e to get the stringized text of the expression into the output, and while 
program_name is nice, it is not required (using it would drag in a dependency 
on program_name).  POSIX states that "The information written about the call 
that failed shall include the text of the argument, the name of the source 
file, the source file line number, and the name of the enclosing function;".  
But I think you are right that we can avoid the need for an rpl_assert function.

> 
> But are there any platforms that lack an assert.h?

Not to my knowledge - at any rate, several gnulib modules assume its presence 
unconditionally.

-- 
Eric Blake






reply via email to

[Prev in Thread] Current Thread [Next in Thread]