Skip to content

Commit 071f3b5

Browse files
authored
[Modules] Fix ModuleDeclState transition when module is used as a regular identifier (#71134)
`ModuleDeclState` is incorrectly changed to `NamedModuleImplementation` for `struct module {}; void foo(module a);`. This is mostly benign but leads to a spurious warning after #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` #include <algorithm> // spurious warning ```
1 parent 7443af1 commit 071f3b5

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

clang/lib/Lex/Preprocessor.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -957,26 +957,29 @@ 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+
// Check "import" and "module" when there is no open bracket. The two
961+
// identifiers are not meaningful with open brackets.
962+
if (StdCXXImportSeqState.atTopLevel()) {
963+
if (Result.getIdentifierInfo()->isModulesImport()) {
964+
TrackGMFState.handleImport(StdCXXImportSeqState.afterTopLevelSeq());
965+
StdCXXImportSeqState.handleImport();
966+
if (StdCXXImportSeqState.afterImportSeq()) {
967+
ModuleImportLoc = Result.getLocation();
968+
NamedModuleImportPath.clear();
969+
IsAtImport = false;
970+
ModuleImportExpectsIdentifier = true;
971+
CurLexerKind = CLK_LexAfterModuleImport;
972+
}
978973
break;
974+
} else if (Result.getIdentifierInfo() == getIdentifierInfo("module")) {
975+
TrackGMFState.handleModule(StdCXXImportSeqState.afterTopLevelSeq());
976+
ModuleDeclState.handleModule();
977+
break;
978+
}
979979
}
980+
ModuleDeclState.handleIdentifier(Result.getIdentifierInfo());
981+
if (ModuleDeclState.isModuleCandidate())
982+
break;
980983
[[fallthrough]];
981984
default:
982985
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)