diff options
author | Fangrui Song <i@maskray.me> | 2023-11-02 22:13:08 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-02 22:13:08 -0700 |
commit | 071f3b5b659fe26812d413a3acb7455fa11e93c8 (patch) | |
tree | 5ec14f8262df9c4ba1d522a4bde4d8bc6386d390 /clang/lib/Lex/Preprocessor.cpp | |
parent | 7443af1ed301af00359af4b353d697f9bc80cf23 (diff) | |
download | llvm-071f3b5b659fe26812d413a3acb7455fa11e93c8.zip llvm-071f3b5b659fe26812d413a3acb7455fa11e93c8.tar.gz llvm-071f3b5b659fe26812d413a3acb7455fa11e93c8.tar.bz2 |
[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
```
Diffstat (limited to 'clang/lib/Lex/Preprocessor.cpp')
-rw-r--r-- | clang/lib/Lex/Preprocessor.cpp | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp index ede4c51..45c0f84 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -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()) { + 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(); |