Skip to content

Commit 729fa59

Browse files
committed
[clang][include-tree] Fix spurious dependencies
1 parent c245082 commit 729fa59

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

clang/include/clang/Basic/Module.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ class alignas(8) Module {
350350
LLVM_PREFERRED_TYPE(bool)
351351
unsigned IsInferred : 1;
352352

353+
/// Whether this is an inferred submodule that's missing from the umbrella
354+
/// header.
355+
unsigned IsInferredMissingFromUmbrellaHeader : 1;
356+
353357
/// Whether we should infer submodules for this module based on
354358
/// the headers.
355359
///

clang/lib/Basic/Module.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
4242
HasIncompatibleModuleFile(false), IsAvailable(true),
4343
IsFromModuleFile(false), IsFramework(IsFramework), IsExplicit(IsExplicit),
4444
IsSystem(false), IsExternC(false), IsInferred(false),
45+
IsInferredMissingFromUmbrellaHeader(false),
4546
InferSubmodules(false), InferExplicitSubmodules(false),
4647
InferExportWildcard(false), ConfigMacrosExhaustive(false),
4748
NoUndeclaredIncludes(false), ModuleMapIsPrivate(false),

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,8 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
22272227
<< Module->getFullModuleName()
22282228
<< SourceRange(Path.front().second, Path.back().second);
22292229

2230+
Module->IsInferredMissingFromUmbrellaHeader = true;
2231+
22302232
return ModuleLoadResult(Module, ModuleLoadResult::MissingExpected);
22312233
}
22322234

clang/lib/Tooling/DependencyScanning/IncludeTreeActionController.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,7 @@ struct IncludeTreePPCallbacks : public PPCallbacks {
204204
bool ModuleImported,
205205
SrcMgr::CharacteristicKind FileType) override {
206206
// File includes are handled by LexedFileChanged.
207-
if (!SuggestedModule)
208-
return;
209-
210-
// Modules that will not be imported and were not loaded from an AST file
211-
// (e.g. the module being implemented) are also handled by LexedFileChanged.
212-
if (!ModuleImported && !SuggestedModule->getASTFile())
207+
if (!ModuleImported)
213208
return;
214209

215210
// Calculate EndLoc for the directive
@@ -462,20 +457,25 @@ void IncludeTreeBuilder::exitedInclude(Preprocessor &PP, FileID IncludedBy,
462457
SourceManager &SM = PP.getSourceManager();
463458
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedExpansionLoc(ExitLoc);
464459

465-
// If the file includes already has a node for the current source location, it
466-
// must be an import that turned out to be spurious.
467-
auto &CurIncludes = IncludeStack.back().Includes;
468-
if (!CurIncludes.empty() && CurIncludes.back().Offset == LocInfo.second) {
469-
assert(!IncludeTree->isSubmodule());
470-
auto Import = CurIncludes.pop_back_val();
471-
assert(Import.Kind == cas::IncludeTree::NodeKind::ModuleImport);
472-
auto SpuriousImport = cas::IncludeTree::SpuriousImport::create(
473-
DB, Import.Ref, IncludeTree->getRef());
474-
if (!SpuriousImport)
460+
if (auto FE = PP.getSourceManager().getFileEntryRefForID(Include)) {
461+
ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
462+
Module *M = ModMap.findModuleForHeader(*FE).getModule();
463+
if (M && M->IsInferredMissingFromUmbrellaHeader) {
464+
assert(!IncludeTree->isSubmodule() &&
465+
"Include of header missing from umbrella header is modular");
466+
467+
moduleImport(PP, M, ExitLoc);
468+
auto Import = IncludeStack.back().Includes.pop_back_val();
469+
470+
auto SpuriousImport = check(cas::IncludeTree::SpuriousImport::create(
471+
DB, Import.Ref, IncludeTree->getRef()));
472+
if (!SpuriousImport)
473+
return;
474+
IncludeStack.back().Includes.push_back(
475+
{SpuriousImport->getRef(), LocInfo.second,
476+
cas::IncludeTree::NodeKind::SpuriousImport});
475477
return;
476-
CurIncludes.push_back({SpuriousImport->getRef(), LocInfo.second,
477-
cas::IncludeTree::NodeKind::SpuriousImport});
478-
return;
478+
}
479479
}
480480

481481
IncludeStack.back().Includes.push_back({IncludeTree->getRef(), LocInfo.second,

0 commit comments

Comments
 (0)