[clang] [Clang][C2y] Add support for if declarations (N3356 paper) (PR #198244)

Muhammad Bassiouni via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 8 04:54:17 PDT 2026


================
@@ -1880,16 +1880,70 @@ Sema::ConditionResult Parser::ParseCXXCondition(StmtResult *InitStmt,
     return Sema::ConditionError();
   }
 
+  if (!getLangOpts().CPlusPlus && Tok.is(tok::kw___extension__)) {
+    // In C, the first clause of a condition may be a declaration used as an
+    // init-statement (C2y), and that declaration may be prefixed by one or more
+    // __extension__ markers. Consume them up front -- mirroring block-statement
+    // parsing -- so the disambiguation below sees the real start of the
+    // declaration. The markers also silence extension diagnostics for the rest
+    // of the condition, including the diagnostic for the init-statement
+    // extension itself.
+    std::optional<ExtensionRAIIObject> ExtensionGuard;
+    ExtensionGuard.emplace(Diags);
+    while (TryConsumeToken(tok::kw___extension__))
+      ;
+  }
+
   ParsedAttributes attrs(AttrFactory);
-  MaybeParseCXX11Attributes(attrs);
+  bool ParsedAttrs = MaybeParseCXX11Attributes(attrs);
+  if (!getLangOpts().CPlusPlus)
----------------
bassiounix wrote:

> Why is this only necessary for C?

TL;DR because of the special case in C2y, C++ handles it properly. `(__attribute__((...)); expr)` specifically.

Part of the reason why it's C2y only is this https://github.com/llvm/llvm-project/pull/198244#discussion_r3543665833

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


More information about the cfe-commits mailing list