clang 22.0.0git
DependencyScanningTool.cpp
Go to the documentation of this file.
1//===- DependencyScanningTool.cpp - clang-scan-deps service ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
11#include <optional>
12
13using namespace clang;
14using namespace tooling;
15using namespace dependencies;
16
21
22namespace {
23/// Prints out all of the gathered dependencies into a string.
24class MakeDependencyPrinterConsumer : public DependencyConsumer {
25public:
26 void handleBuildCommand(Command) override {}
27
28 void
29 handleDependencyOutputOpts(const DependencyOutputOptions &Opts) override {
30 this->Opts = std::make_unique<DependencyOutputOptions>(Opts);
31 }
32
33 void handleFileDependency(StringRef File) override {
34 Dependencies.push_back(std::string(File));
35 }
36
37 // These are ignored for the make format as it can't support the full
38 // set of deps, and handleFileDependency handles enough for implicitly
39 // built modules to work.
40 void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD) override {}
41 void handleModuleDependency(ModuleDeps MD) override {}
42 void handleDirectModuleDependency(ModuleID ID) override {}
43 void handleVisibleModule(std::string ModuleName) override {}
44 void handleContextHash(std::string Hash) override {}
45
46 void printDependencies(std::string &S) {
47 assert(Opts && "Handled dependency output options.");
48
49 class DependencyPrinter : public DependencyFileGenerator {
50 public:
51 DependencyPrinter(DependencyOutputOptions &Opts,
52 ArrayRef<std::string> Dependencies)
53 : DependencyFileGenerator(Opts) {
54 for (const auto &Dep : Dependencies)
55 addDependency(Dep);
56 }
57
58 void printDependencies(std::string &S) {
59 llvm::raw_string_ostream OS(S);
60 outputDependencyFile(OS);
61 }
62 };
63
64 DependencyPrinter Generator(*Opts, Dependencies);
65 Generator.printDependencies(S);
66 }
67
68protected:
69 std::unique_ptr<DependencyOutputOptions> Opts;
70 std::vector<std::string> Dependencies;
71};
72} // anonymous namespace
73
75 const std::vector<std::string> &CommandLine, StringRef CWD) {
76 MakeDependencyPrinterConsumer Consumer;
77 CallbackActionController Controller(nullptr);
78 auto Result =
79 Worker.computeDependencies(CWD, CommandLine, Consumer, Controller);
80 if (Result)
81 return std::move(Result);
82 std::string Output;
83 Consumer.printDependencies(Output);
84 return Output;
85}
86
88 const CompileCommand &Command, StringRef CWD, std::string &MakeformatOutput,
89 std::string &MakeformatOutputPath) {
90 class P1689ModuleDependencyPrinterConsumer
91 : public MakeDependencyPrinterConsumer {
92 public:
93 P1689ModuleDependencyPrinterConsumer(P1689Rule &Rule,
95 : Filename(Command.Filename), Rule(Rule) {
96 Rule.PrimaryOutput = Command.Output;
97 }
98
99 void handleProvidedAndRequiredStdCXXModules(
100 std::optional<P1689ModuleInfo> Provided,
101 std::vector<P1689ModuleInfo> Requires) override {
102 Rule.Provides = Provided;
103 if (Rule.Provides)
104 Rule.Provides->SourcePath = Filename.str();
105 Rule.Requires = Requires;
106 }
107
108 StringRef getMakeFormatDependencyOutputPath() {
109 if (Opts->OutputFormat != DependencyOutputFormat::Make)
110 return {};
111 return Opts->OutputFile;
112 }
113
114 private:
115 StringRef Filename;
116 P1689Rule &Rule;
117 };
118
119 class P1689ActionController : public DependencyActionController {
120 public:
121 // The lookupModuleOutput is for clang modules. P1689 format don't need it.
122 std::string lookupModuleOutput(const ModuleDeps &,
123 ModuleOutputKind Kind) override {
124 return "";
125 }
126 };
127
128 P1689Rule Rule;
129 P1689ModuleDependencyPrinterConsumer Consumer(Rule, Command);
130 P1689ActionController Controller;
131 auto Result = Worker.computeDependencies(CWD, Command.CommandLine, Consumer,
132 Controller);
133 if (Result)
134 return std::move(Result);
135
136 MakeformatOutputPath = Consumer.getMakeFormatDependencyOutputPath();
137 if (!MakeformatOutputPath.empty())
138 Consumer.printDependencies(MakeformatOutput);
139 return Rule;
140}
141
144 const std::vector<std::string> &CommandLine, StringRef CWD,
145 const llvm::DenseSet<ModuleID> &AlreadySeen,
146 LookupModuleOutputCallback LookupModuleOutput,
147 std::optional<llvm::MemoryBufferRef> TUBuffer) {
148 FullDependencyConsumer Consumer(AlreadySeen);
149 CallbackActionController Controller(LookupModuleOutput);
150 llvm::Error Result = Worker.computeDependencies(CWD, CommandLine, Consumer,
151 Controller, TUBuffer);
152
153 if (Result)
154 return std::move(Result);
155 return Consumer.takeTranslationUnitDeps();
156}
157
160 StringRef ModuleName, const std::vector<std::string> &CommandLine,
161 StringRef CWD, const llvm::DenseSet<ModuleID> &AlreadySeen,
162 LookupModuleOutputCallback LookupModuleOutput) {
163 FullDependencyConsumer Consumer(AlreadySeen);
164 CallbackActionController Controller(LookupModuleOutput);
165 llvm::Error Result = Worker.computeDependencies(CWD, CommandLine, Consumer,
166 Controller, ModuleName);
167 if (Result)
168 return std::move(Result);
169 return Consumer.takeTranslationUnitDeps();
170}
171
174
175 TU.ID.ContextHash = std::move(ContextHash);
176 TU.ID.ModuleName = std::move(ModuleName);
177 TU.NamedModuleDeps = std::move(NamedModuleDeps);
178 TU.FileDeps = std::move(Dependencies);
179 TU.PrebuiltModuleDeps = std::move(PrebuiltModuleDeps);
180 TU.VisibleModules = std::move(VisibleModules);
181 TU.Commands = std::move(Commands);
182
183 for (auto &&M : ClangModuleDeps) {
184 auto &MD = M.second;
185 // TODO: Avoid handleModuleDependency even being called for modules
186 // we've already seen.
187 if (AlreadySeen.count(M.first))
188 continue;
189 TU.ModuleGraph.push_back(std::move(MD));
190 }
191 TU.ClangModuleDeps = std::move(DirectModuleDeps);
192
193 return TU;
194}
195
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
A simple dependency action controller that uses a callback.
Dependency scanner callbacks that are used during scanning to influence the behaviour of the scan - f...
The dependency scanning service contains shared configuration and state that is used by the individua...
llvm::Expected< TranslationUnitDeps > getTranslationUnitDependencies(const std::vector< std::string > &CommandLine, StringRef CWD, const llvm::DenseSet< ModuleID > &AlreadySeen, LookupModuleOutputCallback LookupModuleOutput, std::optional< llvm::MemoryBufferRef > TUBuffer=std::nullopt)
Given a Clang driver command-line for a translation unit, gather the modular dependencies and return ...
llvm::Expected< P1689Rule > getP1689ModuleDependencyFile(const clang::tooling::CompileCommand &Command, StringRef CWD, std::string &MakeformatOutput, std::string &MakeformatOutputPath)
Collect the module dependency in P1689 format for C++20 named modules.
llvm::Expected< std::string > getDependencyFile(const std::vector< std::string > &CommandLine, StringRef CWD)
Print out the dependency information into a string using the dependency file format that is specified...
llvm::Expected< TranslationUnitDeps > getModuleDependencies(StringRef ModuleName, const std::vector< std::string > &CommandLine, StringRef CWD, const llvm::DenseSet< ModuleID > &AlreadySeen, LookupModuleOutputCallback LookupModuleOutput)
Given a compilation context specified via the Clang driver command-line, gather modular dependencies ...
DependencyScanningTool(DependencyScanningService &Service, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS=llvm::vfs::createPhysicalFileSystem())
Construct a dependency scanning tool.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
ModuleOutputKind
An output from a module compilation, such as the path of the module file.
llvm::function_ref< std::string(const ModuleDeps &, ModuleOutputKind)> LookupModuleOutputCallback
A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
std::shared_ptr< MatchComputation< T > > Generator
Definition RewriteRule.h:65
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
Definition TypeBase.h:905
Specifies the working directory and command of a compilation.
A command-line tool invocation that is part of building a TU.
std::string ContextHash
The context hash of a module represents the compiler options that affect the resulting command-line i...
std::string ModuleName
The name of the module.
Modular dependency that has already been built prior to the dependency scan.
The full dependencies and module graph for a specific input.
std::vector< PrebuiltModuleDep > PrebuiltModuleDeps
A collection of prebuilt modules this translation unit directly depends on, not including transitive ...
std::vector< std::string > VisibleModules
A list of module names that are visible to this translation unit.
std::vector< Command > Commands
The sequence of commands required to build the translation unit.
std::vector< std::string > FileDeps
A collection of absolute paths to files that this translation unit directly depends on,...
std::vector< std::string > NamedModuleDeps
A list of the C++20 named modules this translation unit depends on.
ModuleDepsGraph ModuleGraph
The graph of direct and transitive modular dependencies.
ModuleID ID
The identifier of the C++20 module this translation unit exports.
std::vector< ModuleID > ClangModuleDeps
A list of modules this translation unit directly depends on, not including transitive dependencies.