Skip to content

[Modules] Fix ModuleDeclState transition when module is used as a regular identifier #71134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions clang/lib/Lex/Preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,26 +957,29 @@ void Preprocessor::Lex(Token &Result) {
ModuleDeclState.handlePeriod();
break;
case tok::identifier:
if (Result.getIdentifierInfo()->isModulesImport()) {
TrackGMFState.handleImport(StdCXXImportSeqState.afterTopLevelSeq());
StdCXXImportSeqState.handleImport();
if (StdCXXImportSeqState.afterImportSeq()) {
ModuleImportLoc = Result.getLocation();
NamedModuleImportPath.clear();
IsAtImport = false;
ModuleImportExpectsIdentifier = true;
CurLexerKind = CLK_LexAfterModuleImport;
}
break;
} else if (Result.getIdentifierInfo() == getIdentifierInfo("module")) {
TrackGMFState.handleModule(StdCXXImportSeqState.afterTopLevelSeq());
ModuleDeclState.handleModule();
break;
} else {
ModuleDeclState.handleIdentifier(Result.getIdentifierInfo());
if (ModuleDeclState.isModuleCandidate())
// Check "import" and "module" when there is no open bracket. The two
// identifiers are not meaningful with open brackets.
if (StdCXXImportSeqState.atTopLevel()) {
Copy link
Member

Choose a reason for hiding this comment

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

I spent some time to understand the rationale here. How about add a comment like: "Otherwise we're in the unclosed brackets, then the modules related identifiers are not meaningful."

if (Result.getIdentifierInfo()->isModulesImport()) {
TrackGMFState.handleImport(StdCXXImportSeqState.afterTopLevelSeq());
StdCXXImportSeqState.handleImport();
if (StdCXXImportSeqState.afterImportSeq()) {
ModuleImportLoc = Result.getLocation();
NamedModuleImportPath.clear();
IsAtImport = false;
ModuleImportExpectsIdentifier = true;
CurLexerKind = CLK_LexAfterModuleImport;
}
break;
} else if (Result.getIdentifierInfo() == getIdentifierInfo("module")) {
TrackGMFState.handleModule(StdCXXImportSeqState.afterTopLevelSeq());
ModuleDeclState.handleModule();
break;
}
}
ModuleDeclState.handleIdentifier(Result.getIdentifierInfo());
if (ModuleDeclState.isModuleCandidate())
break;
[[fallthrough]];
default:
TrackGMFState.handleMisc();
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Preprocessor/include-in-module-purview.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// RUN: %clang_cc1 -std=c++20 %t/a.cppm -E -P -I%t -o %t/tmp 2>&1 | FileCheck %t/a.cppm
// RUN: %clang_cc1 -std=c++20 %t/a.cppm -E -P -I%t -o - 2>&1 \
// RUN: -Wno-include-angled-in-module-purview | FileCheck %t/a.cppm --check-prefix=CHECK-NO-WARN
// RUN: %clang_cc1 -std=c++20 %t/b.cpp -E -P -I%t -o - 2>&1 | FileCheck %t/a.cppm --check-prefix=CHECK-NO-WARN

//--- a.h
// left empty
Expand Down Expand Up @@ -58,3 +59,10 @@ module :private;
// CHECK: 10 warnings generated.

// CHECK-NO-WARN-NOT: warning

//--- b.cpp
/// Don't recognize `module m);` as a module purview or report a spurious
/// warning for <stddef.h>.
struct module {};
void foo(module m);
#include <stddef.h>