[clang] [clang][scan-deps] Centralize logic for populating StableDirs, NFC (PR #135704)
Cyndy Ishida via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 14 17:12:50 PDT 2025
https://github.com/cyndyishida created https://github.com/llvm/llvm-project/pull/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.
>From c0df5a3d188010ee96c56ee4d39b103fd846bdc6 Mon Sep 17 00:00:00 2001
From: Cyndy Ishida <cyndy_ishida at apple.com>
Date: Mon, 14 Apr 2025 17:04:47 -0700
Subject: [PATCH] [clang][scan-deps] Centralize logic for populating
StableDirs, NFC
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.
---
.../DependencyScanning/ModuleDepCollector.h | 6 +++-
.../DependencyScanningWorker.cpp | 29 ++++++++++---------
.../DependencyScanning/ModuleDepCollector.cpp | 20 +++++--------
3 files changed, 28 insertions(+), 27 deletions(-)
diff --git a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h
index ce5e67d2624d9..d2d0d56e5212c 100644
--- a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h
+++ b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h
@@ -282,7 +282,8 @@ class ModuleDepCollector final : public DependencyCollector {
CompilerInstance &ScanInstance, DependencyConsumer &C,
DependencyActionController &Controller,
CompilerInvocation OriginalCI,
- const PrebuiltModulesAttrsMap PrebuiltModulesASTMap);
+ const PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
+ const ArrayRef<StringRef> StableDirs);
void attachToPreprocessor(Preprocessor &PP) override;
void attachToASTReader(ASTReader &R) override;
@@ -305,6 +306,9 @@ class ModuleDepCollector final : public DependencyCollector {
/// Mapping from prebuilt AST filepaths to their attributes referenced during
/// dependency collecting.
const PrebuiltModulesAttrsMap PrebuiltModulesASTMap;
+ /// Directory paths known to be stable through an active development and build
+ /// cycle.
+ const ArrayRef<StringRef> StableDirs;
/// Path to the main source file.
std::string MainFile;
/// Hash identifying the compilation conditions of the current TU.
diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
index 6595f8ff5dc55..bae436afe0897 100644
--- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
+++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
@@ -100,7 +100,7 @@ class PrebuiltModuleListener : public ASTReaderListener {
PrebuiltModulesAttrsMap &PrebuiltModulesASTMap,
const HeaderSearchOptions &HSOpts,
const LangOptions &LangOpts, DiagnosticsEngine &Diags,
- const llvm::SmallVector<StringRef> &StableDirs)
+ const ArrayRef<StringRef> StableDirs)
: PrebuiltModuleFiles(PrebuiltModuleFiles),
NewModuleFiles(NewModuleFiles),
PrebuiltModulesASTMap(PrebuiltModulesASTMap), ExistingHSOpts(HSOpts),
@@ -199,7 +199,7 @@ class PrebuiltModuleListener : public ASTReaderListener {
const LangOptions &ExistingLangOpts;
DiagnosticsEngine &Diags;
std::string CurrentFile;
- const llvm::SmallVector<StringRef> &StableDirs;
+ const ArrayRef<StringRef> StableDirs;
};
/// Visit the given prebuilt module and collect all of the modules it
@@ -208,16 +208,8 @@ static bool visitPrebuiltModule(StringRef PrebuiltModuleFilename,
CompilerInstance &CI,
PrebuiltModuleFilesT &ModuleFiles,
PrebuiltModulesAttrsMap &PrebuiltModulesASTMap,
- DiagnosticsEngine &Diags) {
-
- // Gather the set of stable directories to use as transitive dependencies are
- // discovered.
- llvm::SmallVector<StringRef> StableDirs;
- std::string SysrootToUse(CI.getHeaderSearchOpts().Sysroot);
- if (!SysrootToUse.empty() &&
- (llvm::sys::path::root_directory(SysrootToUse) != SysrootToUse))
- StableDirs = {SysrootToUse, CI.getHeaderSearchOpts().ResourceDir};
-
+ DiagnosticsEngine &Diags,
+ const ArrayRef<StringRef> StableDirs) {
// List of module files to be processed.
llvm::SmallVector<std::string> Worklist;
@@ -448,6 +440,15 @@ class DependencyScanningAction : public tooling::ToolAction {
auto *FileMgr = ScanInstance.createFileManager(FS);
ScanInstance.createSourceManager(*FileMgr);
+ // Create a collection of stable directories derived from the ScanInstance
+ // for determining whether module dependencies would fully resolve from
+ // those directories.
+ llvm::SmallVector<StringRef> StableDirs;
+ const StringRef Sysroot = ScanInstance.getHeaderSearchOpts().Sysroot;
+ if (!Sysroot.empty() &&
+ (llvm::sys::path::root_directory(Sysroot) != Sysroot))
+ StableDirs = {Sysroot, ScanInstance.getHeaderSearchOpts().ResourceDir};
+
// Store a mapping of prebuilt module files and their properties like header
// search options. This will prevent the implicit build to create duplicate
// modules and will force reuse of the existing prebuilt module files
@@ -459,7 +460,7 @@ class DependencyScanningAction : public tooling::ToolAction {
ScanInstance.getPreprocessorOpts().ImplicitPCHInclude,
ScanInstance,
ScanInstance.getHeaderSearchOpts().PrebuiltModuleFiles,
- PrebuiltModulesASTMap, ScanInstance.getDiagnostics()))
+ PrebuiltModulesASTMap, ScanInstance.getDiagnostics(), StableDirs))
return false;
// Create the dependency collector that will collect the produced
@@ -489,7 +490,7 @@ class DependencyScanningAction : public tooling::ToolAction {
case ScanningOutputFormat::Full:
MDC = std::make_shared<ModuleDepCollector>(
Service, std::move(Opts), ScanInstance, Consumer, Controller,
- OriginalInvocation, std::move(PrebuiltModulesASTMap));
+ OriginalInvocation, std::move(PrebuiltModulesASTMap), StableDirs);
ScanInstance.addDependencyCollector(MDC);
break;
}
diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
index ebd392fbfa7d6..429bf823616da 100644
--- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
+++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
@@ -763,14 +763,9 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
MD.IsSystem = M->IsSystem;
// Start off with the assumption that this module is shareable when there
- // is a sysroot provided. As more dependencies are discovered, check if those
- // come from the provided shared directories.
- const llvm::SmallVector<StringRef> StableDirs = {
- MDC.ScanInstance.getHeaderSearchOpts().Sysroot,
- MDC.ScanInstance.getHeaderSearchOpts().ResourceDir};
- MD.IsInStableDirectories =
- !StableDirs[0].empty() &&
- (llvm::sys::path::root_directory(StableDirs[0]) != StableDirs[0]);
+ // are stable directories. As more dependencies are discovered, check if those
+ // come from the provided directories.
+ MD.IsInStableDirectories = !MDC.StableDirs.empty();
// For modules which use export_as link name, the linked product that of the
// corresponding export_as-named module.
@@ -817,7 +812,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
auto FullFilePath = ASTReader::ResolveImportedPath(
PathBuf, IFI.UnresolvedImportedFilename, MF->BaseDirectory);
MD.IsInStableDirectories =
- isPathInStableDir(StableDirs, *FullFilePath);
+ isPathInStableDir(MDC.StableDirs, *FullFilePath);
}
if (!(IFI.TopLevel && IFI.ModuleMap))
return;
@@ -864,7 +859,7 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
// IsInStableDirectories.
if (MD.IsInStableDirectories)
MD.IsInStableDirectories =
- areOptionsInStableDir(StableDirs, CI.getHeaderSearchOpts());
+ areOptionsInStableDir(MDC.StableDirs, CI.getHeaderSearchOpts());
MDC.associateWithContextHash(CI, IgnoreCWD, MD);
@@ -978,11 +973,12 @@ ModuleDepCollector::ModuleDepCollector(
std::unique_ptr<DependencyOutputOptions> Opts,
CompilerInstance &ScanInstance, DependencyConsumer &C,
DependencyActionController &Controller, CompilerInvocation OriginalCI,
- const PrebuiltModulesAttrsMap PrebuiltModulesASTMap)
+ const PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
+ const ArrayRef<StringRef> StableDirs)
: Service(Service), ScanInstance(ScanInstance), Consumer(C),
Controller(Controller),
PrebuiltModulesASTMap(std::move(PrebuiltModulesASTMap)),
- Opts(std::move(Opts)),
+ StableDirs(StableDirs), Opts(std::move(Opts)),
CommonInvocation(
makeCommonInvocationForModuleBuild(std::move(OriginalCI))) {}
More information about the cfe-commits
mailing list