[clang] [clang][Sema] Add -Wswitch-default warning option (PR #73077)

Aaron Puchert via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 3 16:29:13 PST 2023


aaronpuchert wrote:

In the end it probably boils down to how you view an enumeration type. Is it a type with the declared enumerators as inhabitants, or is it an integer type of some length with associated symbolic constants of that type? In other words, is an enumeration a "strong" type or a "weak" type?

The standard itself seems more in the "weak" camp. Yes, it does not allow integer values larger than what the largest enumeration value requires (if the underlying integer type isn't declared), but otherwise integer values that don't correspond to enumeration values can be cast to the enumeration type just fine. In [[dcl.enum]p8](https://eel.is/c++draft/enum#dcl.enum-8):

> [For an enumeration whose underlying type is not fixed,] the values of the enumeration are the values representable by a hypothetical integer type with minimal width _M_ such that all enumerators can be represented.

However, nothing prevents a developer from assuming a stricter model, where only the declared enumeration values are allowed and everything else is undefined behavior. (If not in terms of the language, then in terms of the program.) That is what we have done in the LLVM code base, and it's also the model that I personally prefer.

But it is probably legitimate to follow the weaker model implied by the standard and assume that enumerations can have non-enumerator values, which requires a default label, enforced by this warning.

https://github.com/llvm/llvm-project/pull/73077


More information about the cfe-commits mailing list