gcc suppresses the warning "too small to contain all values"

I need to use enumerations with a limited range so that I can pass them as specific types to our serializer. I gave explicit integer values ​​for the enum members of Enum1 .

Thus, I put two enumerations with the scope corresponding to the one described above in the bit field

 enum class Enum1 { value1 = 0x0, value2 = 0x1, value3 = 0x2 }; enum class Enum2 { value1 = 0x0, value2, value3, // ... value14 }; struct Example { Enum1 value1 : 2; Enum2 value2 : 6; } 

Now, wherever I use the Example type, I get the warning “Example :: value1 'is too small to contain all the values ​​of Enum1", and similarly for Enum2 . Please note that this does not apply to the values ​​that we have defined, and we are not at all interested in values ​​outside of them.

This is a pretty serious distraction in our build process - the project is large and complex, and we do not need to scan many of these warnings (and there are many).

I searched for the GCC flag (G ++) to disable a specific warning. Is there one that I can pass on the command line? Ideally, I would use a warning pragma to disable it locally, if possible.

At the moment there are few opportunities to change the structure of the code, but we can really use these false warnings.

Edit: added voluminous listings with changed identifiers.

+10
source share
2 answers

The problem is that the enumeration cloud always has an integral base type. By default it is int , but you can change it to any other integral type, for example unsigned char .

Unfortunately, you cannot change the base type to a bit field, since they are not real C ++ types.

You can try turning off the warning, but a quick look through g ++ code shows these lines ( gcc/cp/class.c:3468 ):

  else if (TREE_CODE (type) == ENUMERAL_TYPE && (0 > (compare_tree_int (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type)))))) warning_at (DECL_SOURCE_LOCATION (field), 0, "%qD is too small to hold all values of %q#T", field, type); 

The key here is calling warning_at(...) instead of warning(OPT_to_disable_the_warning, ...) . Therefore, there is currently no way to disable it. Besides recompiling the compiler yourself!

Why CLang ++ is worth it - 3.7.1 does not warn about this.

+8
source

As far as I remember, an enumeration with a declared base type can contain any value of this type, regardless of which enumeration constants are defined. So how can you say

 val= enum2{148} 

and expect this to work properly, the warning seems right for this case. You do not declare a base type, and historically this means that the enumeration is guaranteed to be large enough to contain the range of values ​​specified by the enumeration constant from the smallest to the highest. So I would not expect a warning here. Maybe the new enum class also expects a full range, even if the base type was determined automatically (or does the compiler think that it is)? You can try using an enumeration of the clean old syntax and see if it works differently.

+2
source

Source: https://habr.com/ru/post/1245063/


All Articles