diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2023-02-11 12:38:04 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2023-02-11 12:38:11 +0000 |
| commit | e3b557809604d036af6e00c60f012c2025b59a5e (patch) | |
| tree | 8a11ba2269a3b669601e2fd41145b174008f4da8 /clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp | |
| parent | 08e8dd7b9db7bb4a9de26d44c1cbfd24e869c014 (diff) | |
Diffstat (limited to 'clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp')
| -rw-r--r-- | clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp | 166 |
1 files changed, 90 insertions, 76 deletions
diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp index 411fd9676ffd..3fcef00a5780 100644 --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp @@ -8,32 +8,18 @@ #include "clang/Tooling/DependencyScanning/DependencyScanningTool.h" #include "clang/Frontend/Utils.h" +#include <optional> using namespace clang; using namespace tooling; using namespace dependencies; -std::vector<std::string> FullDependencies::getCommandLine( - llvm::function_ref<std::string(const ModuleID &, ModuleOutputKind)> - LookupModuleOutput) const { - std::vector<std::string> Ret = getCommandLineWithoutModulePaths(); - - for (ModuleID MID : ClangModuleDeps) { - auto PCM = LookupModuleOutput(MID, ModuleOutputKind::ModuleFile); - Ret.push_back("-fmodule-file=" + PCM); - } - - return Ret; -} - -std::vector<std::string> -FullDependencies::getCommandLineWithoutModulePaths() const { +static std::vector<std::string> +makeTUCommandLineWithoutPaths(ArrayRef<std::string> OriginalCommandLine) { std::vector<std::string> Args = OriginalCommandLine; Args.push_back("-fno-implicit-modules"); Args.push_back("-fno-implicit-module-maps"); - for (const PrebuiltModuleDep &PMD : PrebuiltModuleDeps) - Args.push_back("-fmodule-file=" + PMD.PCMFile); // These arguments are unused in explicit compiles. llvm::erase_if(Args, [](StringRef Arg) { @@ -56,10 +42,12 @@ DependencyScanningTool::DependencyScanningTool( llvm::Expected<std::string> DependencyScanningTool::getDependencyFile( const std::vector<std::string> &CommandLine, StringRef CWD, - llvm::Optional<StringRef> ModuleName) { + std::optional<StringRef> ModuleName) { /// Prints out all of the gathered dependencies into a string. class MakeDependencyPrinterConsumer : public DependencyConsumer { public: + void handleBuildCommand(Command) override {} + void handleDependencyOutputOpts(const DependencyOutputOptions &Opts) override { this->Opts = std::make_unique<DependencyOutputOptions>(Opts); @@ -81,6 +69,11 @@ llvm::Expected<std::string> DependencyScanningTool::getDependencyFile( void handleContextHash(std::string Hash) override {} + std::string lookupModuleOutput(const ModuleID &ID, + ModuleOutputKind Kind) override { + llvm::report_fatal_error("unexpected call to lookupModuleOutput"); + } + void printDependencies(std::string &S) { assert(Opts && "Handled dependency output options."); @@ -122,77 +115,98 @@ llvm::Expected<FullDependenciesResult> DependencyScanningTool::getFullDependencies( const std::vector<std::string> &CommandLine, StringRef CWD, const llvm::StringSet<> &AlreadySeen, - llvm::Optional<StringRef> ModuleName) { - class FullDependencyPrinterConsumer : public DependencyConsumer { - public: - FullDependencyPrinterConsumer(const llvm::StringSet<> &AlreadySeen) - : AlreadySeen(AlreadySeen) {} - - void - handleDependencyOutputOpts(const DependencyOutputOptions &Opts) override {} - - void handleFileDependency(StringRef File) override { - Dependencies.push_back(std::string(File)); - } + LookupModuleOutputCallback LookupModuleOutput, + std::optional<StringRef> ModuleName) { + FullDependencyConsumer Consumer(AlreadySeen, LookupModuleOutput, + Worker.shouldEagerLoadModules()); + llvm::Error Result = + Worker.computeDependencies(CWD, CommandLine, Consumer, ModuleName); + if (Result) + return std::move(Result); + return Consumer.takeFullDependencies(); +} - void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD) override { - PrebuiltModuleDeps.emplace_back(std::move(PMD)); - } +llvm::Expected<FullDependenciesResult> +DependencyScanningTool::getFullDependenciesLegacyDriverCommand( + const std::vector<std::string> &CommandLine, StringRef CWD, + const llvm::StringSet<> &AlreadySeen, + LookupModuleOutputCallback LookupModuleOutput, + std::optional<StringRef> ModuleName) { + FullDependencyConsumer Consumer(AlreadySeen, LookupModuleOutput, + Worker.shouldEagerLoadModules()); + llvm::Error Result = + Worker.computeDependencies(CWD, CommandLine, Consumer, ModuleName); + if (Result) + return std::move(Result); + return Consumer.getFullDependenciesLegacyDriverCommand(CommandLine); +} - void handleModuleDependency(ModuleDeps MD) override { - ClangModuleDeps[MD.ID.ContextHash + MD.ID.ModuleName] = std::move(MD); - } +FullDependenciesResult FullDependencyConsumer::takeFullDependencies() { + FullDependenciesResult FDR; + FullDependencies &FD = FDR.FullDeps; - void handleContextHash(std::string Hash) override { - ContextHash = std::move(Hash); - } + FD.ID.ContextHash = std::move(ContextHash); + FD.FileDeps = std::move(Dependencies); + FD.PrebuiltModuleDeps = std::move(PrebuiltModuleDeps); + FD.Commands = std::move(Commands); - FullDependenciesResult getFullDependencies( - const std::vector<std::string> &OriginalCommandLine) const { - FullDependencies FD; + for (auto &&M : ClangModuleDeps) { + auto &MD = M.second; + if (MD.ImportedByMainFile) + FD.ClangModuleDeps.push_back(MD.ID); + // TODO: Avoid handleModuleDependency even being called for modules + // we've already seen. + if (AlreadySeen.count(M.first)) + continue; + FDR.DiscoveredModules.push_back(std::move(MD)); + } - FD.OriginalCommandLine = - ArrayRef<std::string>(OriginalCommandLine).slice(1); + return FDR; +} - FD.ID.ContextHash = std::move(ContextHash); +FullDependenciesResult +FullDependencyConsumer::getFullDependenciesLegacyDriverCommand( + const std::vector<std::string> &OriginalCommandLine) const { + FullDependencies FD; - FD.FileDeps.assign(Dependencies.begin(), Dependencies.end()); + FD.DriverCommandLine = makeTUCommandLineWithoutPaths( + ArrayRef<std::string>(OriginalCommandLine).slice(1)); - for (auto &&M : ClangModuleDeps) { - auto &MD = M.second; - if (MD.ImportedByMainFile) - FD.ClangModuleDeps.push_back(MD.ID); - } + FD.ID.ContextHash = std::move(ContextHash); - FD.PrebuiltModuleDeps = std::move(PrebuiltModuleDeps); + FD.FileDeps.assign(Dependencies.begin(), Dependencies.end()); - FullDependenciesResult FDR; + for (const PrebuiltModuleDep &PMD : PrebuiltModuleDeps) + FD.DriverCommandLine.push_back("-fmodule-file=" + PMD.PCMFile); - for (auto &&M : ClangModuleDeps) { - // TODO: Avoid handleModuleDependency even being called for modules - // we've already seen. - if (AlreadySeen.count(M.first)) - continue; - FDR.DiscoveredModules.push_back(std::move(M.second)); + for (auto &&M : ClangModuleDeps) { + auto &MD = M.second; + if (MD.ImportedByMainFile) { + FD.ClangModuleDeps.push_back(MD.ID); + auto PCMPath = LookupModuleOutput(MD.ID, ModuleOutputKind::ModuleFile); + if (EagerLoadModules) { + FD.DriverCommandLine.push_back("-fmodule-file=" + PCMPath); + } else { + FD.DriverCommandLine.push_back("-fmodule-map-file=" + + MD.ClangModuleMapFile); + FD.DriverCommandLine.push_back("-fmodule-file=" + MD.ID.ModuleName + + "=" + PCMPath); } - - FDR.FullDeps = std::move(FD); - return FDR; } + } - private: - std::vector<std::string> Dependencies; - std::vector<PrebuiltModuleDep> PrebuiltModuleDeps; - llvm::MapVector<std::string, ModuleDeps, llvm::StringMap<unsigned>> ClangModuleDeps; - std::string ContextHash; - std::vector<std::string> OutputPaths; - const llvm::StringSet<> &AlreadySeen; - }; + FD.PrebuiltModuleDeps = std::move(PrebuiltModuleDeps); - FullDependencyPrinterConsumer Consumer(AlreadySeen); - llvm::Error Result = - Worker.computeDependencies(CWD, CommandLine, Consumer, ModuleName); - if (Result) - return std::move(Result); - return Consumer.getFullDependencies(CommandLine); + FullDependenciesResult FDR; + + for (auto &&M : ClangModuleDeps) { + // TODO: Avoid handleModuleDependency even being called for modules + // we've already seen. + if (AlreadySeen.count(M.first)) + continue; + FDR.DiscoveredModules.push_back(std::move(M.second)); + } + + FDR.FullDeps = std::move(FD); + return FDR; } |
