Bug 83107 - -Wshift-count-overflow does not report left shift count >= width in some cases.
Summary: -Wshift-count-overflow does not report left shift count >= width in some cases.
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2017-11-22 13:23 UTC by Clément Février
Modified: 2017-12-20 12:57 UTC (History)
2 users (show)

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 Clément Février 2017-11-22 13:23:54 UTC
-Wshift-count-overflow does not report left shift count >= width in some cases.

If the result of a<<b is cast into a type that has a longer size than b, the flag -Wshift-count-overflow will not display any warning.

I think it is a bug because, for example, (unsigned long int)1<<32 is just a recast of the result of 1<<32.
1 is an int, so 1<<32 is also an int with the value 0, which is eventually recast into an unsigned long int.
So -Wshift-count-overflow should report the count overflow.

The following code:

int main()
{
 auto val1 = 1<<32;
 auto val2 = (unsigned long int)1<<32;
}

display this message when compiled (g++ -std=gnu++11 main.cpp):

main.cpp: In function ‘int main()’:
main.cpp:3:17: warning: left shift count >= width of type [-Wshift-count-overflow]
  auto val1 = 1<<32;
                 ^~

while I would expect to have in addition:
main.cpp:4:17: warning: left shift count >= width of type [-Wshift-count-overflow]
  auto val2 = (unsigned long int)1<<32;
                                    ^~
I tried with both gcc v5 and v8. Their respective "-save-temps" output follows

% gcc -v -save-temps -std=gnu++11 main.cpp
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.5' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=gnu++11' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/5/cc1plus -E -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE main.cpp -mtune=generic -march=x86-64 -std=gnu++11 -fpch-preprocess -fstack-protector-strong -Wformat -Wformat-security -o main.ii
ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/5"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/5
 /usr/include/x86_64-linux-gnu/c++/5
 /usr/include/c++/5/backward
 /usr/lib/gcc/x86_64-linux-gnu/5/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=gnu++11' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/5/cc1plus -fpreprocessed main.ii -quiet -dumpbase main.cpp -mtune=generic -march=x86-64 -auxbase main -std=gnu++11 -version -fstack-protector-strong -Wformat -Wformat-security -o main.s
GNU C++11 (Ubuntu 5.4.0-6ubuntu1~16.04.5) version 5.4.0 20160609 (x86_64-linux-gnu)
	compiled by GNU C version 5.4.0 20160609, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++11 (Ubuntu 5.4.0-6ubuntu1~16.04.5) version 5.4.0 20160609 (x86_64-linux-gnu)
	compiled by GNU C version 5.4.0 20160609, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 86227c1230bcf689e6a1b7d559effd6b
main.cpp: In function ‘int main()’:
main.cpp:3:17: warning: left shift count >= width of type [-Wshift-count-overflow]
  auto val1 = 1<<32;
                 ^
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=gnu++11' '-mtune=generic' '-march=x86-64'
 as -v --64 -o main.o main.s
GNU assembler version 2.26.1 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.26.1
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=gnu++11' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/5/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/5/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper -plugin-opt=-fresolution=main.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/5 -L/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/5/../../.. main.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/5/crtend.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crtn.o


% gcc -v -save-temps main.cpp
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/home/clement/bin/libexec/gcc/x86_64-pc-linux-gnu/8.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc/configure --prefix=/home/clement/bin
Thread model: posix
gcc version 8.0.0 20171023 (experimental) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-mtune=generic' '-march=x86-64'
 /home/clement/bin/libexec/gcc/x86_64-pc-linux-gnu/8.0.0/cc1plus -E -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE main.cpp -mtune=generic -march=x86-64 -fpch-preprocess -o main.ii
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../include/c++/8.0.0
 /home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../include/c++/8.0.0/x86_64-pc-linux-gnu
 /home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../include/c++/8.0.0/backward
 /home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/include
 /usr/local/include
 /home/clement/bin/include
 /home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-mtune=generic' '-march=x86-64'
 /home/clement/bin/libexec/gcc/x86_64-pc-linux-gnu/8.0.0/cc1plus -fpreprocessed main.ii -quiet -dumpbase main.cpp -mtune=generic -march=x86-64 -auxbase main -version -o main.s
GNU C++14 (GCC) version 8.0.0 20171023 (experimental) (x86_64-pc-linux-gnu)
	compiled by GNU C version 8.0.0 20171023 (experimental), GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
GNU C++14 (GCC) version 8.0.0 20171023 (experimental) (x86_64-pc-linux-gnu)
	compiled by GNU C version 8.0.0 20171023 (experimental), GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
Compiler executable checksum: 7a9ea9098b162b5d41035c367ad9dcf8
main.cpp: In function ‘int main()’:
main.cpp:3:17: warning: left shift count >= width of type [-Wshift-count-overflow]
  auto val1 = 1<<32;
                 ^~
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-mtune=generic' '-march=x86-64'
 as -v --64 -o main.o main.s
GNU assembler version 2.26.1 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.26.1
COMPILER_PATH=/home/clement/bin/libexec/gcc/x86_64-pc-linux-gnu/8.0.0/:/home/clement/bin/libexec/gcc/x86_64-pc-linux-gnu/8.0.0/:/home/clement/bin/libexec/gcc/x86_64-pc-linux-gnu/:/home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/:/home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/:/home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../lib64/:/lib/x86_64-linux-gnu/:/lib/../lib64/:/usr/lib/x86_64-linux-gnu/:/home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-mtune=generic' '-march=x86-64'
 /home/clement/bin/libexec/gcc/x86_64-pc-linux-gnu/8.0.0/collect2 -plugin /home/clement/bin/libexec/gcc/x86_64-pc-linux-gnu/8.0.0/liblto_plugin.so -plugin-opt=/home/clement/bin/libexec/gcc/x86_64-pc-linux-gnu/8.0.0/lto-wrapper -plugin-opt=-fresolution=main.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o /home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/crtbegin.o -L/home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0 -L/home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/../../.. main.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /home/clement/bin/lib/gcc/x86_64-pc-linux-gnu/8.0.0/crtend.o /usr/lib/x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-mtune=generic' '-march=x86-64'
Comment 1 Martin Liška 2017-12-20 12:57:06 UTC
> I think it is a bug because, for example, (unsigned long int)1<<32 is just a
> recast of the result of 1<<32.
> 1 is an int, so 1<<32 is also an int with the value 0, which is eventually
> recast into an unsigned long int.
> So -Wshift-count-overflow should report the count overflow.

It's not bug because the expression is equivalent to:

 auto val2 = ((unsigned long int)1)<<32;

Which is correct. You are talking about:

pr83107.cpp:5:37: warning: left shift count >= width of type [-Wshift-count-overflow]
  auto val3 = (unsigned long int)(1<<32);