Bug 71803

Summary: Wpedantic: enumerator value is not an integer constant expression
Product: gcc Reporter: Johan Förberg <johan>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED INVALID    
Severity: normal    
Priority: P3    
Version: 6.1.1   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Johan Förberg 2016-07-07 21:23:27 UTC
This concerns GCC 6.1.1 running on Arch Linux amd64. The following code will produce a warning if compiled with -Wpedantic:

    enum { TEST = 1 << 31 };

The warning is:

    warning: enumerator value for ‘TEST’ is not an integer constant expression [-Wpedantic]

But if I instead write 

    enum { TEST = 1 << 30 };

There is no warning.

This is unforunate since e.g. the glib/gio headers contain such enums, so they will add warnings to the compile if using -Wpedantic.

I don't feel that this warning is motivated. According to the C11 standard (final draft, page 106), an integer constant expression "shall have integer type and shall only have operands that are integer constants", which by my understanding this expression satisfies.

Thanks for you attention,
Johan
Comment 1 Andrew Pinski 2016-07-07 21:55:09 UTC
1 << 31

is not a constant integral expression in C99/C90/C11 since you are overflowing the 1 to the sign bit.  If you want 1 << 31, then you can use "(int)(1u << 31)" or if you want unsigned then you can just do 1u << 31.