Skip to content

[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
merged 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
29 changes: 15 additions & 14 deletions clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
20 changes: 8 additions & 12 deletions clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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))) {}

Expand Down
Loading