This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: #elseif and #ifdef funnies
- To: Alex Buell <alex dot buell at tahallah dot demon dot co dot uk>
- Subject: Re: #elseif and #ifdef funnies
- From: Zack Weinberg <zack at codesourcery dot com>
- Date: Wed, 31 Oct 2001 17:48:48 -0800
- Cc: Mailing List - GCC <gcc at gcc dot gnu dot org>
- References: <Pine.LNX.4.33.0111010056150.6705-100000@tahallah.demon.co.uk>
On Thu, Nov 01, 2001 at 12:59:42AM +0000, Alex Buell wrote:
> I just stumbled across something strange with the #elseif and #elif
> pragmas tonight. Here's a program snippet:
>
> #include <stdio.h>
>
> /* #define one */
> #define two 1
>
> int main(int argc, char *argv[])
> {
>
> #if defined(one)
> printf("One found\n");
> #elif defined(two)
> printf("Two found\n");
> #else
> printf("One and two not found\n");
> #endif
>
> return 0;
> }
>
> This works, produces the expected output 'Two found'. However, if I do a
> s/elif/elseif, and compile it, the output is wrong, it prints 'One and two
> found'.
There is no "#elseif" preprocessing directive.
You don't get a complaint from the preprocessor because the invalid
directive is inside a failed conditional group. Perhaps it will be
clearer if I illustrate it thus:
#if __FROBOZZ_MAGIC_CC__
... stuff ...
#nonsense directive
... more stuff ...
#endif
For all GCC knows, "#nonsense directive" is a Frobozz Magic CC
extension, properly disabled by #if, and therefore GCC should not
complain about it.
Uncomment the #define one in your example and you'll get a hard error
from GCC (both 2.95.x and 3.0.x tested).
A case could be made for specifically recognizing #elsif and #elseif
and complaining about them even in failed conditional blocks. However,
I suspect it would only help a few people.
zs