c++warningsbit-shiftpragmagcc4.8

How to suppress ' left shift count >= width of type' warning in g++


(My question is mostly the same as this one, but my needs are different, hence I need a different solution.)

I am attempting to use test-driven development, and am writing a set of tests to validate the behavior of my BIT(n) macro:

#define BIT(n) ( ((uint64_t)1) << (n) )

Since my test for the above includes both expected-pass and expected-fail components, I intentionally do bad things such as

MY_TEST_ASSERT(0 == (value64 = BIT(65)));

which, if there was a problem with my implementation if BIT(n), would output something helpful like

TEST FAILED: '0 == (value64 = BIT(65))' (file 'tests/002.bit-hackery.cpp', line 12))

Everything works as expected, but I get an annoying warning during compilation:

../../inc/bit-hackery.hpp:15:32: warning: left shift count >= width of type [enabled by default]

(It is actually annoying for two reasons: (1) I want it suppressed for this test code, since I am testing this situation on purpose, and (2) in real (non-test) code, I would rather it be treated as an error. But, this question is about the former.)

I've tried suppressing it with a #pragma directive, but I can't seem to find one that works:

#pragma GCC diagnostic push
//#pragma GCC diagnostic ignored "-Wcomparison-fixed"
//#pragma GCC diagnostic ignored "-Wtype-limits"
//#pragma GCC diagnostic ignored "-Wextra"
//#pragma GCC diagnostic ignored "-Wall"
  MY_TEST_ASSERT(0 != (value64 = BIT(65)));
#pragma GCC diagnostic pop

Does anyone know how to suppress this particular warning?

My compiler version is g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4


Solution

  • Unfortunately g++ v4.8.x hasn't a flag to disable that particular warning.

    You can instructs GCC/G++ to indicate which command line option directly controls each diagnostic emitted (if such an option is known to the diagnostic system) via the -fdiagnostics-show-option flag.

    In the specific case no -W option is indicated.

    For newer versions of G++/CLANG++ you've the -Wshift-count-overflow flag.

    I don't think there's a way to disable all g++ warnings on a few line of code (e.g. How to disable all warnings in g++ on a few lines of code).

    The -w flag will disable all warnings on the whole file but it's excessive.


    PS the inline-function version of BIT doesn't trigger the warning:

    std::uint64_t BIT(std::uint64_t n) { return UINT64_C(1) << n; }