[clang] 4e45876 - [Clang] Restrict non fixed enum to a value outside the range of the enumeration values warning to context requiring a constant expression

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 10 11:12:09 PDT 2022


Author: Shafik Yaghmour
Date: 2022-08-10T11:12:01-07:00
New Revision: 4e458765aaef7988e687e190d865f331727825c0

URL: https://github.com/llvm/llvm-project/commit/4e458765aaef7988e687e190d865f331727825c0
DIFF: https://github.com/llvm/llvm-project/commit/4e458765aaef7988e687e190d865f331727825c0.diff

LOG: [Clang] Restrict non fixed enum to a value outside the range of the enumeration values warning to context requiring a constant expression

In D131307 we allowed the diagnostic to be turned into a warning for a
transition period.

This had the side effect of triggering the warning in contexts not required to
be constant expression. This change will restrict the diagnostic to constant
expression contexts. This should reduce the fallout of this diagnostic.

Differential Revision: https://reviews.llvm.org/D131528

Added: 
    

Modified: 
    clang/lib/AST/ExprConstant.cpp
    clang/test/SemaCXX/constant-expression-cxx11.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 4a1f0852381db..992bc9aa5008f 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13533,7 +13533,9 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
       return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
     }
 
-    if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) {
+    if (Info.Ctx.getLangOpts().CPlusPlus &&
+        Info.EvalMode == EvalInfo::EM_ConstantExpression &&
+        DestType->isEnumeralType()) {
       const EnumType *ET = dyn_cast<EnumType>(DestType.getCanonicalType());
       const EnumDecl *ED = ET->getDecl();
       // Check that the value is within the range of the enumeration values.

diff  --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp
index c5742ad8c2cbb..3760fa413174a 100644
--- a/clang/test/SemaCXX/constant-expression-cxx11.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp
@@ -2421,6 +2421,7 @@ void testValueInRangeOfEnumerationValues() {
   constexpr E1 x1 = static_cast<E1>(-8);
   constexpr E1 x2 = static_cast<E1>(8);
   // expected-error at -1 {{integer value 8 is outside the valid range of values [-8, 7] for this enumeration type}}
+  E1 x2b = static_cast<E1>(8); // ok, not a constant expression context
 
   constexpr E2 x3 = static_cast<E2>(-8);
   // expected-error at -1 {{integer value -8 is outside the valid range of values [0, 7] for this enumeration type}}


        


More information about the cfe-commits mailing list