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<clang::dependencies::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<clang::dependencies::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<clang::dependencies::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<clang::dependencies::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:
73 const llvm::DenseSet<clang::dependencies::ModuleID> &AlreadySeen)
74 : AlreadySeen(AlreadySeen) {}
75
77 Commands.push_back(std::move(Cmd));
78 }
79
81
82 void handleFileDependency(StringRef File) override {
83 Dependencies.push_back(std::string(File));
84 }
85
88 PrebuiltModuleDeps.emplace_back(std::move(PMD));
89 }
90
92 ClangModuleDeps[MD.ID] = std::move(MD);
93 }
94
96 DirectModuleDeps.push_back(ID);
97 }
98
99 void handleVisibleModule(std::string ModuleName) override {
100 VisibleModules.push_back(ModuleName);
101 }
102
103 void handleContextHash(std::string Hash) override {
104 ContextHash = std::move(Hash);
105 }
106
108 std::optional<clang::dependencies::P1689ModuleInfo> Provided,
109 std::vector<clang::dependencies::P1689ModuleInfo> Requires) override {
110 ModuleName = Provided ? Provided->ModuleName : "";
111 llvm::transform(Requires, std::back_inserter(NamedModuleDeps),
112 [](const auto &Module) { return Module.ModuleName; });
113 }
114
116
117private:
118 std::vector<std::string> Dependencies;
119 std::vector<clang::dependencies::PrebuiltModuleDep> PrebuiltModuleDeps;
120 llvm::MapVector<clang::dependencies::ModuleID,
122 ClangModuleDeps;
123 std::string ModuleName;
124 std::vector<std::string> NamedModuleDeps;
125 std::vector<clang::dependencies::ModuleID> DirectModuleDeps;
126 std::vector<std::string> VisibleModules;
127 std::vector<clang::dependencies::Command> Commands;
128 std::string ContextHash;
129 const llvm::DenseSet<clang::dependencies::ModuleID> &AlreadySeen;
130};
131
132/// A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
134 llvm::function_ref<std::string(const clang::dependencies::ModuleDeps &,
136
137/// A simple dependency action controller that uses a callback. If no callback
138/// is provided, it is assumed that looking up module outputs is unreachable.
141public:
143
144 static std::string
147 llvm::report_fatal_error("unexpected call to lookupModuleOutput");
148 };
149
151 : LookupModuleOutput(std::move(LMO)) {
152 if (!LookupModuleOutput) {
153 LookupModuleOutput = lookupUnreachableModuleOutput;
154 }
155 }
156
157 std::string
160 return LookupModuleOutput(MD, Kind);
161 }
162
163private:
164 LookupModuleOutputCallback LookupModuleOutput;
165};
166
167} // end namespace dependencies
168} // end namespace clang
169
170#endif // LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGUTILS_H
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
Describes a module or submodule.
Definition Module.h:144
CallbackActionController(LookupModuleOutputCallback LMO)
std::string lookupModuleOutput(const clang::dependencies::ModuleDeps &MD, clang::dependencies::ModuleOutputKind Kind) override
static std::string lookupUnreachableModuleOutput(const clang::dependencies::ModuleDeps &MD, clang::dependencies::ModuleOutputKind Kind)
Dependency scanner callbacks that are used during scanning to influence the behaviour of the scan - f...
void handleModuleDependency(clang::dependencies::ModuleDeps MD) override
void handleDependencyOutputOpts(const DependencyOutputOptions &) override
void handleDirectModuleDependency(clang::dependencies::ModuleID ID) override
FullDependencyConsumer(const llvm::DenseSet< clang::dependencies::ModuleID > &AlreadySeen)
void handleProvidedAndRequiredStdCXXModules(std::optional< clang::dependencies::P1689ModuleInfo > Provided, std::vector< clang::dependencies::P1689ModuleInfo > Requires) override
void handlePrebuiltModuleDependency(clang::dependencies::PrebuiltModuleDep PMD) override
void handleBuildCommand(clang::dependencies::Command Cmd) override
void handleVisibleModule(std::string ModuleName) override
std::vector< clang::dependencies::ModuleDeps > ModuleDepsGraph
Graph of modular dependencies.
ModuleOutputKind
An output from a module compilation, such as the path of the module file.
llvm::function_ref< std::string(const clang::dependencies::ModuleDeps &, clang::dependencies::ModuleOutputKind)> LookupModuleOutputCallback
A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
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.
clang::dependencies::ModuleID ID
The identifier of the C++20 module this translation unit exports.
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< std::string > DriverCommandLine
Deprecated driver command-line. This will be removed in a future version.
std::vector< clang::dependencies::ModuleID > ClangModuleDeps
A list of modules this translation unit directly depends on, not including transitive dependencies.
std::vector< std::string > NamedModuleDeps
A list of the C++20 named modules this translation unit depends on.
std::vector< clang::dependencies::PrebuiltModuleDep > PrebuiltModuleDeps
A collection of prebuilt modules this translation unit directly depends on, not including transitive ...
std::vector< clang::dependencies::Command > Commands
The sequence of commands required to build the translation unit.
ModuleDepsGraph ModuleGraph
The graph of direct and transitive modular dependencies.