[clang] [Clang] Fix a crash when parsing an invalid `decltype` (PR #148798)
Corentin Jabot via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 15 04:58:54 PDT 2025
https://github.com/cor3ntin updated https://github.com/llvm/llvm-project/pull/148798
>From c80347a825264474a51ee6e638434d3454a0a7e3 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 15 Jul 2025 09:38:59 +0200
Subject: [PATCH 1/2] [Clang] Fix a crash when parsing an invalid `decltype`
We would try to exact an annotated token before checking
if it was valid, leading to a crash when `decltype` was the only
token that was parsed (which can happen in the absense of opening
paren)
Fixes #114815
---
clang/lib/Parse/ParseDeclCXX.cpp | 6 +++---
clang/test/Parser/gh114815.cpp | 6 ++++++
2 files changed, 9 insertions(+), 3 deletions(-)
create mode 100644 clang/test/Parser/gh114815.cpp
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 59e6e0af4b5b0..5bdc13d75a9e1 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -1132,14 +1132,14 @@ void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec &DS,
SourceLocation EndLoc) {
// make sure we have a token we can turn into an annotation token
if (PP.isBacktrackEnabled()) {
- PP.RevertCachedTokens(1);
- if (DS.getTypeSpecType() == TST_error) {
+ if (DS.getTypeSpecType() == TST_error)
// We encountered an error in parsing 'decltype(...)' so lets annotate all
// the tokens in the backtracking cache - that we likely had to skip over
// to get to a token that allows us to resume parsing, such as a
// semi-colon.
EndLoc = PP.getLastCachedTokenLocation();
- }
+ else
+ PP.RevertCachedTokens(1);
} else
PP.EnterToken(Tok, /*IsReinject*/ true);
diff --git a/clang/test/Parser/gh114815.cpp b/clang/test/Parser/gh114815.cpp
new file mode 100644
index 0000000000000..6a89384e9e66d
--- /dev/null
+++ b/clang/test/Parser/gh114815.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -verify %s -std=c++11 -fsyntax-only
+
+#define ID(X) X
+extern int ID(decltype);
+// expected-error at -1 {{expected '(' after 'decltype'}} \
+// expected-error at -1 {{expected unqualified-id}}
>From b8b2f2598d560f5777398eed2ddd451f0b279019 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 15 Jul 2025 13:57:52 +0200
Subject: [PATCH 2/2] turns out, we don't need this code at all
---
clang/lib/Parse/ParseDeclCXX.cpp | 9 +--------
1 file changed, 1 insertion(+), 8 deletions(-)
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 5bdc13d75a9e1..adcdba6f8b3aa 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -1132,14 +1132,7 @@ void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec &DS,
SourceLocation EndLoc) {
// make sure we have a token we can turn into an annotation token
if (PP.isBacktrackEnabled()) {
- if (DS.getTypeSpecType() == TST_error)
- // We encountered an error in parsing 'decltype(...)' so lets annotate all
- // the tokens in the backtracking cache - that we likely had to skip over
- // to get to a token that allows us to resume parsing, such as a
- // semi-colon.
- EndLoc = PP.getLastCachedTokenLocation();
- else
- PP.RevertCachedTokens(1);
+ PP.RevertCachedTokens(1);
} else
PP.EnterToken(Tok, /*IsReinject*/ true);
More information about the cfe-commits
mailing list