[Bug c/80383] wrong caret location and missing detail in warning: initializer element is not a constant expression on a signed overflow

egallager at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Aug 1 11:04:00 GMT 2017


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80383

Eric Gallager <egallager at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2017-08-01
                 CC|                            |egallager at gcc dot gnu.org
     Ever confirmed|0                           |1

--- Comment #1 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Martin Sebor from comment #0)
> The caret in the pedantic warnings issued for the following test case points
> to the wrong subexpression, making the warning confusing.  The caret should
> point to the shift expression and the operands should be underlined.  It
> would help if the warning also explained why the result of the shift
> expression isn't a constant expression.  Perhaps -Wshift-overflow=2 should
> be enabled by -Wpedantic?
> 
> $ cat b.c && gcc -O2 -S -Wall -Wextra -Wpedantic b.c
> const int i = 1 << (sizeof (int) * __CHAR_BIT__ - 1);
> const int j = 1 << (sizeof (int) * __CHAR_BIT__);
> b.c:1:15: warning: initializer element is not a constant expression
> [-Wpedantic]
>  const int i = 1 << (sizeof (int) * __CHAR_BIT__ - 1);
>                ^
> b.c:2:17: warning: left shift count >= width of type [-Wshift-count-overflow]
>  const int j = 1 << (sizeof (int) * __CHAR_BIT__);
>                  ^~
> b.c:2:15: warning: initializer element is not a constant expression
> [-Wpedantic]
>  const int j = 1 << (sizeof (int) * __CHAR_BIT__);
>                ^
> 

Confirmed for this part at least.

> In contrast, with -Wshift-overlow=2, GCC prints the following.  With
> -Wshift-overflow and -Wshift-count-overflow the caret is in the right place
> (but the operands aren't underlined).
> 
> b.c:1:17: warning: result of ‘1 << 31’ requires 33 bits to represent, but
> ‘int’ only has 32 bits [-Wshift-overflow=]
>  const int i = 1 << (sizeof (int) * __CHAR_BIT__ - 1);
>                  ^~
> b.c:1:15: warning: initializer element is not a constant expression
> [-Wpedantic]
>  const int i = 1 << (sizeof (int) * __CHAR_BIT__ - 1);
>                ^
> b.c:2:17: warning: left shift count >= width of type [-Wshift-count-overflow]
>  const int j = 1 << (sizeof (int) * __CHAR_BIT__);
>                  ^~
> b.c:2:15: warning: initializer element is not a constant expression
> [-Wpedantic]
>  const int j = 1 << (sizeof (int) * __CHAR_BIT__);
>                ^
> 

I tried testing this but it looks like the -Wshift-overflow=2 option has
broken; I'll have to open a separate bug about that:

$ /usr/local/bin/gcc -c -O2 -S -Wall -Wextra -Wpedantic -Wshift-overlow=2
80383.c
gcc: error: unrecognized command line option ‘-Wshift-overlow=2’; did you mean
‘-Wshift-overflow=’?
$ /usr/local/bin/gcc -c -O2 -S -Wall -Wextra -Wpedantic -Wshift-overlow 80383.c
gcc: error: unrecognized command line option ‘-Wshift-overlow’; did you mean
‘-Wshift-overflow’?


More information about the Gcc-bugs mailing list