[clang] 2608f55 - [Clang] Tighten restrictions on enum out of range diagnostic

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 11 13:44:33 PDT 2022


Author: Shafik Yaghmour
Date: 2022-08-11T13:44:26-07:00
New Revision: 2608f553b8fd02bfd5a81d9e45406cee0c2dfe26

URL: https://github.com/llvm/llvm-project/commit/2608f553b8fd02bfd5a81d9e45406cee0c2dfe26
DIFF: https://github.com/llvm/llvm-project/commit/2608f553b8fd02bfd5a81d9e45406cee0c2dfe26.diff

LOG: [Clang] Tighten restrictions on enum out of range diagnostic

In D131528 using Info.EvalMode == EvalInfo::EM_ConstantExpression is not strict
enough to restrict the diagnostic to only constant expression contexts. It is
sometimes set in cases where we are still determining if we are in a constant
expression context.

Using InConstantContext will tighten the restriction.

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

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 992bc9aa5008f..67a1fa4318661 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13533,8 +13533,7 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
       return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
     }
 
-    if (Info.Ctx.getLangOpts().CPlusPlus &&
-        Info.EvalMode == EvalInfo::EM_ConstantExpression &&
+    if (Info.Ctx.getLangOpts().CPlusPlus && Info.InConstantContext &&
         DestType->isEnumeralType()) {
       const EnumType *ET = dyn_cast<EnumType>(DestType.getCanonicalType());
       const EnumDecl *ED = ET->getDecl();

diff  --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp
index 23dfde872f121..c4f64ff9cd30a 100644
--- a/clang/test/SemaCXX/constant-expression-cxx11.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp
@@ -2455,4 +2455,18 @@ void testValueInRangeOfEnumerationValues() {
   constexpr EMaxInt x20 = static_cast<EMaxInt>((long)__INT_MAX__+1);
   // expected-error at -1 {{integer value 2147483648 is outside the valid range of values [-2147483648, 2147483647] for this enumeration type}}
 }
+
+enum SortOrder {
+  AscendingOrder,
+  DescendingOrder
+};
+
+class A {
+  static void f(SortOrder order);
+};
+
+void A::f(SortOrder order) {
+  if (order == SortOrder(-1)) // ok, not a constant expression context
+    return;
+}
 }


        


More information about the cfe-commits mailing list