From 32219c8c44787c79edbd68962b554860338651e8 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Tue, 30 Mar 2021 10:45:04 +0200 Subject: [PATCH] [clang][deps] Simplify function discovering .pcm and .modulemap files This patch simplifies (and renames) the `appendCommonModuleArguments` function. It no longer tries to construct the command line for explicitly building modules. Instead, it only performs the DFS traversal of modular dependencies and queries the callbacks to collect paths to `.pcm` and `.modulemap` files. This makes it more flexible and usable in two contexts: * Generating additional command line arguments for the main TU in modular build. The `std::vector` output parameters can be used to manually generate appropriate command line flags. * Generate full command line for a module. The output parameters can be the corresponding parts of `CompilerInvocation`. (In a follow-up patch.) Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D100531 --- .../DependencyScanning/ModuleDepCollector.h | 13 +++++------ .../DependencyScanning/DependencyScanningTool.cpp | 13 +++++++++-- .../DependencyScanning/ModuleDepCollector.cpp | 26 +++++++++++++--------- clang/test/ClangScanDeps/modules-full.cpp | 2 +- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h index 87bb1b8..d482607 100644 --- a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h +++ b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h @@ -79,11 +79,11 @@ struct ModuleDeps { /// Gets the full command line suitable for passing to clang. /// - /// \param LookupPCMPath this function is called to fill in `-fmodule-file=` + /// \param LookupPCMPath This function is called to fill in `-fmodule-file=` /// flags and for the `-o` flag. It needs to return a /// path for where the PCM for the given module is to /// be located. - /// \param LookupModuleDeps this fucntion is called to collect the full + /// \param LookupModuleDeps This function is called to collect the full /// transitive set of dependencies for this /// compilation. std::vector getFullCommandLine( @@ -92,14 +92,13 @@ struct ModuleDeps { }; namespace detail { -/// Append the `-fmodule-file=` and `-fmodule-map-file=` arguments for the -/// modules in \c Modules transitively, along with other needed arguments to -/// use explicitly built modules. -void appendCommonModuleArguments( +/// Collect the paths of PCM and module map files for the modules in \c Modules +/// transitively. +void collectPCMAndModuleMapPaths( llvm::ArrayRef Modules, std::function LookupPCMPath, std::function LookupModuleDeps, - std::vector &Result); + std::vector &PCMPaths, std::vector &ModMapPaths); } // namespace detail class ModuleDepCollector; diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp index 3c61242..afb3ceb 100644 --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp @@ -18,8 +18,17 @@ std::vector FullDependencies::getAdditionalCommandLine( std::function LookupModuleDeps) const { std::vector Ret = AdditionalNonPathCommandLine; - dependencies::detail::appendCommonModuleArguments( - ClangModuleDeps, LookupPCMPath, LookupModuleDeps, Ret); + Ret.push_back("-fno-implicit-modules"); + Ret.push_back("-fno-implicit-module-maps"); + + std::vector PCMPaths; + std::vector ModMapPaths; + dependencies::detail::collectPCMAndModuleMapPaths( + ClangModuleDeps, LookupPCMPath, LookupModuleDeps, PCMPaths, ModMapPaths); + for (const std::string &PCMPath : PCMPaths) + Ret.push_back("-fmodule-file=" + PCMPath); + for (const std::string &ModMapPath : ModMapPaths) + Ret.push_back("-fmodule-map-file=" + ModMapPath); return Ret; } diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp index 1fee831..f9edae2 100644 --- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp +++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp @@ -25,17 +25,26 @@ std::vector ModuleDeps::getFullCommandLine( // TODO: Build full command line. That also means capturing the original // command line into NonPathCommandLine. - dependencies::detail::appendCommonModuleArguments( - ClangModuleDeps, LookupPCMPath, LookupModuleDeps, Ret); + Ret.push_back("-fno-implicit-modules"); + Ret.push_back("-fno-implicit-module-maps"); + + std::vector PCMPaths; + std::vector ModMapPaths; + dependencies::detail::collectPCMAndModuleMapPaths( + ClangModuleDeps, LookupPCMPath, LookupModuleDeps, PCMPaths, ModMapPaths); + for (const std::string &PCMPath : PCMPaths) + Ret.push_back("-fmodule-file=" + PCMPath); + for (const std::string &ModMapPath : ModMapPaths) + Ret.push_back("-fmodule-map-file=" + ModMapPath); return Ret; } -void dependencies::detail::appendCommonModuleArguments( +void dependencies::detail::collectPCMAndModuleMapPaths( llvm::ArrayRef Modules, std::function LookupPCMPath, std::function LookupModuleDeps, - std::vector &Result) { + std::vector &PCMPaths, std::vector &ModMapPaths) { llvm::StringSet<> AlreadyAdded; std::function)> AddArgs = @@ -46,15 +55,12 @@ void dependencies::detail::appendCommonModuleArguments( const ModuleDeps &M = LookupModuleDeps(MID); // Depth first traversal. AddArgs(M.ClangModuleDeps); - Result.push_back(("-fmodule-file=" + LookupPCMPath(MID)).str()); - if (!M.ClangModuleMapFile.empty()) { - Result.push_back("-fmodule-map-file=" + M.ClangModuleMapFile); - } + PCMPaths.push_back(LookupPCMPath(MID).str()); + if (!M.ClangModuleMapFile.empty()) + ModMapPaths.push_back(M.ClangModuleMapFile); } }; - Result.push_back("-fno-implicit-modules"); - Result.push_back("-fno-implicit-module-maps"); AddArgs(Modules); } diff --git a/clang/test/ClangScanDeps/modules-full.cpp b/clang/test/ClangScanDeps/modules-full.cpp index db25496..1f95d77 100644 --- a/clang/test/ClangScanDeps/modules-full.cpp +++ b/clang/test/ClangScanDeps/modules-full.cpp @@ -148,8 +148,8 @@ // CHECK-NEXT: "-fno-implicit-modules", // CHECK-NEXT: "-fno-implicit-module-maps", // CHECK-NEXT: "-fmodule-file=[[PREFIX]]/module-cache{{(_clangcl)?}}/[[CONTEXT_HASH_H1]]/header2-{{[A-Z0-9]+}}.pcm", -// CHECK-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap", // CHECK-NEXT: "-fmodule-file=[[PREFIX]]/module-cache{{(_clangcl)?}}/[[CONTEXT_HASH_H1]]/header1-{{[A-Z0-9]+}}.pcm", +// CHECK-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap", // CHECK-NEXT: "-fmodule-map-file=[[PREFIX]]/Inputs/module.modulemap" // CHECK-NEXT: ], // CHECK-NEXT: "file-deps": [ -- 2.7.4