[clang] [clang-tools-extra] [Clang] Add support for the C `defer` TS (PR #162848)

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 10 07:33:30 PDT 2025


================
@@ -2376,6 +2381,33 @@ StmtResult Parser::ParseReturnStatement() {
   return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
 }
 
+StmtResult Parser::ParseDeferStatement(SourceLocation *TrailingElseLoc) {
+  assert(Tok.is(tok::kw_defer));
+  SourceLocation DeferLoc = ConsumeToken();
+  Actions.ActOnStartOfDeferStmt(DeferLoc, getCurScope());
+  auto OnError = llvm::make_scope_exit(
+      [&] { Actions.ActOnDeferStmtError(getCurScope()); });
+
+  StmtResult Res = ParseStatement(TrailingElseLoc);
+  if (!Res.isUsable())
+    return StmtError();
+
+  // Diagnose this *after* parsing the body for better synchronisation.
+  if (getLangOpts().CPlusPlus) {
+    Diag(DeferLoc, diag::err_defer_unsupported);
+    return StmtError();
+  }
----------------
cor3ntin wrote:

Maybe the alternative is to not parse the driver option at all in c++ mode
(`ShouldParseIf<!strconcat("!", cplusplus.KeyPath)>`)

And in the front end we can just assert on it

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


More information about the cfe-commits mailing list