[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
Tue Mar 31 08:57:13 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:

Ah, I don’t think I’ve ever printed a token in a diagnostic before; I’ll try that

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


More information about the llvm-branch-commits mailing list