[clang] [Clang] use constant evaluation context for constexpr if conditions (PR #123667)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 27 04:15:51 PST 2025


================
@@ -2203,8 +2203,18 @@ Parser::ParseCXXCondition(StmtResult *InitStmt, SourceLocation Loc,
       return ParseCXXCondition(nullptr, Loc, CK, MissingOK);
     }
 
-    // Parse the expression.
-    ExprResult Expr = ParseExpression(); // expression
+    ExprResult Expr;
+    {
+      EnterExpressionEvaluationContext Eval(
+          Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated,
+          /*LambdaContextDecl=*/nullptr,
+          /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_Other,
+          /*ShouldEnter=*/CK == Sema::ConditionKind::ConstexprIf);
+
+      // Parse the expression.
+      Expr = ParseExpression(); // expression
+    }
----------------
zyn0217 wrote:

```suggestion
    ExprResult Expr = [&] {
      EnterExpressionEvaluationContext Eval(
          Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated,
          /*LambdaContextDecl=*/nullptr,
          /*ExprContext=*/Sema::ExpressionEvaluationContextRecord::EK_Other,
          /*ShouldEnter=*/CK == Sema::ConditionKind::ConstexprIf);

      // Parse the expression.
      return ParseExpression(); // expression
    }();
```

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


More information about the cfe-commits mailing list