-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
…ular 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` ```
@llvm/pr-subscribers-clang Author: Fangrui Song (MaskRay) Changes
A real world example is:
Full diff: https://github.com/llvm/llvm-project/pull/71134.diff 2 Files Affected:
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp
index ede4c51487ffbe7..482d73f87df07b7 100644
--- a/clang/lib/Lex/Preprocessor.cpp
+++ b/clang/lib/Lex/Preprocessor.cpp
@@ -957,26 +957,27 @@ 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())
+ if (StdCXXImportSeqState.atTopLevel()) {
+ 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();
diff --git a/clang/test/Preprocessor/include-in-module-purview.cppm b/clang/test/Preprocessor/include-in-module-purview.cppm
index 0a080112b43277c..4a7883653ac45dd 100644
--- a/clang/test/Preprocessor/include-in-module-purview.cppm
+++ b/clang/test/Preprocessor/include-in-module-purview.cppm
@@ -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
@@ -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>
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into this. LGTM.
} else { | ||
ModuleDeclState.handleIdentifier(Result.getIdentifierInfo()); | ||
if (ModuleDeclState.isModuleCandidate()) | ||
if (StdCXXImportSeqState.atTopLevel()) { |
There was a problem hiding this comment.
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."
This reverts commit a480926. Reason for revert: Supposedly fixed in llvm/llvm-project#71134 Original change's description: > Temporarily disable warning -Winclude-angled-in-module-purview > > Bug: 1499028 > Change-Id: Ia73aadeb9cd34a582bc5c710235c0ecb4c6318fa > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5001651 > Reviewed-by: Alan Zhao <[email protected]> > Commit-Queue: Amy Huang <[email protected]> > Cr-Commit-Position: refs/heads/main@{#1219224} Bug: 1499028 Change-Id: I013798dc82b403fb0ecac121603ccbe98be55dac No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5003342 Commit-Queue: Rubber Stamper <[email protected]> Bot-Commit: Rubber Stamper <[email protected]> Auto-Submit: Arthur Eubanks <[email protected]> Cr-Commit-Position: refs/heads/main@{#1219530}
This reverts commit a4809261e71327bdb5a119968bd5de20b7f04d39. Reason for revert: Supposedly fixed in llvm/llvm-project#71134 Original change's description: > Temporarily disable warning -Winclude-angled-in-module-purview > > Bug: 1499028 > Change-Id: Ia73aadeb9cd34a582bc5c710235c0ecb4c6318fa > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5001651 > Reviewed-by: Alan Zhao <[email protected]> > Commit-Queue: Amy Huang <[email protected]> > Cr-Commit-Position: refs/heads/main@{#1219224} Bug: 1499028 Change-Id: I013798dc82b403fb0ecac121603ccbe98be55dac No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5003342 Commit-Queue: Rubber Stamper <[email protected]> Bot-Commit: Rubber Stamper <[email protected]> Auto-Submit: Arthur Eubanks <[email protected]> Cr-Commit-Position: refs/heads/main@{#1219530} NOKEYCHECK=True GitOrigin-RevId: 1001b65ced52aad23dc60d37aa564ba12bf4ea4b
ModuleDeclState
is incorrectly changed toNamedModuleImplementation
for
struct module {}; void foo(module a);
. This is mostly benign butleads to a spurious warning after #69555.
A real world example is: