[clang] [clang] Emit bad shift warnings (PR #70307)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 18 04:48:13 PDT 2024


AaronBallman wrote:

> Just to confirm: is this the intentional outcome of this patch?

Before C++20, left shift of -1 does not produce a mathematical result that's within the range of representable values for the result type, so it's undefined behavior, and therefore not a core constant expression, and therefore invalid as the initializer for an enumeration constant. So I believe the behavior is expected and correct; you can see the UB diagnostic in other contexts: https://gcc.godbolt.org/z/bqj6xhzzr and GCC agrees with Clang in the enumeration case: https://gcc.godbolt.org/z/qjKzeaYTz

> the diagnostic - "expression is not an integral constant expression" - doesn't sound helpful (how is it not constant or not integral?). And what's worse, is that it's an error rather than a warning that can be disabled.

It has integral constants but it's not an integral constant expression as far as the language is concerned. The note tells you why it's not a valid integral constant expression. As for warning vs error; the standard makes the code ill-formed and we treat that as an error generally speaking.

If you can't enable C++20 mode, you can still get the same value in C++17 and earlier with well-formed code: https://gcc.godbolt.org/z/3ra83joqs

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


More information about the cfe-commits mailing list