[PATCH] D130058: [Clang] Diagnose ill-formed constant expression when setting a non fixed enum to a value outside the range of the enumeration values
Erich Keane via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 20 06:08:37 PDT 2022
erichkeane added inline comments.
================
Comment at: clang/lib/AST/Decl.cpp:4609
+
+ if (NumNegativeBits) {
+ unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1);
----------------
How does this work if both NumNegativeBits and NumPositiveBits are 0? Based on my reading of the standard, an empty enum should have the valid values 0 and 1, so a 'bitwidth' of 1.
================
Comment at: clang/test/SemaCXX/constant-expression-cxx11.cpp:2409
+// Empty but as-if it had a single enumerator with value 0
+enum EEmpty {};
+
----------------
Would also like to see `enum E4 {e41=0};`, which should have the values 0 and 1.
================
Comment at: clang/test/SemaCXX/constant-expression-cxx11.cpp:2429
+ constexpr EEmpty x7 = static_cast<EEmpty>(1); // expected-error {{must be initialized by a constant expression}}
+ // expected-note at -1 {{store of value outside of the range of unscoped enum, valid values 0 to 0}}
+
----------------
Ok, so I think '1' should be valid here based on my reading.
An empty should be the exact same case as the proposed `E4` above, and I believe we can never allow 'zero' bits.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D130058/new/
https://reviews.llvm.org/D130058
More information about the cfe-commits
mailing list