Bug 47701 - separate warning to detect c99 enum constraint violation
Summary: separate warning to detect c99 enum constraint violation
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.4.5
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-02-11 16:53 UTC by Eric Blake
Modified: 2015-04-23 10:13 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Blake 2011-02-11 16:53:39 UTC
C99 6.7.2.2 states:

The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.  Obviously, gcc has an extension that any integral value can be used, and -pedantic can detect that this extension was utilized:

$ cat foo.c
enum { a = 0x100000001LL };
int main(void) { return a; }
$ gcc -Wall -Wextra -std=c99 -fdiagnostics-show-option foo.c -o foo
$ gcc -Wall -Wextra -std=c99 -pedantic -fdiagnostics-show-option foo.c -o foo
foo.c:1: warning: ISO C restricts enumerator values to range of ‘int’ [-pedantic]
$ ./foo; echo $?
1

However, -pedantic is a rather heavy hammer; I would love to have a dedicated -W option, independent of -pedantic, for detecting just the issue of using an integer constant outside the range of int, to be sure that I don't fall foul of something like failure to compiler, or worse behavior like silent enum truncation, on other compilers that don't implement the same extension as gcc.
Comment 1 Marek Polacek 2015-04-23 10:13:10 UTC
The compiler now says

t.c:1:12: warning: ISO C restricts enumerator values to range of ‘int’ [-Wpedantic]
 enum { a = 0x100000001LL };
            ^
t.c: In function ‘main’:
t.c:2:25: warning: overflow in implicit constant conversion [-Woverflow]
 int main(void) { return a; }
                         ^
So I suppose we can close this as fixed now.  Please reopen if you want something else.