Skip to content

Commit d42fe78

Browse files
committed
Handle c++ operator keyword after module/import
Signed-off-by: yronglin <[email protected]>
1 parent d7e5043 commit d42fe78

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

clang/lib/Lex/Preprocessor.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,12 +1199,22 @@ bool Preprocessor::HandleModuleContextualKeyword(
11991199
llvm::SaveAndRestore<bool> SavedParsingPreprocessorDirective(
12001200
CurPPLexer->ParsingPreprocessorDirective, true);
12011201

1202+
// The next token may be an angled string literal after import keyword.
1203+
llvm::SaveAndRestore<bool> SavedParsingFilemame(
1204+
CurPPLexer->ParsingFilename,
1205+
Result.getIdentifierInfo()->isModulesImport());
1206+
1207+
std::optional<Token> NextTok =
1208+
CurLexer ? CurLexer->peekNextPPToken() : CurTokenLexer->peekNextPPToken();
1209+
if (!NextTok)
1210+
return false;
1211+
1212+
if (NextTok->is(tok::raw_identifier))
1213+
LookUpIdentifierInfo(*NextTok);
1214+
12021215
if (Result.getIdentifierInfo()->isModulesImport()) {
1203-
// The next token may be an angled string literal.
1204-
llvm::SaveAndRestore<bool> SavedParsingFilemame(CurPPLexer->ParsingFilename,
1205-
true);
1206-
if (isNextPPTokenOneOf(tok::raw_identifier, tok::less, tok::string_literal,
1207-
tok::colon, tok::header_name)) {
1216+
if (NextTok->isOneOf(tok::identifier, tok::less, tok::string_literal,
1217+
tok::colon, tok::header_name)) {
12081218
Result.setKind(tok::kw_import);
12091219
ModuleImportLoc = Result.getLocation();
12101220
IsAtImport = false;
@@ -1213,7 +1223,7 @@ bool Preprocessor::HandleModuleContextualKeyword(
12131223
}
12141224

12151225
if (Result.getIdentifierInfo()->isModulesDeclaration() &&
1216-
isNextPPTokenOneOf(tok::raw_identifier, tok::colon, tok::semi)) {
1226+
NextTok->isOneOf(tok::identifier, tok::colon, tok::semi)) {
12171227
Result.setKind(tok::kw_module);
12181228
ModuleDeclLoc = Result.getLocation();
12191229
return true;

clang/test/CXX/module/cpp.pre/p1.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
// RUN: %clang_cc1 -std=c++20 %t/not_import.cpp -fsyntax-only -verify
2323
// RUN: %clang_cc1 -std=c++20 %t/import_spaceship.cpp -fsyntax-only -verify
2424
// RUN: %clang_cc1 -std=c++20 %t/leading_empty_macro.cpp -fsyntax-only -verify
25+
// RUN: %clang_cc1 -std=c++20 %t/operator_keyword_and.cpp -fsyntax-only -verify
26+
// RUN: %clang_cc1 -std=c++20 %t/operator_keyword_and2.cpp -fsyntax-only -verify
2527

2628

2729
//--- hash.cpp
@@ -88,3 +90,15 @@ export module M;
8890
typedef int import;
8991
#define EMP
9092
EMP import m; // The phase 7 grammar should see import as a typedef-name.
93+
94+
//--- operator_keyword_and.cpp
95+
// expected-no-diagnostics
96+
typedef int import;
97+
extern
98+
import and x;
99+
100+
//--- operator_keyword_and2.cpp
101+
// expected-no-diagnostics
102+
typedef int module;
103+
extern
104+
module and x;

0 commit comments

Comments
 (0)