https://github.com/bassiounix created 
https://github.com/llvm/llvm-project/pull/244055

Fixes #243727.

`isCXXDeclarationSpecifier`'s `::` case annotates the token and then recurses to
handle whatever it got, which only makes progress when the token can actually be
annotated as a scope specifier. Outside C++ it cannot be, so in C the recursion
never advanced and the stack overflowed:

```c
void f() { if (::0); }   // clang 22: error: expected expression  /  trunk: 
crash
```

Since 226b0b59bfca (#198244) C parses conditions through `ParseCondition`, so C
conditions now reach this code. `Parser::isDeclarationSpecifier` already guards
the same case; this adds the matching guard to its C++ twin.

Objective-C is unchanged: it keeps diagnosing `::` through
`TryAnnotateTypeOrScopeToken`, and the new test pins that. Output is now
byte-identical to pre-regression clang 22 for the reduced repro in C89-C2y,
OpenCL C and Objective-C; C++ and Objective-C++ are untouched.

`if (__super)` in C with `-fms-extensions` still crashes through the same loop 
in
`ParseCastExpression`. That predates #198244 (clang 22 crashes on it too) and is
not specific to conditions, so it is left out of this patch.

Test plan:
- New test `clang/test/Parser/gh243727.c` fails on trunk (stack overflow) and
  passes with the fix; it covers `if`/`while`/`do-while`/`switch`/both `for`
  clauses in C and Objective-C.
- Ran the C, Parser, SemaCXX, Sema, SemaObjC, SemaObjCXX, Misc, FixIt,
  SemaOpenCL and ParserOpenACC lit suites on a debug build: 4621 tests, only the
  5 failures that already fail at HEAD without the change
  (`Misc/warning-flags{,-tree,-enabled}`, `Misc/warning-wall`,
  `Parser/skip-function-bodies.mm`).
- Existing `Parser/objc-coloncolon.m` passes.

CC @AaronBallman @Sirraide @shafik @hongtaihu


>From 805b58ce62bf2ac582c15c8672d2839c018ce42d Mon Sep 17 00:00:00 2001
From: bassiounix <[email protected]>
Date: Thu, 19 Nov 2026 01:57:32 +0200
Subject: [PATCH] [Clang][Parse] Do not treat '::' as a decl-specifier outside
 C++

isCXXDeclarationSpecifier's '::' case annotates the token and then recurses to
handle whatever it got, which only makes progress when the token can actually be
annotated as a scope specifier. Outside C++ it cannot be, so in C the recursion
never advanced and the stack overflowed:

  void f() { if (::0); }

Since 226b0b59bfca ([Clang][C2y] Add support for if declarations (N3356
paper)) C parses conditions through ParseCondition, so C conditions now reach
this code. Parser::isDeclarationSpecifier already guards the same case; this
adds the matching guard to its C++ twin. Objective-C is unaffected: it keeps
diagnosing '::' through TryAnnotateTypeOrScopeToken.

Fixes #243727.
---
 clang/lib/Parse/ParseTentative.cpp |  5 ++++
 clang/test/Parser/gh243727.c       | 40 ++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)
 create mode 100644 clang/test/Parser/gh243727.c

diff --git a/clang/lib/Parse/ParseTentative.cpp 
b/clang/lib/Parse/ParseTentative.cpp
index a4581538fbcec1..1ffb5305fcf237 100644
--- a/clang/lib/Parse/ParseTentative.cpp
+++ b/clang/lib/Parse/ParseTentative.cpp
@@ -1201,6 +1201,11 @@ 
Parser::isCXXDeclarationSpecifier(ImplicitTypenameContext AllowImplicitTypename,
   }
 
   case tok::coloncolon: {    // ::foo::bar
+    // '::' cannot start a declaration outside C++, and outside C++ it cannot 
be
+    // annotated as a scope specifier either, so recursing below would never
+    // advance.
+    if (!getLangOpts().CPlusPlus)
+      return TPResult::False;
     const Token &Next = NextToken();
     if (Next.isOneOf(tok::kw_new,       // ::new
                      tok::kw_delete))   // ::delete
diff --git a/clang/test/Parser/gh243727.c b/clang/test/Parser/gh243727.c
new file mode 100644
index 00000000000000..4a20847652e23f
--- /dev/null
+++ b/clang/test/Parser/gh243727.c
@@ -0,0 +1,40 @@
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,c %s
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,c -std=c2y %s
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,objc -x objective-c %s
+
+// Outside C++, '::' cannot start a declaration, so the tentative-parse
+// disambiguation must report it as such and parse the condition as an
+// expression. Recursing on the same token never makes progress, which used to
+// overflow the stack in C. In Objective-C it produced a cascade of bogus
+// declaration diagnostics instead of a single 'expected a type'.
+// https://github.com/llvm/llvm-project/issues/243727
+
+void reduced(void) { if (::0); }
+// c-error@-1 {{expected expression}}
+// objc-error@-2 {{expected a type}}
+// expected-warning@-3 {{if statement has empty body}}
+// expected-note@-4 {{put the semicolon on a separate line to silence this 
warning}}
+
+void if_condition(void) { if (::0) {} }
+// c-error@-1 {{expected expression}}
+// objc-error@-2 {{expected a type}}
+
+void while_condition(void) { while (::0) {} }
+// c-error@-1 {{expected expression}}
+// objc-error@-2 {{expected a type}}
+
+void do_while_condition(void) { do {} while (::0); }
+// c-error@-1 {{expected expression}}
+// objc-error@-2 {{expected a type}}
+
+void switch_condition(void) { switch (::0) {} }
+// c-error@-1 {{expected expression}}
+// objc-error@-2 {{expected a type}}
+
+void for_init_clause(void) { for (::0;;) {} }
+// c-error@-1 {{expected expression}}
+// objc-error@-2 {{expected a type}}
+
+void for_condition_clause(void) { for (; ::0;) {} }
+// c-error@-1 {{expected expression}}
+// objc-error@-2 {{expected a type}}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to