Use of #if DEBUG in reent.h

Eric Blake eblake@redhat.com
Wed Jan 4 14:05:00 GMT 2017


On 01/04/2017 04:14 AM, Freddie Chopin wrote:
> Hi!
> 
> Maybe it would be ok to change the offending line to:
> 
> #if defined(DEBUG) && DEBUG != 0
> 
> This way the old behaviour should be retained in most of the cases.
>>From what I've read, if you add "-DDEBUG" to GCC flags, it is
> equivalent to "-DDEBUG=1".

Not by my experience.  There's a difference to being defined to the
empty string (-DDEBUG on the command line corresponds to '#define DEBUG'
in source) vs. an explicit string (-DDEBUG=1 corresponds to '#define
DEBUG 1').

' I also think that any undefined macro in a
> "#if whatever" statement is considered to be 0, but this is also only a
> GCC extensions.

No, it is required by the C standard.  Any otherwise-unknown
preprocessor token is required to be treated as 0 in arithmetic context.
 However, based on your choice of warning flags, gcc can treat such
usage as a preprocessor error.  Therefore, it is indeed safest to check
whether a macro is defined before coercing it into an arithmetic value,
when writing headers that must be robust to various choices of compiler
warning flags.  Gcc also has a pragma for marking a particular header as
a system header (which, among other things, silences warnings about
constructs in that header that would normally provoke warnings, such as
the implicit use of an undefined macro name meaning 0).

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 604 bytes
Desc: OpenPGP digital signature
URL: <http://sourceware.org/pipermail/newlib/attachments/20170104/97b7d630/attachment.sig>


More information about the Newlib mailing list