Skip to content

Commit 4005088

Browse files
authored
[clang][depscan] Centralize logic for populating StableDirs, NFC (#135704)
Pass a reference to `StableDirs` when creating ModuleDepCollector. This avoids needing to create one from the same ScanInstance for each call to `handleTopLevelModule` & reduces the amount of potential downstream changes needed for handling StableDirs.
1 parent a955399 commit 4005088

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ class ModuleDepCollector final : public DependencyCollector {
282282
CompilerInstance &ScanInstance, DependencyConsumer &C,
283283
DependencyActionController &Controller,
284284
CompilerInvocation OriginalCI,
285-
const PrebuiltModulesAttrsMap PrebuiltModulesASTMap);
285+
const PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
286+
const ArrayRef<StringRef> StableDirs);
286287

287288
void attachToPreprocessor(Preprocessor &PP) override;
288289
void attachToASTReader(ASTReader &R) override;
@@ -305,6 +306,9 @@ class ModuleDepCollector final : public DependencyCollector {
305306
/// Mapping from prebuilt AST filepaths to their attributes referenced during
306307
/// dependency collecting.
307308
const PrebuiltModulesAttrsMap PrebuiltModulesASTMap;
309+
/// Directory paths known to be stable through an active development and build
310+
/// cycle.
311+
const ArrayRef<StringRef> StableDirs;
308312
/// Path to the main source file.
309313
std::string MainFile;
310314
/// Hash identifying the compilation conditions of the current TU.

clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class PrebuiltModuleListener : public ASTReaderListener {
100100
PrebuiltModulesAttrsMap &PrebuiltModulesASTMap,
101101
const HeaderSearchOptions &HSOpts,
102102
const LangOptions &LangOpts, DiagnosticsEngine &Diags,
103-
const llvm::SmallVector<StringRef> &StableDirs)
103+
const ArrayRef<StringRef> StableDirs)
104104
: PrebuiltModuleFiles(PrebuiltModuleFiles),
105105
NewModuleFiles(NewModuleFiles),
106106
PrebuiltModulesASTMap(PrebuiltModulesASTMap), ExistingHSOpts(HSOpts),
@@ -199,7 +199,7 @@ class PrebuiltModuleListener : public ASTReaderListener {
199199
const LangOptions &ExistingLangOpts;
200200
DiagnosticsEngine &Diags;
201201
std::string CurrentFile;
202-
const llvm::SmallVector<StringRef> &StableDirs;
202+
const ArrayRef<StringRef> StableDirs;
203203
};
204204

205205
/// Visit the given prebuilt module and collect all of the modules it
@@ -208,16 +208,8 @@ static bool visitPrebuiltModule(StringRef PrebuiltModuleFilename,
208208
CompilerInstance &CI,
209209
PrebuiltModuleFilesT &ModuleFiles,
210210
PrebuiltModulesAttrsMap &PrebuiltModulesASTMap,
211-
DiagnosticsEngine &Diags) {
212-
213-
// Gather the set of stable directories to use as transitive dependencies are
214-
// discovered.
215-
llvm::SmallVector<StringRef> StableDirs;
216-
std::string SysrootToUse(CI.getHeaderSearchOpts().Sysroot);
217-
if (!SysrootToUse.empty() &&
218-
(llvm::sys::path::root_directory(SysrootToUse) != SysrootToUse))
219-
StableDirs = {SysrootToUse, CI.getHeaderSearchOpts().ResourceDir};
220-
211+
DiagnosticsEngine &Diags,
212+
const ArrayRef<StringRef> StableDirs) {
221213
// List of module files to be processed.
222214
llvm::SmallVector<std::string> Worklist;
223215

@@ -448,6 +440,15 @@ class DependencyScanningAction : public tooling::ToolAction {
448440
auto *FileMgr = ScanInstance.createFileManager(FS);
449441
ScanInstance.createSourceManager(*FileMgr);
450442

443+
// Create a collection of stable directories derived from the ScanInstance
444+
// for determining whether module dependencies would fully resolve from
445+
// those directories.
446+
llvm::SmallVector<StringRef> StableDirs;
447+
const StringRef Sysroot = ScanInstance.getHeaderSearchOpts().Sysroot;
448+
if (!Sysroot.empty() &&
449+
(llvm::sys::path::root_directory(Sysroot) != Sysroot))
450+
StableDirs = {Sysroot, ScanInstance.getHeaderSearchOpts().ResourceDir};
451+
451452
// Store a mapping of prebuilt module files and their properties like header
452453
// search options. This will prevent the implicit build to create duplicate
453454
// modules and will force reuse of the existing prebuilt module files
@@ -459,7 +460,7 @@ class DependencyScanningAction : public tooling::ToolAction {
459460
ScanInstance.getPreprocessorOpts().ImplicitPCHInclude,
460461
ScanInstance,
461462
ScanInstance.getHeaderSearchOpts().PrebuiltModuleFiles,
462-
PrebuiltModulesASTMap, ScanInstance.getDiagnostics()))
463+
PrebuiltModulesASTMap, ScanInstance.getDiagnostics(), StableDirs))
463464
return false;
464465

465466
// Create the dependency collector that will collect the produced
@@ -489,7 +490,7 @@ class DependencyScanningAction : public tooling::ToolAction {
489490
case ScanningOutputFormat::Full:
490491
MDC = std::make_shared<ModuleDepCollector>(
491492
Service, std::move(Opts), ScanInstance, Consumer, Controller,
492-
OriginalInvocation, std::move(PrebuiltModulesASTMap));
493+
OriginalInvocation, std::move(PrebuiltModulesASTMap), StableDirs);
493494
ScanInstance.addDependencyCollector(MDC);
494495
break;
495496
}

clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -763,14 +763,9 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
763763
MD.IsSystem = M->IsSystem;
764764

765765
// Start off with the assumption that this module is shareable when there
766-
// is a sysroot provided. As more dependencies are discovered, check if those
767-
// come from the provided shared directories.
768-
const llvm::SmallVector<StringRef> StableDirs = {
769-
MDC.ScanInstance.getHeaderSearchOpts().Sysroot,
770-
MDC.ScanInstance.getHeaderSearchOpts().ResourceDir};
771-
MD.IsInStableDirectories =
772-
!StableDirs[0].empty() &&
773-
(llvm::sys::path::root_directory(StableDirs[0]) != StableDirs[0]);
766+
// are stable directories. As more dependencies are discovered, check if those
767+
// come from the provided directories.
768+
MD.IsInStableDirectories = !MDC.StableDirs.empty();
774769

775770
// For modules which use export_as link name, the linked product that of the
776771
// corresponding export_as-named module.
@@ -817,7 +812,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
817812
auto FullFilePath = ASTReader::ResolveImportedPath(
818813
PathBuf, IFI.UnresolvedImportedFilename, MF->BaseDirectory);
819814
MD.IsInStableDirectories =
820-
isPathInStableDir(StableDirs, *FullFilePath);
815+
isPathInStableDir(MDC.StableDirs, *FullFilePath);
821816
}
822817
if (!(IFI.TopLevel && IFI.ModuleMap))
823818
return;
@@ -864,7 +859,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
864859
// IsInStableDirectories.
865860
if (MD.IsInStableDirectories)
866861
MD.IsInStableDirectories =
867-
areOptionsInStableDir(StableDirs, CI.getHeaderSearchOpts());
862+
areOptionsInStableDir(MDC.StableDirs, CI.getHeaderSearchOpts());
868863

869864
MDC.associateWithContextHash(CI, IgnoreCWD, MD);
870865

@@ -978,11 +973,12 @@ ModuleDepCollector::ModuleDepCollector(
978973
std::unique_ptr<DependencyOutputOptions> Opts,
979974
CompilerInstance &ScanInstance, DependencyConsumer &C,
980975
DependencyActionController &Controller, CompilerInvocation OriginalCI,
981-
const PrebuiltModulesAttrsMap PrebuiltModulesASTMap)
976+
const PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
977+
const ArrayRef<StringRef> StableDirs)
982978
: Service(Service), ScanInstance(ScanInstance), Consumer(C),
983979
Controller(Controller),
984980
PrebuiltModulesASTMap(std::move(PrebuiltModulesASTMap)),
985-
Opts(std::move(Opts)),
981+
StableDirs(StableDirs), Opts(std::move(Opts)),
986982
CommonInvocation(
987983
makeCommonInvocationForModuleBuild(std::move(OriginalCI))) {}
988984

0 commit comments

Comments
 (0)