clang 17.0.0git
DependencyScanningTool.h
Go to the documentation of this file.
1//===- DependencyScanningTool.h - 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
9#ifndef LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGTOOL_H
10#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGTOOL_H
11
16#include "llvm/ADT/MapVector.h"
17#include "llvm/ADT/StringSet.h"
18#include "llvm/ADT/StringMap.h"
19#include <optional>
20#include <string>
21#include <vector>
22
23namespace clang {
24namespace tooling {
25namespace dependencies {
26
27/// A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
29 llvm::function_ref<std::string(const ModuleID &, ModuleOutputKind)>;
30
31/// Graph of modular dependencies.
32using ModuleDepsGraph = std::vector<ModuleDeps>;
33
34/// The full dependencies and module graph for a specific input.
36 /// The graph of direct and transitive modular dependencies.
38
39 /// The identifier of the C++20 module this translation unit exports.
40 ///
41 /// If the translation unit is not a module then \c ID.ModuleName is empty.
43
44 /// A collection of absolute paths to files that this translation unit
45 /// directly depends on, not including transitive dependencies.
46 std::vector<std::string> FileDeps;
47
48 /// A collection of prebuilt modules this translation unit directly depends
49 /// on, not including transitive dependencies.
50 std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;
51
52 /// A list of modules this translation unit directly depends on, not including
53 /// transitive dependencies.
54 ///
55 /// This may include modules with a different context hash when it can be
56 /// determined that the differences are benign for this compilation.
57 std::vector<ModuleID> ClangModuleDeps;
58
59 /// The sequence of commands required to build the translation unit. Commands
60 /// should be executed in order.
61 ///
62 /// FIXME: If we add support for multi-arch builds in clang-scan-deps, we
63 /// should make the dependencies between commands explicit to enable parallel
64 /// builds of each architecture.
65 std::vector<Command> Commands;
66
67 /// Deprecated driver command-line. This will be removed in a future version.
68 std::vector<std::string> DriverCommandLine;
69};
70
71/// The high-level implementation of the dependency discovery tool that runs on
72/// an individual worker thread.
74public:
75 /// Construct a dependency scanning tool.
78 llvm::vfs::createPhysicalFileSystem());
79
80 /// Print out the dependency information into a string using the dependency
81 /// file format that is specified in the options (-MD is the default) and
82 /// return it.
83 ///
84 /// \returns A \c StringError with the diagnostic output if clang errors
85 /// occurred, dependency file contents otherwise.
87 getDependencyFile(const std::vector<std::string> &CommandLine, StringRef CWD);
88
89 /// Given a Clang driver command-line for a translation unit, gather the
90 /// modular dependencies and return the information needed for explicit build.
91 ///
92 /// \param AlreadySeen This stores modules which have previously been
93 /// reported. Use the same instance for all calls to this
94 /// function for a single \c DependencyScanningTool in a
95 /// single build. Use a different one for different tools,
96 /// and clear it between builds.
97 /// \param LookupModuleOutput This function is called to fill in
98 /// "-fmodule-file=", "-o" and other output
99 /// arguments for dependencies.
100 ///
101 /// \returns a \c StringError with the diagnostic output if clang errors
102 /// occurred, \c TranslationUnitDeps otherwise.
104 getTranslationUnitDependencies(const std::vector<std::string> &CommandLine,
105 StringRef CWD,
106 const llvm::StringSet<> &AlreadySeen,
107 LookupModuleOutputCallback LookupModuleOutput);
108
109 /// Given a compilation context specified via the Clang driver command-line,
110 /// gather modular dependencies of module with the given name, and return the
111 /// information needed for explicit build.
113 getModuleDependencies(StringRef ModuleName,
114 const std::vector<std::string> &CommandLine,
115 StringRef CWD, const llvm::StringSet<> &AlreadySeen,
116 LookupModuleOutputCallback LookupModuleOutput);
117
118private:
120};
121
123public:
124 FullDependencyConsumer(const llvm::StringSet<> &AlreadySeen,
125 LookupModuleOutputCallback LookupModuleOutput)
126 : AlreadySeen(AlreadySeen), LookupModuleOutput(LookupModuleOutput) {}
127
129 Commands.push_back(std::move(Cmd));
130 }
131
133
134 void handleFileDependency(StringRef File) override {
135 Dependencies.push_back(std::string(File));
136 }
137
139 PrebuiltModuleDeps.emplace_back(std::move(PMD));
140 }
141
143 ClangModuleDeps[MD.ID.ContextHash + MD.ID.ModuleName] = std::move(MD);
144 }
145
146 void handleContextHash(std::string Hash) override {
147 ContextHash = std::move(Hash);
148 }
149
150 std::string lookupModuleOutput(const ModuleID &ID,
151 ModuleOutputKind Kind) override {
152 return LookupModuleOutput(ID, Kind);
153 }
154
157
158private:
159 std::vector<std::string> Dependencies;
160 std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;
161 llvm::MapVector<std::string, ModuleDeps, llvm::StringMap<unsigned>>
162 ClangModuleDeps;
163 std::vector<Command> Commands;
164 std::string ContextHash;
165 std::vector<std::string> OutputPaths;
166 const llvm::StringSet<> &AlreadySeen;
167 LookupModuleOutputCallback LookupModuleOutput;
168};
169
170} // end namespace dependencies
171} // end namespace tooling
172} // end namespace clang
173
174#endif // LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGTOOL_H
CompileCommand Cmd
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
The dependency scanning service contains shared configuration and state that is used by the individua...
The high-level implementation of the dependency discovery tool that runs on an individual worker thre...
llvm::Expected< TranslationUnitDeps > getTranslationUnitDependencies(const std::vector< std::string > &CommandLine, StringRef CWD, const llvm::StringSet<> &AlreadySeen, LookupModuleOutputCallback LookupModuleOutput)
Given a Clang driver command-line for a translation unit, gather the modular dependencies and return ...
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< ModuleDepsGraph > getModuleDependencies(StringRef ModuleName, const std::vector< std::string > &CommandLine, StringRef CWD, const llvm::StringSet<> &AlreadySeen, LookupModuleOutputCallback LookupModuleOutput)
Given a compilation context specified via the Clang driver command-line, gather modular dependencies ...
An individual dependency scanning worker that is able to run on its own thread.
std::string lookupModuleOutput(const ModuleID &ID, ModuleOutputKind Kind) override
FullDependencyConsumer(const llvm::StringSet<> &AlreadySeen, LookupModuleOutputCallback LookupModuleOutput)
void handleDependencyOutputOpts(const DependencyOutputOptions &) override
void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD) override
ModuleOutputKind
An output from a module compilation, such as the path of the module file.
std::vector< ModuleDeps > ModuleDepsGraph
Graph of modular dependencies.
llvm::function_ref< std::string(const ModuleID &, ModuleOutputKind)> LookupModuleOutputCallback
A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
A command-line tool invocation that is part of building a TU.
ModuleID ID
The identifier of the module.
This is used to identify a specific module.
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< 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,...
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.
std::vector< std::string > DriverCommandLine
Deprecated driver command-line. This will be removed in a future version.