https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88895
Bug ID: 88895 Summary: unnecessary warnings in unreachable code (shift) Product: gcc Version: 7.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: scott.a.mayo at gmail dot com Target Milestone: --- Created attachment 45450 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45450&action=edit processed .cpp The compiler complains about shift operations that are undefined, when the code in question is going to be unreachable anyway, at points that should be obvious even without optimization. Basically, g++ is warning about undefined shifts too early; it should wait until code generation and see what unreachable code is weeded out. The attachment shows a standard idiom for packing arbitrary integer types into buffer in big endian fashion, regardless of the endianness of the platform. g++ generates the correct code (which melts down to a single instruction!) but along the way it's squawking. I realize it's easier to warn earlier in the process, but I'm trying to enforce 0 tolerance for warnings, and having to turn off this warning is onerous since the code is a template, wants to live in a header, so turning off the warning the obvious way also turns it off in places it may be wanted. This is a big deal in safety-critical code. g++: gcc version 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04) COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.3.0-27ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --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 --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Command line: g++ -O3 -Wall StopWhining.cpp Warnings: StopWhining.cpp: In instantiation of ‘void PackRule::pack(void*, T) const [with T = unsigned int]’: StopWhining.cpp:75:34: required from here StopWhining.cpp:33:63: warning: right shift count >= width of type [-Wshift-count-overflow] case 8: tobyte[sizeof(T) - 8] = (unsigned char)(v >> 56); //waterfall ~~~^~~~~~ StopWhining.cpp:34:63: warning: right shift count >= width of type [-Wshift-count-overflow] case 7: tobyte[sizeof(T) - 7] = (unsigned char)(v >> 48); ~~~^~~~~~ StopWhining.cpp:35:63: warning: right shift count >= width of type [-Wshift-count-overflow] case 6: tobyte[sizeof(T) - 6] = (unsigned char)(v >> 40); ~~~^~~~~~ StopWhining.cpp:36:63: warning: right shift count >= width of type [-Wshift-count-overflow] case 5: tobyte[sizeof(T) - 5] = (unsigned char)(v >> 32); ~~~^~~~~~ StopWhining.cpp: In instantiation of ‘T PackRule::unpack(const void*, T*) const [with T = unsigned int]’: StopWhining.cpp:77:21: required from here StopWhining.cpp:58:63: warning: left shift count >= width of type [-Wshift-count-overflow] case 8: v |= (0xffU & (T)frombyte[sizeof(T) - 8]) << 56; //waterfall ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~ StopWhining.cpp:59:63: warning: left shift count >= width of type [-Wshift-count-overflow] case 7: v |= (0xffU & (T)frombyte[sizeof(T) - 7]) << 48; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~ StopWhining.cpp:60:63: warning: left shift count >= width of type [-Wshift-count-overflow] case 6: v |= (0xffU & (T)frombyte[sizeof(T) - 6]) << 40; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~ StopWhining.cpp:61:63: warning: left shift count >= width of type [-Wshift-count-overflow] case 5: v |= (0xffU & (T)frombyte[sizeof(T) - 5]) << 32; .ii attached