diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningService.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningService.h --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningService.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningService.h @@ -65,12 +65,14 @@ } private: - const ScanningMode Mode; - const ScanningOutputFormat Format; + /// The preprocessing mode used for scanning. + ScanningMode Mode; + /// The output format. + ScanningOutputFormat Format; /// Whether to optimize the modules' command-line arguments. - const bool OptimizeArgs; + bool OptimizeArgs; /// Whether to set up command-lines to load PCM files eagerly. - const bool EagerLoadModules; + bool EagerLoadModules; /// The global file system cache. DependencyScanningFilesystemSharedCache SharedCache; }; diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h @@ -100,9 +100,13 @@ DependencyConsumer &Consumer, DependencyActionController &Controller, std::optional ModuleName = std::nullopt); - bool shouldEagerLoadModules() const { return EagerLoadModules; } + /// Get the parent scanning service. + const DependencyScanningService &getService() const { return Service; } private: + /// The parent scanning service. + const DependencyScanningService &Service; + /// The PCH container reader/writer. std::shared_ptr PCHContainerOps; /// The file system to be used during the scan. /// This is either \c FS passed in the constructor (when performing canonical @@ -112,11 +116,6 @@ /// dependency-directives-extracting) filesystem overlaid on top of \c FS /// (passed in the constructor). llvm::IntrusiveRefCntPtr DepFS; - ScanningOutputFormat Format; - /// Whether to optimize the modules' command-line arguments. - bool OptimizeArgs; - /// Whether to set up command-lines to load PCM files eagerly. - bool EagerLoadModules; }; } // end namespace dependencies diff --git a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h --- a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h +++ b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h @@ -16,6 +16,7 @@ #include "clang/Lex/HeaderSearch.h" #include "clang/Lex/PPCallbacks.h" #include "clang/Serialization/ASTReader.h" +#include "clang/Tooling/DependencyScanning/DependencyScanningService.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringSet.h" #include "llvm/Support/raw_ostream.h" @@ -209,8 +210,8 @@ ModuleDepCollector(std::unique_ptr Opts, CompilerInstance &ScanInstance, DependencyConsumer &C, DependencyActionController &Controller, - CompilerInvocation OriginalCI, bool OptimizeArgs, - bool EagerLoadModules, bool IsStdModuleP1689Format); + CompilerInvocation OriginalCI, + const DependencyScanningService &Service); void attachToPreprocessor(Preprocessor &PP) override; void attachToASTReader(ASTReader &R) override; @@ -246,13 +247,7 @@ std::unique_ptr Opts; /// The original Clang invocation passed to dependency scanner. CompilerInvocation OriginalInvocation; - /// Whether to optimize the modules' command-line arguments. - bool OptimizeArgs; - /// Whether to set up command-lines to load PCM files eagerly. - bool EagerLoadModules; - /// If we're generating dependency output in P1689 format - /// for standard C++ modules. - bool IsStdModuleP1689Format; + const DependencyScanningService &Service; std::optional ProvidedStdCXXModule; std::vector RequiredStdCXXModules; diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp @@ -137,11 +137,10 @@ StringRef WorkingDirectory, DependencyConsumer &Consumer, DependencyActionController &Controller, llvm::IntrusiveRefCntPtr DepFS, - ScanningOutputFormat Format, bool OptimizeArgs, bool EagerLoadModules, + const DependencyScanningService &Service, bool DisableFree, std::optional ModuleName = std::nullopt) : WorkingDirectory(WorkingDirectory), Consumer(Consumer), - Controller(Controller), DepFS(std::move(DepFS)), Format(Format), - OptimizeArgs(OptimizeArgs), EagerLoadModules(EagerLoadModules), + Controller(Controller), DepFS(std::move(DepFS)), Service(Service), DisableFree(DisableFree), ModuleName(ModuleName) {} bool runInvocation(std::shared_ptr Invocation, @@ -230,7 +229,7 @@ ScanInstance.getFrontendOpts().Inputs)}; Opts->IncludeSystemHeaders = true; - switch (Format) { + switch (Service.getFormat()) { case ScanningOutputFormat::Make: ScanInstance.addDependencyCollector( std::make_shared( @@ -240,8 +239,7 @@ case ScanningOutputFormat::Full: MDC = std::make_shared( std::move(Opts), ScanInstance, Consumer, Controller, - OriginalInvocation, OptimizeArgs, EagerLoadModules, - Format == ScanningOutputFormat::P1689); + OriginalInvocation, Service); ScanInstance.addDependencyCollector(MDC); break; } @@ -291,9 +289,7 @@ DependencyConsumer &Consumer; DependencyActionController &Controller; llvm::IntrusiveRefCntPtr DepFS; - ScanningOutputFormat Format; - bool OptimizeArgs; - bool EagerLoadModules; + const DependencyScanningService &Service; bool DisableFree; std::optional ModuleName; std::optional ScanInstanceStorage; @@ -307,8 +303,7 @@ DependencyScanningWorker::DependencyScanningWorker( DependencyScanningService &Service, llvm::IntrusiveRefCntPtr FS) - : Format(Service.getFormat()), OptimizeArgs(Service.canOptimizeArgs()), - EagerLoadModules(Service.shouldEagerLoadModules()) { + : Service(Service) { PCHContainerOps = std::make_shared(); // We need to read object files from PCH built outside the scanner. PCHContainerOps->registerReader( @@ -449,8 +444,7 @@ // always true for a driver invocation. bool DisableFree = true; DependencyScanningAction Action(WorkingDirectory, Consumer, Controller, DepFS, - Format, OptimizeArgs, EagerLoadModules, - DisableFree, ModuleName); + Service, DisableFree, ModuleName); bool Success = forEachDriverJob( FinalCommandLine, *Diags, *FileMgr, [&](const driver::Command &Cmd) { if (StringRef(Cmd.getCreator().getName()) != "clang") { diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp --- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp +++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp @@ -140,7 +140,8 @@ // TODO: Verify this works fine when modulemap for module A is eagerly // loaded from A.pcm, and module map passed on the command line contains // definition of a submodule: "explicit module A.Private { ... }". - if (EagerLoadModules && DepModuleMapFiles.contains(*ModuleMapEntry)) + if (Service.shouldEagerLoadModules() && + DepModuleMapFiles.contains(*ModuleMapEntry)) continue; // Don't report module map file of the current module unless it also @@ -193,7 +194,7 @@ void ModuleDepCollector::addModuleMapFiles( CompilerInvocation &CI, ArrayRef ClangModuleDeps) const { - if (EagerLoadModules) + if (Service.shouldEagerLoadModules()) return; // Only pcm is needed for eager load. for (const ModuleID &MID : ClangModuleDeps) { @@ -208,7 +209,7 @@ for (const ModuleID &MID : ClangModuleDeps) { std::string PCMPath = Controller.lookupModuleOutput(MID, ModuleOutputKind::ModuleFile); - if (EagerLoadModules) + if (Service.shouldEagerLoadModules()) CI.getFrontendOpts().ModuleFiles.push_back(std::move(PCMPath)); else CI.getHeaderSearchOpts().PrebuiltModuleFiles.insert( @@ -297,7 +298,8 @@ void ModuleDepCollector::associateWithContextHash(const CompilerInvocation &CI, ModuleDeps &Deps) { - Deps.ID.ContextHash = getModuleContextHash(Deps, CI, EagerLoadModules); + Deps.ID.ContextHash = + getModuleContextHash(Deps, CI, Service.shouldEagerLoadModules()); bool Inserted = ModuleDepsByID.insert({Deps.ID, &Deps}).second; (void)Inserted; assert(Inserted && "duplicate module mapping"); @@ -401,7 +403,7 @@ MDC.Consumer.handleDependencyOutputOpts(*MDC.Opts); - if (MDC.IsStdModuleP1689Format) + if (MDC.Service.getFormat() == ScanningOutputFormat::P1689) MDC.Consumer.handleProvidedAndRequiredStdCXXModules( MDC.ProvidedStdCXXModule, MDC.RequiredStdCXXModules); @@ -480,7 +482,7 @@ CompilerInvocation CI = MDC.makeInvocationForModuleBuildWithoutOutputs( MD, [&](CompilerInvocation &BuildInvocation) { - if (MDC.OptimizeArgs) + if (MDC.Service.canOptimizeArgs()) optimizeHeaderSearchOpts(BuildInvocation.getHeaderSearchOpts(), *MDC.ScanInstance.getASTReader(), *MF); }); @@ -579,11 +581,10 @@ std::unique_ptr Opts, CompilerInstance &ScanInstance, DependencyConsumer &C, DependencyActionController &Controller, CompilerInvocation OriginalCI, - bool OptimizeArgs, bool EagerLoadModules, bool IsStdModuleP1689Format) + const DependencyScanningService &Service) : ScanInstance(ScanInstance), Consumer(C), Controller(Controller), Opts(std::move(Opts)), OriginalInvocation(std::move(OriginalCI)), - OptimizeArgs(OptimizeArgs), EagerLoadModules(EagerLoadModules), - IsStdModuleP1689Format(IsStdModuleP1689Format) {} + Service(Service) {} void ModuleDepCollector::attachToPreprocessor(Preprocessor &PP) { PP.addPPCallbacks(std::make_unique(*this));