[clang] [Clang] Treat the body of an expansion statement with expansion size 0 as discarded (PR #212319)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 27 11:49:42 PDT 2026


================
@@ -2371,6 +2371,20 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc,
 
   MisleadingIndentationChecker MIChecker(*this, MSK_for, ForLoc);
 
+  // If this is an expansion statement with a known expansion size of 0, parse
+  // the body as a discarded statement.
+  bool ShouldEnterDiscardedContext = false;
+  if (ESD && ForRangeStmt.isUsable()) {
+    auto *Pattern = ForRangeStmt.getAs<CXXExpansionStmtPattern>();
+    ShouldEnterDiscardedContext = Pattern->getExpansionSize() == 0u;
----------------
Sirraide wrote:

`if constexpr` behaves the same way:

```c++
bool ShouldEnter = ConstexprCondition && !*ConstexprCondition;
Sema::ExpressionEvaluationContext Context =
    Sema::ExpressionEvaluationContext::DiscardedStatement;
if (NotLocation.isInvalid() && IsConsteval) {
  Context = Sema::ExpressionEvaluationContext::ImmediateFunctionContext;
  ShouldEnter = true;
}

EnterExpressionEvaluationContext PotentiallyDiscarded(
    Actions, Context, nullptr,
    Sema::ExpressionEvaluationContextRecord::EK_Other, ShouldEnter);
```
i.e. `ShouldEnter` is `false` if `ConstexprCondition` is `std::nullopt`.

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


More information about the cfe-commits mailing list