Skip to content

Commit 0093c2f

Browse files
committed
Adding an API to report exported modules and update existing tests.
1 parent cd826d6 commit 0093c2f

36 files changed

+159
-40
lines changed

clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ struct ModuleID {
115115
}
116116
};
117117

118+
struct ExtendedModuleID {
119+
ModuleID ID;
120+
bool Exported;
121+
122+
bool operator<(const ExtendedModuleID &Other) const {
123+
return std::tie(ID, Exported) < std::tie(Other.ID, Other.Exported);
124+
}
125+
};
126+
118127
/// P1689ModuleInfo - Represents the needed information of standard C++20
119128
/// modules for P1689 format.
120129
struct P1689ModuleInfo {
@@ -183,7 +192,7 @@ struct ModuleDeps {
183192
///
184193
/// This may include modules with a different context hash when it can be
185194
/// determined that the differences are benign for this compilation.
186-
std::vector<ModuleID> ClangModuleDeps;
195+
std::vector<ExtendedModuleID> ClangModuleDeps;
187196

188197
/// The set of libraries or frameworks to link against when
189198
/// an entity from this module is used.
@@ -270,7 +279,8 @@ class ModuleDepCollectorPP final : public PPCallbacks {
270279
llvm::DenseSet<const Module *> &AddedModules);
271280

272281
/// Add discovered module dependency for the given module.
273-
void addOneModuleDep(const Module *M, const ModuleID ID, ModuleDeps &MD);
282+
void addOneModuleDep(const Module *M, bool Exported, const ModuleID ID,
283+
ModuleDeps &MD);
274284
};
275285

276286
/// Collects modular and non-modular dependencies of the main file by attaching
@@ -352,16 +362,16 @@ class ModuleDepCollector final : public DependencyCollector {
352362

353363
/// Collect module map files for given modules.
354364
llvm::DenseSet<const FileEntry *>
355-
collectModuleMapFiles(ArrayRef<ModuleID> ClangModuleDeps) const;
365+
collectModuleMapFiles(ArrayRef<ExtendedModuleID> ClangModuleDeps) const;
356366

357367
/// Add module map files to the invocation, if needed.
358368
void addModuleMapFiles(CompilerInvocation &CI,
359-
ArrayRef<ModuleID> ClangModuleDeps) const;
369+
ArrayRef<ExtendedModuleID> ClangModuleDeps) const;
360370
/// Add module files (pcm) to the invocation, if needed.
361371
void addModuleFiles(CompilerInvocation &CI,
362-
ArrayRef<ModuleID> ClangModuleDeps) const;
372+
ArrayRef<ExtendedModuleID> ClangModuleDeps) const;
363373
void addModuleFiles(CowCompilerInvocation &CI,
364-
ArrayRef<ModuleID> ClangModuleDeps) const;
374+
ArrayRef<ExtendedModuleID> ClangModuleDeps) const;
365375

366376
/// Add paths that require looking up outputs to the given dependencies.
367377
void addOutputPaths(CowCompilerInvocation &CI, ModuleDeps &Deps);

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,10 @@ ModuleDepCollector::getInvocationAdjustedForModuleBuildWithoutOutputs(
389389
}
390390

391391
llvm::DenseSet<const FileEntry *> ModuleDepCollector::collectModuleMapFiles(
392-
ArrayRef<ModuleID> ClangModuleDeps) const {
392+
ArrayRef<ExtendedModuleID> ClangModuleDeps) const {
393393
llvm::DenseSet<const FileEntry *> ModuleMapFiles;
394-
for (const ModuleID &MID : ClangModuleDeps) {
395-
ModuleDeps *MD = ModuleDepsByID.lookup(MID);
394+
for (const auto &MID : ClangModuleDeps) {
395+
ModuleDeps *MD = ModuleDepsByID.lookup(MID.ID);
396396
assert(MD && "Inconsistent dependency info");
397397
// TODO: Track ClangModuleMapFile as `FileEntryRef`.
398398
auto FE = ScanInstance.getFileManager().getOptionalFileRef(
@@ -404,44 +404,45 @@ llvm::DenseSet<const FileEntry *> ModuleDepCollector::collectModuleMapFiles(
404404
}
405405

406406
void ModuleDepCollector::addModuleMapFiles(
407-
CompilerInvocation &CI, ArrayRef<ModuleID> ClangModuleDeps) const {
407+
CompilerInvocation &CI, ArrayRef<ExtendedModuleID> ClangModuleDeps) const {
408408
if (Service.shouldEagerLoadModules())
409409
return; // Only pcm is needed for eager load.
410410

411-
for (const ModuleID &MID : ClangModuleDeps) {
412-
ModuleDeps *MD = ModuleDepsByID.lookup(MID);
411+
for (const auto &MID : ClangModuleDeps) {
412+
ModuleDeps *MD = ModuleDepsByID.lookup(MID.ID);
413413
assert(MD && "Inconsistent dependency info");
414414
CI.getFrontendOpts().ModuleMapFiles.push_back(MD->ClangModuleMapFile);
415415
}
416416
}
417417

418418
void ModuleDepCollector::addModuleFiles(
419-
CompilerInvocation &CI, ArrayRef<ModuleID> ClangModuleDeps) const {
420-
for (const ModuleID &MID : ClangModuleDeps) {
421-
ModuleDeps *MD = ModuleDepsByID.lookup(MID);
419+
CompilerInvocation &CI, ArrayRef<ExtendedModuleID> ClangModuleDeps) const {
420+
for (const auto &MID : ClangModuleDeps) {
421+
ModuleDeps *MD = ModuleDepsByID.lookup(MID.ID);
422422
std::string PCMPath =
423423
Controller.lookupModuleOutput(*MD, ModuleOutputKind::ModuleFile);
424424

425425
if (Service.shouldEagerLoadModules())
426426
CI.getFrontendOpts().ModuleFiles.push_back(std::move(PCMPath));
427427
else
428428
CI.getHeaderSearchOpts().PrebuiltModuleFiles.insert(
429-
{MID.ModuleName, std::move(PCMPath)});
429+
{MID.ID.ModuleName, std::move(PCMPath)});
430430
}
431431
}
432432

433433
void ModuleDepCollector::addModuleFiles(
434-
CowCompilerInvocation &CI, ArrayRef<ModuleID> ClangModuleDeps) const {
435-
for (const ModuleID &MID : ClangModuleDeps) {
436-
ModuleDeps *MD = ModuleDepsByID.lookup(MID);
434+
CowCompilerInvocation &CI,
435+
ArrayRef<ExtendedModuleID> ClangModuleDeps) const {
436+
for (const auto &MID : ClangModuleDeps) {
437+
ModuleDeps *MD = ModuleDepsByID.lookup(MID.ID);
437438
std::string PCMPath =
438439
Controller.lookupModuleOutput(*MD, ModuleOutputKind::ModuleFile);
439440

440441
if (Service.shouldEagerLoadModules())
441442
CI.getMutFrontendOpts().ModuleFiles.push_back(std::move(PCMPath));
442443
else
443444
CI.getMutHeaderSearchOpts().PrebuiltModuleFiles.insert(
444-
{MID.ModuleName, std::move(PCMPath)});
445+
{MID.ID.ModuleName, std::move(PCMPath)});
445446
}
446447
}
447448

@@ -471,10 +472,10 @@ void ModuleDepCollector::applyDiscoveredDependencies(CompilerInvocation &CI) {
471472
CI.getFrontendOpts().ModuleMapFiles.emplace_back(
472473
CurrentModuleMap->getNameAsRequested());
473474

474-
SmallVector<ModuleID> DirectDeps;
475+
SmallVector<ExtendedModuleID> DirectDeps;
475476
for (const auto &KV : ModularDeps)
476477
if (DirectModularDeps.contains(KV.first))
477-
DirectDeps.push_back(KV.second->ID);
478+
DirectDeps.push_back({KV.second->ID, /* Exported = */ false});
478479

479480
// TODO: Report module maps the same way it's done for modular dependencies.
480481
addModuleMapFiles(CI, DirectDeps);
@@ -598,9 +599,9 @@ static std::string getModuleContextHash(const ModuleDeps &MD,
598599
// example, case-insensitive paths to modulemap files. Usually such a case
599600
// would indicate a missed optimization to canonicalize, but it may be
600601
// difficult to canonicalize all cases when there is a VFS.
601-
for (const auto &ID : MD.ClangModuleDeps) {
602-
HashBuilder.add(ID.ModuleName);
603-
HashBuilder.add(ID.ContextHash);
602+
for (const auto &EMID : MD.ClangModuleDeps) {
603+
HashBuilder.add(EMID.ID.ModuleName);
604+
HashBuilder.add(EMID.ID.ContextHash);
604605
}
605606

606607
HashBuilder.add(EagerLoadModules);
@@ -924,22 +925,30 @@ void ModuleDepCollectorPP::addAllSubmoduleDeps(
924925
});
925926
}
926927

927-
void ModuleDepCollectorPP::addOneModuleDep(const Module *M, const ModuleID ID,
928-
ModuleDeps &MD) {
929-
MD.ClangModuleDeps.push_back(ID);
928+
void ModuleDepCollectorPP::addOneModuleDep(const Module *M, bool Exported,
929+
const ModuleID ID, ModuleDeps &MD) {
930+
MD.ClangModuleDeps.push_back({ID, Exported});
931+
930932
if (MD.IsInStableDirectories)
931933
MD.IsInStableDirectories = MDC.ModularDeps[M]->IsInStableDirectories;
932934
}
933935

934936
void ModuleDepCollectorPP::addModuleDep(
935937
const Module *M, ModuleDeps &MD,
936938
llvm::DenseSet<const Module *> &AddedModules) {
939+
SmallVector<Module *> ExportedModulesVector;
940+
M->getExportedModules(ExportedModulesVector);
941+
llvm::DenseSet<const Module *> ExportedModulesSet(
942+
ExportedModulesVector.begin(), ExportedModulesVector.end());
937943
for (const Module *Import : M->Imports) {
938-
if (Import->getTopLevelModule() != M->getTopLevelModule() &&
944+
const Module *ImportedTopLevelModule = Import->getTopLevelModule();
945+
if (ImportedTopLevelModule != M->getTopLevelModule() &&
939946
!MDC.isPrebuiltModule(Import)) {
940-
if (auto ImportID = handleTopLevelModule(Import->getTopLevelModule()))
941-
if (AddedModules.insert(Import->getTopLevelModule()).second)
942-
addOneModuleDep(Import->getTopLevelModule(), *ImportID, MD);
947+
if (auto ImportID = handleTopLevelModule(ImportedTopLevelModule))
948+
if (AddedModules.insert(ImportedTopLevelModule).second) {
949+
bool Exported = ExportedModulesSet.contains(ImportedTopLevelModule);
950+
addOneModuleDep(ImportedTopLevelModule, Exported, *ImportID, MD);
951+
}
943952
}
944953
}
945954
}
@@ -963,7 +972,7 @@ void ModuleDepCollectorPP::addAffectingClangModule(
963972
!MDC.isPrebuiltModule(Affecting)) {
964973
if (auto ImportID = handleTopLevelModule(Affecting))
965974
if (AddedModules.insert(Affecting).second)
966-
addOneModuleDep(Affecting, *ImportID, MD);
975+
addOneModuleDep(Affecting, /* Exported = */ false, *ImportID, MD);
967976
}
968977
}
969978
}

clang/test/ClangScanDeps/diagnostics.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module mod { header "mod.h" }
3838
// CHECK-NEXT: "[[PREFIX]]/mod.h"
3939
// CHECK-NEXT: ],
4040
// CHECK-NEXT: "link-libraries": [],
41+
// CHECK-NEXT: "clang-modules-exported": [],
4142
// CHECK-NEXT: "name": "mod"
4243
// CHECK-NEXT: }
4344
// CHECK-NEXT: ],

clang/test/ClangScanDeps/header-search-pruning-transitive.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ module X { header "X.h" }
7676
// CHECK-NEXT: "[[PREFIX]]/X.h"
7777
// CHECK-NEXT: ],
7878
// CHECK-NEXT: "link-libraries": [],
79+
// CHECK-NEXT: "clang-modules-exported": [],
7980
// CHECK-NEXT: "name": "X"
8081
// CHECK-NEXT: },
8182
// CHECK-NEXT: {
@@ -92,6 +93,7 @@ module X { header "X.h" }
9293
// CHECK-NEXT: "[[PREFIX]]/end/end.h"
9394
// CHECK-NEXT: ],
9495
// CHECK-NEXT: "link-libraries": [],
96+
// CHECK-NEXT: "clang-modules-exported": [],
9597
// CHECK-NEXT: "name": "Y"
9698
// CHECK-NEXT: }
9799
// CHECK-NEXT: ],
@@ -132,6 +134,7 @@ module X { header "X.h" }
132134
// CHECK-NEXT: "[[PREFIX]]/X.h"
133135
// CHECK-NEXT: ],
134136
// CHECK-NEXT: "link-libraries": [],
137+
// CHECK-NEXT: "clang-modules-exported": [],
135138
// CHECK-NEXT: "name": "X"
136139
// CHECK-NEXT: },
137140
// CHECK-NEXT: {
@@ -147,6 +150,7 @@ module X { header "X.h" }
147150
// CHECK-NEXT: "[[PREFIX]]/end/end.h"
148151
// CHECK-NEXT: ],
149152
// CHECK-NEXT: "link-libraries": [],
153+
// CHECK-NEXT: "clang-modules-exported": [],
150154
// CHECK-NEXT: "name": "Y"
151155
// CHECK-NEXT: }
152156
// CHECK-NEXT: ],

clang/test/ClangScanDeps/header-search-pruning.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
// CHECK_A-NEXT: "file-deps": [
3535
// CHECK_A: ],
3636
// CHECK_A-NEXT: "link-libraries": [],
37+
// CHECK_A-NEXT: "clang-modules-exported": [],
3738
// CHECK_A-NEXT: "name": "mod"
3839
// CHECK_A-NEXT: }
3940
// CHECK_A-NEXT: ]
@@ -57,6 +58,7 @@
5758
// CHECK_B-NEXT: "file-deps": [
5859
// CHECK_B: ],
5960
// CHECK_B-NEXT: "link-libraries": [],
61+
// CHECK_B-NEXT: "clang-modules-exported": [],
6062
// CHECK_B-NEXT: "name": "mod"
6163
// CHECK_B-NEXT: }
6264
// CHECK_B-NEXT: ]
@@ -82,6 +84,7 @@
8284
// CHECK_AB-NEXT: "file-deps": [
8385
// CHECK_AB: ],
8486
// CHECK_AB-NEXT: "link-libraries": [],
87+
// CHECK_AB-NEXT: "clang-modules-exported": [],
8588
// CHECK_AB-NEXT: "name": "mod"
8689
// CHECK_AB-NEXT: }
8790
// CHECK_AB-NEXT: ]

clang/test/ClangScanDeps/link-libraries.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module transitive {
5353
// CHECK-NEXT: "link-name": "Framework"
5454
// CHECK-NEXT: }
5555
// CHECK-NEXT: ],
56+
// CHECK-NEXT: "clang-modules-exported": [],
5657
// CHECK-NEXT: "name": "Framework"
5758
// CHECK-NEXT: },
5859
// CHECK-NEXT: {
@@ -71,6 +72,7 @@ module transitive {
7172
// CHECK-NEXT: "[[PREFIX]]/direct.h"
7273
// CHECK-NEXT: ],
7374
// CHECK-NEXT: "link-libraries": [],
75+
// CHECK-NEXT: "clang-modules-exported": [],
7476
// CHECK-NEXT: "name": "direct"
7577
// CHECK-NEXT: },
7678
// CHECK-NEXT: {
@@ -95,6 +97,7 @@ module transitive {
9597
// CHECK-NEXT: "[[PREFIX]]/Inputs/frameworks/module.modulemap"
9698
// CHECK-NEXT: ],
9799
// CHECK-NEXT: "link-libraries": [],
100+
// CHECK-NEXT: "clang-modules-exported": [],
98101
// CHECK-NEXT: "name": "root"
99102
// CHECK-NEXT: },
100103
// CHECK-NEXT: {
@@ -113,6 +116,7 @@ module transitive {
113116
// CHECK-NEXT: "link-name": "libTransitive"
114117
// CHECK-NEXT: }
115118
// CHECK-NEXT: ],
119+
// CHECK-NEXT: "clang-modules-exported": [],
116120
// CHECK-NEXT: "name": "transitive"
117121
// CHECK-NEXT: }
118122
// CHECK-NEXT: ],

clang/test/ClangScanDeps/modules-canononical-module-map-case.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ framework module FW {
7171
// CHECK-NEXT: "link-name": "FW"
7272
// CHECK-NEXT: }
7373
// CHECK-NEXT: ],
74+
// CHECK-NEXT: "clang-modules-exported": [],
7475
// CHECK-NEXT: "name": "FW"
7576
// CHECK-NEXT: }
7677
// CHECK-NEXT: ]

clang/test/ClangScanDeps/modules-context-hash.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
// CHECK-NEXT: "[[PREFIX]]/a/dep.h"
4040
// CHECK-NEXT: ],
4141
// CHECK-NEXT: "link-libraries": [],
42+
// CHECK-NEXT: "clang-modules-exported": [],
4243
// CHECK-NEXT: "name": "mod"
4344
// CHECK-NEXT: }
4445
// CHECK-NEXT: ],
@@ -77,6 +78,7 @@
7778
// CHECK-NEXT: "[[PREFIX]]/b/dep.h"
7879
// CHECK-NEXT: ],
7980
// CHECK-NEXT: "link-libraries": [],
81+
// CHECK-NEXT: "clang-modules-exported": [],
8082
// CHECK-NEXT: "name": "mod"
8183
// CHECK-NEXT: }
8284
// CHECK-NEXT: ],

clang/test/ClangScanDeps/modules-dep-args.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ module Direct { header "direct.h" }
6262
// CHECK-NEXT: "[[PREFIX]]/direct.h"
6363
// CHECK-NEXT: ],
6464
// CHECK-NEXT: "link-libraries": [],
65+
// CHECK-NEXT: "clang-modules-exported": [],
6566
// CHECK-NEXT: "name": "Direct"
6667
// CHECK-NEXT: },
6768
// CHECK-NEXT: {
@@ -75,6 +76,7 @@ module Direct { header "direct.h" }
7576
// CHECK-NEXT: "[[PREFIX]]/transitive.h"
7677
// CHECK-NEXT: ],
7778
// CHECK-NEXT: "link-libraries": [],
79+
// CHECK-NEXT: "clang-modules-exported": [],
7880
// CHECK-NEXT: "name": "Transitive"
7981
// CHECK-NEXT: }
8082
// CHECK-NEXT: ],

clang/test/ClangScanDeps/modules-excluded-header.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
// CHECK-NEXT: "file-deps": [
4444
// CHECK: ],
4545
// CHECK-NEXT: "link-libraries": [],
46+
// CHECK-NEXT: "clang-modules-exported": [],
4647
// CHECK-NEXT: "name": "Mod"
4748
// CHECK-NEXT: }
4849
// CHECK-NEXT: ]

0 commit comments

Comments
 (0)