Skip to content

Commit 1d98af4

Browse files
jansvoboda11ronlieb
authored andcommitted
[clang][lex] Always pass suggested module to InclusionDirective() callback (llvm#81061)
This patch provides more information to the `PPCallbacks::InclusionDirective()` hook. We now always pass the suggested module, regardless of whether it was actually imported or not. The extra `bool ModuleImported` parameter then denotes whether the header `#include` will be automatically translated into import the the module. The main change is in `clang/lib/Lex/PPDirectives.cpp`, where we take care to not modify `SuggestedModule` after it's been populated by `LookupHeaderIncludeOrImport()`. We now exclusively use the `SM` (`ModuleToImport`) variable instead, which has been equivalent to `SuggestedModule` until now. This allows us to use the original non-modified `SuggestedModule` for the callback itself. (This patch turns out to be necessary for swiftlang#8011). Change-Id: I6f64614c4468ae4737b20609e065d69390d2e443
1 parent 2d7a9b5 commit 1d98af4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+168
-132
lines changed

clang-tools-extra/clang-move/Move.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ class FindAllIncludes : public PPCallbacks {
133133
CharSourceRange FilenameRange,
134134
OptionalFileEntryRef /*File*/, StringRef SearchPath,
135135
StringRef /*RelativePath*/,
136-
const Module * /*Imported*/,
136+
const Module * /*SuggestedModule*/,
137+
bool /*ModuleImported*/,
137138
SrcMgr::CharacteristicKind /*FileType*/) override {
138139
if (auto FileEntry = SM.getFileEntryRefForID(SM.getFileID(HashLoc)))
139140
MoveTool->addIncludes(FileName, IsAngled, SearchPath,

clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ void ExpandModularHeadersPPCallbacks::InclusionDirective(
166166
SourceLocation DirectiveLoc, const Token &IncludeToken,
167167
StringRef IncludedFilename, bool IsAngled, CharSourceRange FilenameRange,
168168
OptionalFileEntryRef IncludedFile, StringRef SearchPath,
169-
StringRef RelativePath, const Module *Imported,
169+
StringRef RelativePath, const Module *SuggestedModule, bool ModuleImported,
170170
SrcMgr::CharacteristicKind FileType) {
171-
if (Imported) {
171+
if (ModuleImported) {
172172
serialization::ModuleFile *MF =
173173
Compiler.getASTReader()->getModuleManager().lookup(
174-
*Imported->getASTFile());
174+
*SuggestedModule->getASTFile());
175175
handleModuleFile(MF);
176176
}
177177
parseToLocation(DirectiveLoc);

clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ExpandModularHeadersPPCallbacks : public PPCallbacks {
6969
bool IsAngled, CharSourceRange FilenameRange,
7070
OptionalFileEntryRef IncludedFile,
7171
StringRef SearchPath, StringRef RelativePath,
72-
const Module *Imported,
72+
const Module *SuggestedModule, bool ModuleImported,
7373
SrcMgr::CharacteristicKind FileType) override;
7474

7575
void EndOfMainFile() override;

clang-tools-extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class KernelNameRestrictionPPCallbacks : public PPCallbacks {
2929
StringRef FileName, bool IsAngled,
3030
CharSourceRange FileNameRange,
3131
OptionalFileEntryRef File, StringRef SearchPath,
32-
StringRef RelativePath, const Module *Imported,
32+
StringRef RelativePath, const Module *SuggestedModule,
33+
bool ModuleImported,
3334
SrcMgr::CharacteristicKind FileType) override;
3435

3536
void EndOfMainFile() override;
@@ -61,7 +62,7 @@ void KernelNameRestrictionCheck::registerPPCallbacks(const SourceManager &SM,
6162
void KernelNameRestrictionPPCallbacks::InclusionDirective(
6263
SourceLocation HashLoc, const Token &, StringRef FileName, bool,
6364
CharSourceRange, OptionalFileEntryRef, StringRef, StringRef, const Module *,
64-
SrcMgr::CharacteristicKind) {
65+
bool, SrcMgr::CharacteristicKind) {
6566
IncludeDirective ID = {HashLoc, FileName};
6667
IncludeDirectives.push_back(std::move(ID));
6768
}

clang-tools-extra/clang-tidy/bugprone/SuspiciousIncludeCheck.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class SuspiciousIncludePPCallbacks : public PPCallbacks {
2626
StringRef FileName, bool IsAngled,
2727
CharSourceRange FilenameRange,
2828
OptionalFileEntryRef File, StringRef SearchPath,
29-
StringRef RelativePath, const Module *Imported,
29+
StringRef RelativePath, const Module *SuggestedModule,
30+
bool ModuleImported,
3031
SrcMgr::CharacteristicKind FileType) override;
3132

3233
private:
@@ -51,8 +52,8 @@ void SuspiciousIncludeCheck::registerPPCallbacks(
5152
void SuspiciousIncludePPCallbacks::InclusionDirective(
5253
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
5354
bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
54-
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
55-
SrcMgr::CharacteristicKind FileType) {
55+
StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule,
56+
bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
5657
if (IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import)
5758
return;
5859

clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class IncludeOrderPPCallbacks : public PPCallbacks {
2727
StringRef FileName, bool IsAngled,
2828
CharSourceRange FilenameRange,
2929
OptionalFileEntryRef File, StringRef SearchPath,
30-
StringRef RelativePath, const Module *Imported,
30+
StringRef RelativePath, const Module *SuggestedModule,
31+
bool ModuleImported,
3132
SrcMgr::CharacteristicKind FileType) override;
3233
void EndOfMainFile() override;
3334

@@ -81,8 +82,8 @@ static int getPriority(StringRef Filename, bool IsAngled, bool IsMainModule) {
8182
void IncludeOrderPPCallbacks::InclusionDirective(
8283
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
8384
bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
84-
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
85-
SrcMgr::CharacteristicKind FileType) {
85+
StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule,
86+
bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
8687
// We recognize the first include as a special main module header and want
8788
// to leave it in the top position.
8889
IncludeDirective ID = {HashLoc, FilenameRange, std::string(FileName),

clang-tools-extra/clang-tidy/llvmlibc/RestrictSystemLibcHeadersCheck.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class RestrictedIncludesPPCallbacks
3333
StringRef FileName, bool IsAngled,
3434
CharSourceRange FilenameRange,
3535
OptionalFileEntryRef File, StringRef SearchPath,
36-
StringRef RelativePath, const Module *Imported,
36+
StringRef RelativePath, const Module *SuggestedModule,
37+
bool ModuleImported,
3738
SrcMgr::CharacteristicKind FileType) override;
3839

3940
private:
@@ -45,14 +46,14 @@ class RestrictedIncludesPPCallbacks
4546
void RestrictedIncludesPPCallbacks::InclusionDirective(
4647
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
4748
bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
48-
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
49-
SrcMgr::CharacteristicKind FileType) {
49+
StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule,
50+
bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
5051
// Compiler provided headers are allowed (e.g stddef.h).
5152
if (SrcMgr::isSystem(FileType) && SearchPath == CompilerIncudeDir)
5253
return;
5354
portability::RestrictedIncludesPPCallbacks::InclusionDirective(
5455
HashLoc, IncludeTok, FileName, IsAngled, FilenameRange, File, SearchPath,
55-
RelativePath, Imported, FileType);
56+
RelativePath, SuggestedModule, ModuleImported, FileType);
5657
}
5758

5859
void RestrictSystemLibcHeadersCheck::registerPPCallbacks(

clang-tools-extra/clang-tidy/misc/HeaderIncludeCycleCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class CyclicDependencyCallbacks : public PPCallbacks {
8383
void InclusionDirective(SourceLocation, const Token &, StringRef FilePath,
8484
bool, CharSourceRange Range,
8585
OptionalFileEntryRef File, StringRef, StringRef,
86-
const Module *,
86+
const Module *, bool,
8787
SrcMgr::CharacteristicKind FileType) override {
8888
if (FileType != clang::SrcMgr::C_User)
8989
return;

clang-tools-extra/clang-tidy/modernize/DeprecatedHeadersCheck.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class IncludeModernizePPCallbacks : public PPCallbacks {
3232
StringRef FileName, bool IsAngled,
3333
CharSourceRange FilenameRange,
3434
OptionalFileEntryRef File, StringRef SearchPath,
35-
StringRef RelativePath, const Module *Imported,
35+
StringRef RelativePath, const Module *SuggestedModule,
36+
bool ModuleImported,
3637
SrcMgr::CharacteristicKind FileType) override;
3738

3839
private:
@@ -178,8 +179,8 @@ IncludeModernizePPCallbacks::IncludeModernizePPCallbacks(
178179
void IncludeModernizePPCallbacks::InclusionDirective(
179180
SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
180181
bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File,
181-
StringRef SearchPath, StringRef RelativePath, const Module *Imported,
182-
SrcMgr::CharacteristicKind FileType) {
182+
StringRef SearchPath, StringRef RelativePath, const Module *SuggestedModule,
183+
bool ModuleImported, SrcMgr::CharacteristicKind FileType) {
183184

184185
// If we don't want to warn for non-main file reports and this is one, skip
185186
// it.

clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ class MacroToEnumCallbacks : public PPCallbacks {
117117
StringRef FileName, bool IsAngled,
118118
CharSourceRange FilenameRange,
119119
OptionalFileEntryRef File, StringRef SearchPath,
120-
StringRef RelativePath, const Module *Imported,
120+
StringRef RelativePath, const Module *SuggestedModule,
121+
bool ModuleImported,
121122
SrcMgr::CharacteristicKind FileType) override {
122123
clearCurrentEnum(HashLoc);
123124
}

0 commit comments

Comments
 (0)