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

Conversation

MaskRay
Copy link
Member

@MaskRay MaskRay commented Nov 3, 2023

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

…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`
```
@MaskRay MaskRay requested a review from ChuanqiXu9 November 3, 2023 02:55
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 3, 2023
@MaskRay MaskRay requested a review from dwblaikie November 3, 2023 02:56
@llvmbot
Copy link
Member

llvmbot commented Nov 3, 2023

@llvm/pr-subscribers-clang

Author: Fangrui Song (MaskRay)

Changes

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`

Full diff: https://github.com/llvm/llvm-project/pull/71134.diff

2 Files Affected:

  • (modified) clang/lib/Lex/Preprocessor.cpp (+19-18)
  • (modified) clang/test/Preprocessor/include-in-module-purview.cppm (+8)
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>

Copy link
Member

@ChuanqiXu9 ChuanqiXu9 left a 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()) {
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."

@MaskRay MaskRay merged commit 071f3b5 into llvm:main Nov 3, 2023
@MaskRay MaskRay deleted the lex-module branch November 3, 2023 05:13
aarongable pushed a commit to chromium/chromium that referenced this pull request Nov 3, 2023
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}
UI-RayanWang pushed a commit to ubiquiti/ubnt_libjingle_component_src_build that referenced this pull request Dec 15, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants