clang 22.0.0git
DependencyScanningUtils.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_DEPENDENCYSCANNINGUTILS_H
10#define LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGUTILS_H
11
15#include "llvm/ADT/DenseSet.h"
16#include "llvm/ADT/MapVector.h"
17#include <string>
18#include <vector>
19
20namespace clang {
21namespace dependencies {
22
23/// Graph of modular dependencies.
24using ModuleDepsGraph = std::vector<ModuleDeps>;
25
26/// The full dependencies and module graph for a specific input.
28 /// The graph of direct and transitive modular dependencies.
30
31 /// The identifier of the C++20 module this translation unit exports.
32 ///
33 /// If the translation unit is not a module then \c ID.ModuleName is empty.
35
36 /// A collection of absolute paths to files that this translation unit
37 /// directly depends on, not including transitive dependencies.
38 std::vector<std::string> FileDeps;
39
40 /// A collection of prebuilt modules this translation unit directly depends
41 /// on, not including transitive dependencies.
42 std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;
43
44 /// A list of modules this translation unit directly depends on, not including
45 /// transitive dependencies.
46 ///
47 /// This may include modules with a different context hash when it can be
48 /// determined that the differences are benign for this compilation.
49 std::vector<ModuleID> ClangModuleDeps;
50
51 /// A list of module names that are visible to this translation unit. This
52 /// includes both direct and transitive module dependencies.
53 std::vector<std::string> VisibleModules;
54
55 /// A list of the C++20 named modules this translation unit depends on.
56 std::vector<std::string> NamedModuleDeps;
57
58 /// The sequence of commands required to build the translation unit. Commands
59 /// should be executed in order.
60 ///
61 /// FIXME: If we add support for multi-arch builds in clang-scan-deps, we
62 /// should make the dependencies between commands explicit to enable parallel
63 /// builds of each architecture.
64 std::vector<Command> Commands;
65
66 /// Deprecated driver command-line. This will be removed in a future version.
67 std::vector<std::string> DriverCommandLine;
68};
69
71public:
72 FullDependencyConsumer(const llvm::DenseSet<ModuleID> &AlreadySeen)
73 : AlreadySeen(AlreadySeen) {}
74
75 void handleBuildCommand(Command Cmd) override {
76 Commands.push_back(std::move(Cmd));
77 }
78
80
81 void handleFileDependency(StringRef File) override {
82 Dependencies.push_back(std::string(File));
83 }
84
86 PrebuiltModuleDeps.emplace_back(std::move(PMD));
87 }
88
90 ClangModuleDeps[MD.ID] = std::move(MD);
91 }
92
94 DirectModuleDeps.push_back(ID);
95 }
96
97 void handleVisibleModule(std::string ModuleName) override {
98 VisibleModules.push_back(ModuleName);
99 }
100
101 void handleContextHash(std::string Hash) override {
102 ContextHash = std::move(Hash);
103 }
104
106 std::optional<P1689ModuleInfo> Provided,
107 std::vector<P1689ModuleInfo> Requires) override {
108 ModuleName = Provided ? Provided->ModuleName : "";
109 llvm::transform(Requires, std::back_inserter(NamedModuleDeps),
110 [](const auto &Module) { return Module.ModuleName; });
111 }
112
114
115private:
116 std::vector<std::string> Dependencies;
117 std::vector<PrebuiltModuleDep> PrebuiltModuleDeps;
118 llvm::MapVector<ModuleID, ModuleDeps> ClangModuleDeps;
119 std::string ModuleName;
120 std::vector<std::string> NamedModuleDeps;
121 std::vector<ModuleID> DirectModuleDeps;
122 std::vector<std::string> VisibleModules;
123 std::vector<Command> Commands;
124 std::string ContextHash;
125 const llvm::DenseSet<ModuleID> &AlreadySeen;
126};
127
128/// A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
130 llvm::function_ref<std::string(const ModuleDeps &, ModuleOutputKind)>;
131
132/// A simple dependency action controller that uses a callback. If no callback
133/// is provided, it is assumed that looking up module outputs is unreachable.
135public:
137
138 static std::string lookupUnreachableModuleOutput(const ModuleDeps &MD,
139 ModuleOutputKind Kind) {
140 llvm::report_fatal_error("unexpected call to lookupModuleOutput");
141 };
142
144 : LookupModuleOutput(std::move(LMO)) {
145 if (!LookupModuleOutput) {
146 LookupModuleOutput = lookupUnreachableModuleOutput;
147 }
148 }
149
150 std::string lookupModuleOutput(const ModuleDeps &MD,
151 ModuleOutputKind Kind) override {
152 return LookupModuleOutput(MD, Kind);
153 }
154
155private:
156 LookupModuleOutputCallback LookupModuleOutput;
157};
158
159} // end namespace dependencies
160} // end namespace clang
161
162#endif // LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGUTILS_H
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
Describes a module or submodule.
Definition Module.h:144
static std::string lookupUnreachableModuleOutput(const ModuleDeps &MD, ModuleOutputKind Kind)
CallbackActionController(LookupModuleOutputCallback LMO)
std::string lookupModuleOutput(const ModuleDeps &MD, ModuleOutputKind Kind) override
Dependency scanner callbacks that are used during scanning to influence the behaviour of the scan - f...
void handleDependencyOutputOpts(const DependencyOutputOptions &) override
void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD) override
FullDependencyConsumer(const llvm::DenseSet< ModuleID > &AlreadySeen)
void handleProvidedAndRequiredStdCXXModules(std::optional< P1689ModuleInfo > Provided, std::vector< P1689ModuleInfo > Requires) override
void handleVisibleModule(std::string ModuleName) override
llvm::function_ref< std::string(const ModuleDeps &, ModuleOutputKind)> LookupModuleOutputCallback
A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
ModuleOutputKind
An output from a module compilation, such as the path of the module file.
std::vector< ModuleDeps > ModuleDepsGraph
Graph of modular dependencies.
The JSON file list parser is used to communicate input to InstallAPI.
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.
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.