This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: #elseif and #ifdef funnies


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]