[PATCH] D133248: [clang] Fix crash upon stray coloncolon token in C2x mode
Hu Jialun via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 2 21:07:23 PDT 2022
SuibianP created this revision.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
SuibianP requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
The parser assumes that the lexer never emits coloncolon token for
C code, but this assumption no longer holds in C2x attribute namespaces.
As a result, stray coloncolon tokens out of attributes cause assertion
failures and hangs in release build, which this patch tries to handle.
Crash input minimal example: T n::v
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D133248
Files:
clang/include/clang/Parse/Parser.h
clang/lib/Parse/ParseDecl.cpp
clang/lib/Parse/Parser.cpp
clang/test/Parser/c2x-attributes.c
Index: clang/test/Parser/c2x-attributes.c
===================================================================
--- clang/test/Parser/c2x-attributes.c
+++ clang/test/Parser/c2x-attributes.c
@@ -141,3 +141,6 @@
struct [[]] S4 *s; // expected-error {{an attribute list cannot appear here}}
struct S5 {};
int c = sizeof(struct [[]] S5); // expected-error {{an attribute list cannot appear here}}
+
+// Ensure that '::' outside of attributes does not crash and is not treated as scope
+double n::v; // expected-error {{expected ';' after top level declarator}}
Index: clang/lib/Parse/Parser.cpp
===================================================================
--- clang/lib/Parse/Parser.cpp
+++ clang/lib/Parse/Parser.cpp
@@ -2060,9 +2060,9 @@
}
if (!getLangOpts().CPlusPlus) {
- // If we're in C, we can't have :: tokens at all (the lexer won't return
- // them). If the identifier is not a type, then it can't be scope either,
- // just early exit.
+ // If we're in C, the only place we can have :: tokens is C2x
+ // attribute which is parsed elsewhere. If the identifier is not a type,
+ // then it can't be scope either, just early exit.
return false;
}
Index: clang/lib/Parse/ParseDecl.cpp
===================================================================
--- clang/lib/Parse/ParseDecl.cpp
+++ clang/lib/Parse/ParseDecl.cpp
@@ -5336,6 +5336,8 @@
return isDeclarationSpecifier();
case tok::coloncolon: // ::foo::bar
+ if (!getLangOpts().CPlusPlus)
+ return false;
if (NextToken().is(tok::kw_new) || // ::new
NextToken().is(tok::kw_delete)) // ::delete
return false;
Index: clang/include/clang/Parse/Parser.h
===================================================================
--- clang/include/clang/Parse/Parser.h
+++ clang/include/clang/Parse/Parser.h
@@ -860,10 +860,11 @@
bool TryAnnotateCXXScopeToken(bool EnteringContext = false);
bool MightBeCXXScopeToken() {
- return Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
- (Tok.is(tok::annot_template_id) &&
- NextToken().is(tok::coloncolon)) ||
- Tok.is(tok::kw_decltype) || Tok.is(tok::kw___super);
+ return (getLangOpts().CPlusPlus) &&
+ (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
+ (Tok.is(tok::annot_template_id) &&
+ NextToken().is(tok::coloncolon)) ||
+ Tok.is(tok::kw_decltype) || Tok.is(tok::kw___super));
}
bool TryAnnotateOptionalCXXScopeToken(bool EnteringContext = false) {
return MightBeCXXScopeToken() && TryAnnotateCXXScopeToken(EnteringContext);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133248.457761.patch
Type: text/x-patch
Size: 2644 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220903/6e062d90/attachment.bin>
More information about the cfe-commits
mailing list