clang 23.0.0git
DependencyGraph.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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_DEPENDENCYSCANNING_DEPENDENCYGRAPH_H
10#define LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYGRAPH_H
11
12#include "clang/Basic/LLVM.h"
13#include "clang/Basic/Module.h"
15#include "llvm/ADT/STLFunctionalExtras.h"
16#include "llvm/ADT/SmallVector.h"
17
18#include <string>
19#include <variant>
20#include <vector>
21
22namespace clang::dependencies {
23/// Modular dependency that has already been built prior to the dependency scan.
25 std::string ModuleName;
26 std::string PCMFile;
27 std::string ModuleMapFile;
28};
29
30/// This is used to identify a specific module.
31struct ModuleID {
32 /// The name of the module. This may include `:` for C++20 module partitions,
33 /// or a header-name for C++20 header units.
34 std::string ModuleName;
35
36 /// The context hash of a module represents the compiler options that affect
37 /// the resulting command-line invocation.
38 ///
39 /// Modules with the same name and ContextHash but different invocations could
40 /// cause non-deterministic build results.
41 ///
42 /// Modules with the same name but a different \c ContextHash should be
43 /// treated as separate modules for the purpose of a build.
44 std::string ContextHash;
45
46 bool operator==(const ModuleID &Other) const {
47 return std::tie(ModuleName, ContextHash) ==
48 std::tie(Other.ModuleName, Other.ContextHash);
49 }
50
51 bool operator<(const ModuleID &Other) const {
52 return std::tie(ModuleName, ContextHash) <
53 std::tie(Other.ModuleName, Other.ContextHash);
54 }
55};
56
57/// P1689ModuleInfo - Represents the needed information of standard C++20
58/// modules for P1689 format.
60 /// The name of the module. This may include `:` for partitions.
61 std::string ModuleName;
62
63 /// Optional. The source path to the module.
64 std::string SourcePath;
65
66 /// If this module is a standard c++ interface unit.
68
69 enum class ModuleType {
71 // To be supported
72 // AngleHeaderUnit,
73 // QuoteHeaderUnit
74 };
76};
77
78struct ModuleDeps {
79 /// The identifier of the module.
81
82 /// Whether this is a "system" module.
84
85 /// Whether this module is fully composed of file & module inputs from
86 /// locations likely to stay the same across the active development and build
87 /// cycle. For example, when all those input paths only resolve in Sysroot.
88 ///
89 /// External paths, as opposed to virtual file paths, are always used
90 /// for computing this value.
92
93 /// Whether current working directory is ignored.
95
96 /// The path to the modulemap file which defines this module.
97 ///
98 /// This can be used to explicitly build this module. This file will
99 /// additionally appear in \c FileDeps as a dependency.
101
102 /// A collection of absolute paths to module map files that this module needs
103 /// to know about. The ordering is significant.
104 std::vector<std::string> ModuleMapFileDeps;
105
106 /// A collection of prebuilt modular dependencies this module directly depends
107 /// on, not including transitive dependencies.
108 std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;
109
110 /// A list of module identifiers this module directly depends on, not
111 /// including transitive dependencies.
112 ///
113 /// This may include modules with a different context hash when it can be
114 /// determined that the differences are benign for this compilation.
115 std::vector<ModuleID> ClangModuleDeps;
116
117 /// The set of libraries or frameworks to link against when
118 /// an entity from this module is used.
120
121 /// Invokes \c Cb for all file dependencies of this module. Each provided
122 /// \c StringRef is only valid within the individual callback invocation.
123 void forEachFileDep(llvm::function_ref<void(StringRef)> Cb) const;
124
125 /// Get (or compute) the compiler invocation that can be used to build this
126 /// module. Does not include argv[0].
127 const std::vector<std::string> &getBuildArguments() const;
128
129private:
130 friend class ModuleDepCollector;
132
133 /// The absolute directory path that is the base for relative paths
134 /// in \c FileDeps.
135 std::string FileDepsBaseDir;
136
137 /// A collection of paths to files that this module directly depends on, not
138 /// including transitive dependencies.
139 std::vector<std::string> FileDeps;
140
141 mutable std::variant<std::monostate, CowCompilerInvocation,
142 std::vector<std::string>>
143 BuildInfo;
144};
145
146/// A command-line tool invocation that is part of building a TU.
147///
148/// \see TranslationUnitDeps::Commands.
149struct Command {
150 std::string Executable;
151 std::vector<std::string> Arguments;
152};
153
154/// Graph of modular dependencies.
155using ModuleDepsGraph = std::vector<ModuleDeps>;
156
157/// The full dependencies and module graph for a specific input.
159 /// The graph of direct and transitive modular dependencies.
161
162 /// The identifier of the C++20 module this translation unit exports.
163 ///
164 /// If the translation unit is not a module then \c ID.ModuleName is empty.
166
167 /// A collection of absolute paths to files that this translation unit
168 /// directly depends on, not including transitive dependencies.
169 std::vector<std::string> FileDeps;
170
171 /// A collection of prebuilt modules this translation unit directly depends
172 /// on, not including transitive dependencies.
173 std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;
174
175 /// A list of modules this translation unit directly depends on, not including
176 /// transitive dependencies.
177 ///
178 /// This may include modules with a different context hash when it can be
179 /// determined that the differences are benign for this compilation.
180 std::vector<ModuleID> ClangModuleDeps;
181
182 /// A list of module names that are visible to this translation unit. This
183 /// includes both direct and transitive module dependencies.
184 std::vector<std::string> VisibleModules;
185
186 /// A list of the C++20 named modules this translation unit depends on.
187 std::vector<std::string> NamedModuleDeps;
188
189 /// The sequence of commands required to build the translation unit. Commands
190 /// should be executed in order.
191 ///
192 /// FIXME: If we add support for multi-arch builds in clang-scan-deps, we
193 /// should make the dependencies between commands explicit to enable parallel
194 /// builds of each architecture.
195 std::vector<Command> Commands;
196
197 /// Deprecated driver command-line. This will be removed in a future version.
198 std::vector<std::string> DriverCommandLine;
199};
200} // namespace clang::dependencies
201
202namespace llvm {
203inline hash_code hash_value(const clang::dependencies::ModuleID &ID) {
204 return hash_combine(ID.ModuleName, ID.ContextHash);
205}
206
207template <> struct DenseMapInfo<clang::dependencies::ModuleID> {
209 static inline ModuleID getEmptyKey() { return ModuleID{"", ""}; }
210 static inline ModuleID getTombstoneKey() {
211 return ModuleID{"~", "~"}; // ~ is not a valid module name or context hash
212 }
213 static unsigned getHashValue(const ModuleID &ID) { return hash_value(ID); }
214 static bool isEqual(const ModuleID &LHS, const ModuleID &RHS) {
215 return LHS == RHS;
216 }
217};
218} // namespace llvm
219
220#endif // LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYGRAPH_H
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::Module class, which describes a module in the source code.
Same as CompilerInvocation, but with copy-on-write optimization.
std::vector< ModuleDeps > ModuleDepsGraph
Graph of modular dependencies.
The JSON file list parser is used to communicate input to InstallAPI.
@ Other
Other implicit parameter.
Definition Decl.h:1763
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
hash_code hash_value(const clang::dependencies::ModuleID &ID)
A command-line tool invocation that is part of building a TU.
std::vector< std::string > Arguments
std::vector< std::string > ModuleMapFileDeps
A collection of absolute paths to module map files that this module needs to know about.
bool IsInStableDirectories
Whether this module is fully composed of file & module inputs from locations likely to stay the same ...
ModuleID ID
The identifier of the module.
bool IgnoreCWD
Whether current working directory is ignored.
void forEachFileDep(llvm::function_ref< void(StringRef)> Cb) const
Invokes Cb for all file dependencies of this module.
std::vector< PrebuiltModuleDep > PrebuiltModuleDeps
A collection of prebuilt modular dependencies this module directly depends on, not including transiti...
llvm::SmallVector< Module::LinkLibrary, 2 > LinkLibraries
The set of libraries or frameworks to link against when an entity from this module is used.
std::vector< ModuleID > ClangModuleDeps
A list of module identifiers this module directly depends on, not including transitive dependencies.
const std::vector< std::string > & getBuildArguments() const
Get (or compute) the compiler invocation that can be used to build this module.
std::string ClangModuleMapFile
The path to the modulemap file which defines this module.
bool IsSystem
Whether this is a "system" module.
This is used to identify a specific module.
bool operator==(const ModuleID &Other) const
std::string ModuleName
The name of the module.
std::string ContextHash
The context hash of a module represents the compiler options that affect the resulting command-line i...
bool operator<(const ModuleID &Other) const
P1689ModuleInfo - Represents the needed information of standard C++20 modules for P1689 format.
std::string SourcePath
Optional. The source path to the module.
bool IsStdCXXModuleInterface
If this module is a standard c++ interface unit.
std::string ModuleName
The name of the module. This may include : for partitions.
Modular dependency that has already been built prior to the dependency scan.
The full dependencies and module graph for a specific input.
std::vector< std::string > VisibleModules
A list of module names that are visible to this translation unit.
std::vector< std::string > FileDeps
A collection of absolute paths to files that this translation unit directly depends on,...
std::vector< PrebuiltModuleDep > PrebuiltModuleDeps
A collection of prebuilt modules this translation unit directly depends on, not including transitive ...
ModuleID ID
The identifier of the C++20 module this translation unit exports.
std::vector< std::string > DriverCommandLine
Deprecated driver command-line. This will be removed in a future version.
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.
std::vector< Command > Commands
The sequence of commands required to build the translation unit.
std::vector< ModuleID > ClangModuleDeps
A list of modules this translation unit directly depends on, not including transitive dependencies.
static bool isEqual(const ModuleID &LHS, const ModuleID &RHS)
static unsigned getHashValue(const ModuleID &ID)