Author: Cyndy Ishida
Date: 2025-04-15T09:59:23-07:00
New Revision: 40050888a11a92a2fbf4da5dda5956ca9ecb6b55

URL: 
https://github.com/llvm/llvm-project/commit/40050888a11a92a2fbf4da5dda5956ca9ecb6b55
DIFF: 
https://github.com/llvm/llvm-project/commit/40050888a11a92a2fbf4da5dda5956ca9ecb6b55.diff

LOG: [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.

Added: 
    

Modified: 
    clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h
    clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp
    clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Removed: 
    


################################################################################
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))) {}
 


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to