[llvm-branch-commits] [clang] [Clang] [C++26] Expansion Statements (Part 2: Parsing and Parser Tests) (PR #169681)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Apr 1 07:28:57 PDT 2026


================
@@ -261,6 +261,36 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
   }
 
   case tok::kw_template: {
+    if (NextToken().is(tok::kw_for)) {
+      // Expansion statements are not backported for now.
+      if (!getLangOpts().CPlusPlus26) {
+        Diag(Tok.getLocation(), diag::err_expansion_stmt_requires_cxx2c);
+
+        // Trying to parse this as a regular 'for' statement instead yields
+        // better error recovery.
+        ConsumeToken();
+        return ParseForStatement(TrailingElseLoc, PrecedingLabel);
+      }
+
+      SourceLocation TemplateLoc = ConsumeToken();
+      return ParseExpansionStatement(TrailingElseLoc, PrecedingLabel,
+                                     TemplateLoc);
+    }
+
+    // Since 'template for' is a thing, users might reasonably try to write
+    // 'template while' or something like that; diagnose that here for better
+    // QOI (and basically just ignore the 'template' token for error recovery).
+    if (NextToken().isOneOf(tok::kw_while, tok::kw_do)) {
+      bool IsWhile = NextToken().is(tok::kw_while);
+      Diag(Tok.getLocation(), diag::err_expansion_stmt_invalid_kw)
+          << (IsWhile ? "while" : "do");
----------------
Sirraide wrote:

Ok, done, this section is now:
```c++
    if (NextToken().isOneOf(tok::kw_while, tok::kw_do)) {
      Diag(Tok.getLocation(), diag::err_expansion_stmt_invalid_kw)
          << NextToken().getKind();

      ConsumeToken();
      if (Tok.is(tok::kw_while))
        return ParseWhileStatement(TrailingElseLoc, PrecedingLabel);
      return ParseDoStatement(PrecedingLabel);
    }
```
(not pushing this for now because I don’t want github to send out another 11 emails just for this change...)

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


More information about the llvm-branch-commits mailing list