We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 176 - SDL_stdinc.h uses #if when it should be using #ifdef
Summary: SDL_stdinc.h uses #if when it should be using #ifdef
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: *don't know* (show other bugs)
Version: HG 1.2
Hardware: All Other
: P2 normal
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-03-22 12:31 UTC by Max Horn
Modified: 2006-04-13 09:38 UTC (History)
0 users

See Also:


Attachments
Patch for SDL_stdinc.h (9.92 KB, patch)
2006-03-22 12:37 UTC, Max Horn
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Max Horn 2006-03-22 12:31:14 UTC
SDL_stdinc.h generates a lot of warnings for me (or rather, errors, since I usually use -Werror). The reason is that it uses #if to check the various HAVE_XYZ flags. But these flags are actually not define to 1 or 0 to describer the presence/absence of a feature -- rather, they are either defined to be 1, or not defined at all!

Hence, the proper way to check for them is to use #ifdef or #if defined().

The attached patch does exactly this.
Comment 1 Max Horn 2006-03-22 12:37:14 UTC
Created attachment 89 [details]
Patch for SDL_stdinc.h
Comment 2 Sam Lantinga 2006-03-22 12:41:47 UTC
What compiler do you have that doesn't accept that?

All the compilers I've seen have the following behavior
foo not defined
#if foo -> FALSE
#undef foo
#if foo -> FALSE
#define foo 0
#if foo -> FALSE
#define foo 1
#if foo -> TRUE

For the record, if you look at the autoconf configure tests, they use exactly the same convention:
# Factoring default headers for most tests.
ac_includes_default="\
#include <stdio.h>
#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#if HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#if STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
...
Comment 3 Max Horn 2006-03-22 13:02:06 UTC
To clarify: Normaly, GCC (or, to be precise, the preprocessor) will ignore this, and compile the code happily. However, one can specify -Wundef to get a warning about this.

One can probably argue whether to consider this a bug or not; but I think that (a) from a semantic point of view, using "#if FOO" when FOO is not defined is strange, and (b) since it is possible to trigger a warning about this, and a trivial fix exists, it should be corrected.

I can think of two alternative patches, BTW:
1) Simply use #define HAVE_FOO 0, instead of not defining HAVE_FOO at all
2) Change
  #if HAVE_FOO
to
  #if HAVE_FOO+0
which always does the right thing.

But I think I still prefer the attached patch :-).
Comment 4 Sam Lantinga 2006-04-13 09:38:53 UTC
This patch (and a couple cases you missed) is in CVS.  Thanks!