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

Bug#202319: cpp: enum in #if does not cause warning/error



Package: cpp
Version: 3:3.3-2
Severity: normal
Tags: upstream

If an enumerated type is used in #if statements no warning/error is given and
the TRUE branch of the #if is always executed.

Using enum in #if is declared illegal by K&R along with cast and sizeof
(page 91, paragraph 2 of 4.11.3 of "The C Programming Language, Second
Edition" [ANSI C]).

We use enum and #defines fairly interchangeably in header files for
project constants and ran into a case where I want to conditionally compile 
code based on the value of the constant -- which happened to be an enumerated
type.

If the constant is a #define it works great.  If the constant is an enum
value it always evaluates the TRUE case of the
    #if ... 
    #endif 
which is not obvious when reading the code.

---- sample code follows ----

#include <stdio.h>

enum {S=32, T=33};
#define MY T

int main(void)
{
#if (MY == S)
    printf("MY(%d) == S\n", MY);
#else
    printf("MY(%d) != S\n", MY);
#endif
    return 0;
}

---- end of sample code ----

If the enum {S=32, T=33}; is replaced with
#define S 32
#define T 33
the code compiles properly.

Since normally the constants/enums are lost in some project
header file you won't necessarily know if it was created by 
an enum or a #define and some warning or error message would be
appreciated.

Many thanks for an awesome compiler guys!

Regards,
Bernt

-- System Information:
Debian Release: testing/unstable
Architecture: i386
Kernel: Linux gollum 2.4.21-k7 #1 Wed Jul 2 13:30:26 EDT 2003 i686
Locale: LANG=en_CA, LC_CTYPE=en_CA

Versions of packages cpp depends on:
ii  cpp-3.3                    1:3.3.1-0pre0 The GNU C preprocessor

-- no debconf information




Reply to: