-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][depscan] Centralize logic for populating StableDirs, NFC #135704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
@llvm/pr-subscribers-clang Author: Cyndy Ishida (cyndyishida) ChangesPass a reference to Full diff: https://github.com/llvm/llvm-project/pull/135704.diff 3 Files Affected:
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))) {}
|
jansvoboda11
approved these changes
Apr 15, 2025
var-const
pushed a commit
to ldionne/llvm-project
that referenced
this pull request
Apr 17, 2025
…m#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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pass a reference to
StableDirs
when creating ModuleDepCollector. This avoids needing to create one from the same ScanInstance for each call tohandleTopLevelModule
& reduces the amount of potential downstream changes needed for handling StableDirs.