Skip to content

Commit 92cfc6e

Browse files
committed
[Modules] Fix ModuleDeclState transition when module is used as a regular identifier
`ModuleDeclState` is incorrectly changed to `NamedModuleImplementation` for `struct module {}; void foo(module a);`. This is mostly benign but leads to a spurious warning after llvm#69555. A real world example is: ``` // pybind11.h class module_ { ... }; using module = module_; // tensorflow void DefineMetricsModule(pybind11::module main_module); // `module main_module);` incorrectly changes `ModuleDeclState` to `NamedModuleImplementation` ```
1 parent 94202e7 commit 92cfc6e

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

clang/lib/Lex/Preprocessor.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -957,26 +957,27 @@ void Preprocessor::Lex(Token &Result) {
957957
ModuleDeclState.handlePeriod();
958958
break;
959959
case tok::identifier:
960-
if (Result.getIdentifierInfo()->isModulesImport()) {
961-
TrackGMFState.handleImport(StdCXXImportSeqState.afterTopLevelSeq());
962-
StdCXXImportSeqState.handleImport();
963-
if (StdCXXImportSeqState.afterImportSeq()) {
964-
ModuleImportLoc = Result.getLocation();
965-
NamedModuleImportPath.clear();
966-
IsAtImport = false;
967-
ModuleImportExpectsIdentifier = true;
968-
CurLexerKind = CLK_LexAfterModuleImport;
969-
}
970-
break;
971-
} else if (Result.getIdentifierInfo() == getIdentifierInfo("module")) {
972-
TrackGMFState.handleModule(StdCXXImportSeqState.afterTopLevelSeq());
973-
ModuleDeclState.handleModule();
974-
break;
975-
} else {
976-
ModuleDeclState.handleIdentifier(Result.getIdentifierInfo());
977-
if (ModuleDeclState.isModuleCandidate())
960+
if (StdCXXImportSeqState.atTopLevel()) {
961+
if (Result.getIdentifierInfo()->isModulesImport()) {
962+
TrackGMFState.handleImport(StdCXXImportSeqState.afterTopLevelSeq());
963+
StdCXXImportSeqState.handleImport();
964+
if (StdCXXImportSeqState.afterImportSeq()) {
965+
ModuleImportLoc = Result.getLocation();
966+
NamedModuleImportPath.clear();
967+
IsAtImport = false;
968+
ModuleImportExpectsIdentifier = true;
969+
CurLexerKind = CLK_LexAfterModuleImport;
970+
}
978971
break;
972+
} else if (Result.getIdentifierInfo() == getIdentifierInfo("module")) {
973+
TrackGMFState.handleModule(StdCXXImportSeqState.afterTopLevelSeq());
974+
ModuleDeclState.handleModule();
975+
break;
976+
}
979977
}
978+
ModuleDeclState.handleIdentifier(Result.getIdentifierInfo());
979+
if (ModuleDeclState.isModuleCandidate())
980+
break;
980981
[[fallthrough]];
981982
default:
982983
TrackGMFState.handleMisc();

clang/test/Preprocessor/include-in-module-purview.cppm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// RUN: %clang_cc1 -std=c++20 %t/a.cppm -E -P -I%t -o %t/tmp 2>&1 | FileCheck %t/a.cppm
66
// RUN: %clang_cc1 -std=c++20 %t/a.cppm -E -P -I%t -o - 2>&1 \
77
// RUN: -Wno-include-angled-in-module-purview | FileCheck %t/a.cppm --check-prefix=CHECK-NO-WARN
8+
// 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
89

910
//--- a.h
1011
// left empty
@@ -58,3 +59,10 @@ module :private;
5859
// CHECK: 10 warnings generated.
5960

6061
// CHECK-NO-WARN-NOT: warning
62+
63+
//--- b.cpp
64+
/// Don't recognize `module m);` as a module purview or report a spurious
65+
/// warning for <stddef.h>.
66+
struct module {};
67+
void foo(module m);
68+
#include <stddef.h>

0 commit comments

Comments
 (0)