[Clang] Tighten restrictions on enum out of range diagnostic
authorShafik Yaghmour <shafik.yaghmour@intel.com>
Thu, 11 Aug 2022 20:42:40 +0000 (13:42 -0700)
committerShafik Yaghmour <shafik.yaghmour@intel.com>
Thu, 11 Aug 2022 20:44:26 +0000 (13:44 -0700)
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

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

index 992bc9a..67a1fa4 100644 (file)
@@ -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();
index 23dfde8..c4f64ff 100644 (file)
@@ -2455,4 +2455,18 @@ void testValueInRangeOfEnumerationValues() {
   constexpr EMaxInt x20 = static_cast<EMaxInt>((long)__INT_MAX__+1);
   // expected-error@-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;
+}
 }