Skip to content

[Clang] fix parser recovery for invalid static_assert string messages - #187859

Merged
a-tarasyuk merged 7 commits into
llvm:mainfrom
a-tarasyuk:fix/187690
Apr 27, 2026
Merged

[Clang] fix parser recovery for invalid static_assert string messages#187859
a-tarasyuk merged 7 commits into
llvm:mainfrom
a-tarasyuk:fix/187690

Conversation

@a-tarasyuk

Copy link
Copy Markdown
Member

Fixes #187690


This PR fixes parser recovery for invalid static_assert declarations with string literal messages. The parser now stops the message lookahead on ; and eof, so invalid inputs are diagnosed as parse errors.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 21, 2026
@llvmbot

llvmbot commented Mar 21, 2026

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-clang

Author: Oleksandr Tarasiuk (a-tarasyuk)

Changes

Fixes #187690


This PR fixes parser recovery for invalid static_assert declarations with string literal messages. The parser now stops the message lookahead on ; and eof, so invalid inputs are diagnosed as parse errors.


Full diff: https://github.com/llvm/llvm-project/pull/187859.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Parse/ParseDeclCXX.cpp (+1-1)
  • (modified) clang/test/Parser/static_assert.cpp (+7-5)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 45234c316eba8..fb674faa42006 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -339,6 +339,7 @@ Bug Fixes in This Version
 - Fixed a crash when normalizing constraints involving concept template parameters whose index coincided with non-concept template parameters in the same parameter mapping.
 - Fixed a crash caused by accessing dependent diagnostics of a non-dependent context.
 - Fixed a crash when substituting into a non-type template parameter that has a type containing an undeduced placeholder type.
+- Fixed a crash when parsing invalid ``static_assert`` declarations with string-literal messages (#GH187690).
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 274c354d59808..e3bb52647176f 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -980,7 +980,7 @@ Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd) {
     if (getLangOpts().CPlusPlus11) {
       for (unsigned I = 0;; ++I) {
         const Token &T = GetLookAheadToken(I);
-        if (T.is(tok::r_paren))
+        if (T.isOneOf(tok::r_paren, tok::semi, tok::eof))
           break;
         if (!tokenIsLikeStringLiteral(T, getLangOpts()) || T.hasUDSuffix()) {
           ParseAsExpression = true;
diff --git a/clang/test/Parser/static_assert.cpp b/clang/test/Parser/static_assert.cpp
index 4fe7d3cda7b21..52ef8a93cea34 100644
--- a/clang/test/Parser/static_assert.cpp
+++ b/clang/test/Parser/static_assert.cpp
@@ -1,6 +1,8 @@
-// RUN: %clang_cc1 -fsyntax-only -triple=x86_64-linux -std=c++2a -verify=cxx2a %s
-// RUN: %clang_cc1 -fsyntax-only -triple=x86_64-linux -std=c++2c -verify=cxx2c %s
+// RUN: %clang_cc1 -fsyntax-only -triple=x86_64-linux -verify %s
 
-static_assert(true, "" // cxx2a-warning {{'static_assert' with a user-generated message is a C++26 extension}} \
-                       // cxx2a-note {{to match this '('}} cxx2c-note {{to match this '('}}
-                       // cxx2a-error {{expected ')'}}     cxx2c-error {{expected ')'}}
+// expected-error@+1 {{unexpected ';' before ')'}}
+static_assert(true, "";);
+
+// expected-error@+2 {{expected ')'}}
+// expected-note@+1 {{to match this '('}}
+static_assert(true, ""

Comment thread clang/lib/Parse/ParseDeclCXX.cpp Outdated
@a-tarasyuk
a-tarasyuk requested a review from AaronBallman March 31, 2026 07:33
@shafik

shafik commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

bisect points to this commit: #102044 it may help you formulate a better fix.

@a-tarasyuk a-tarasyuk closed this Apr 24, 2026
@cor3ntin

Copy link
Copy Markdown
Contributor

@a-tarasyuk Can I ask why you close this PR?

@a-tarasyuk a-tarasyuk reopened this Apr 26, 2026
@a-tarasyuk

Copy link
Copy Markdown
Member Author

@cor3ntin, after the latest discussion, I got the impression that the goal was to fully rethink recovery handling, not just for static_assert.

@cor3ntin

Copy link
Copy Markdown
Contributor

@a-tarasyuk while we might want to think more about BalancedDelimiterTracker deal with semi-colon, the fix here is correct (and it's not clear we can do much better than what you do here)

So i think we should land this.

@AaronBallman AaronBallman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@a-tarasyuk
a-tarasyuk merged commit 5ee1495 into llvm:main Apr 27, 2026
11 checks passed
yingopq pushed a commit to yingopq/llvm-project that referenced this pull request Apr 29, 2026
…llvm#187859)

Fixes llvm#187690

--- 

This PR fixes parser recovery for invalid `static_assert` declarations
with string literal messages. The parser now stops the message lookahead
on `;` and `eof`, so invalid inputs are diagnosed as parse errors.
KHicketts pushed a commit to KHicketts/llvm-project that referenced this pull request Apr 30, 2026
…llvm#187859)

Fixes llvm#187690

--- 

This PR fixes parser recovery for invalid `static_assert` declarations
with string literal messages. The parser now stops the message lookahead
on `;` and `eof`, so invalid inputs are diagnosed as parse errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang] unexpected ';' before ')' in static_assert triggers a crash

6 participants