clang 23.0.0git
DependencyScanningWorker.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_DEPENDENCYSCANNINGWORKER_H
10#define LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
11
14#include "clang/Basic/LLVM.h"
19#include "llvm/ADT/IntrusiveRefCntPtr.h"
20#include "llvm/Support/FileSystem.h"
21#include "llvm/Support/MemoryBufferRef.h"
22#include "llvm/Support/VirtualFileSystem.h"
23#include <optional>
24#include <string>
25
26namespace clang {
27
29
30namespace tooling {
32}
33
34namespace dependencies {
35
36class DependencyScanningWorkerFilesystem;
37
38/// A command-line tool invocation that is part of building a TU.
39///
40/// \see TranslationUnitDeps::Commands.
41struct Command {
42 std::string Executable;
43 std::vector<std::string> Arguments;
44};
45
47public:
49
51 std::optional<P1689ModuleInfo> Provided,
52 std::vector<P1689ModuleInfo> Requires) {}
53
54 virtual void handleBuildCommand(Command Cmd) {}
55
56 virtual void
58
59 virtual void handleFileDependency(StringRef Filename) = 0;
60
62
63 virtual void handleModuleDependency(ModuleDeps MD) = 0;
64
66
67 virtual void handleVisibleModule(std::string ModuleName) = 0;
68
69 virtual void handleContextHash(std::string Hash) = 0;
70};
71
72/// Dependency scanner callbacks that are used during scanning to influence the
73/// behaviour of the scan - for example, to customize the scanned invocations.
75public:
77
78 /// Creates a copy of the controller. The result must be both thread-safe.
79 virtual std::unique_ptr<DependencyActionController> clone() const = 0;
80
81 /// Provides output path for a given module dependency. Must be thread-safe.
82 virtual std::string lookupModuleOutput(const ModuleDeps &MD,
83 ModuleOutputKind Kind) = 0;
84
85 /// Initializes the scan invocation.
86 virtual void initializeScanInvocation(CompilerInvocation &ScanInvocation) {}
87
88 /// Initializes the scan instance and modifies the resulting TU invocation.
89 /// Returns true on success, false on failure.
90 virtual bool initialize(CompilerInstance &ScanInstance,
91 CompilerInvocation &NewInvocation) {
92 return true;
93 }
94
95 /// Finalizes the scan instance and modifies the resulting TU invocation.
96 /// Returns true on success, false on failure.
97 virtual bool finalize(CompilerInstance &ScanInstance,
98 CompilerInvocation &NewInvocation) {
99 return true;
100 }
101
102 /// Returns the cache key for the resulting invocation, or nullopt.
103 virtual std::optional<std::string>
104 getCacheKey(const CompilerInvocation &NewInvocation) {
105 return std::nullopt;
106 }
107
108 /// Initializes the module scan instance.
109 /// Returns true on success, false on failure.
110 virtual bool initializeModuleBuild(CompilerInstance &ModuleScanInstance) {
111 return true;
112 }
113
114 /// Finalizes the module scan instance.
115 /// Returns true on success, false on failure.
116 virtual bool finalizeModuleBuild(CompilerInstance &ModuleScanInstance) {
117 return true;
118 }
119
120 /// Modifies the resulting module invocation and the associated structure.
121 /// Returns true on success, false on failure.
122 virtual bool finalizeModuleInvocation(CompilerInstance &ScanInstance,
124 const ModuleDeps &MD) {
125 return true;
126 }
127};
128
129/// An individual dependency scanning worker that is able to run on its own
130/// thread.
131///
132/// The worker computes the dependencies for the input files by preprocessing
133/// sources either using a fast mode where the source files are minimized, or
134/// using the regular processing run.
136public:
137 /// Construct a dependency scanning worker.
138 ///
139 /// @param Service The parent service. Must outlive the worker.
141
143
144 /// Run the dependency scanning worker for the given frontend command-line,
145 /// and report the discovered dependencies to the provided consumer.
146 ///
147 /// OverlayFS should be based on the Worker's dependency scanning file-system
148 /// and can be used to provide any input specified on the command-line as
149 /// in-memory file. If no overlay file-system is provided, the Worker's
150 /// dependency scanning file-system is used instead.
151 ///
152 /// \returns false if any errors occurred (with diagnostics reported to
153 /// \c DiagConsumer), true otherwise.
155 StringRef WorkingDirectory, ArrayRef<std::string> CommandLine,
156 DependencyConsumer &DepConsumer, DependencyActionController &Controller,
157 DiagnosticConsumer &DiagConsumer,
159 nullptr);
160
161 /// Run the dependency scanning tool for all given frontend command-lines,
162 /// and report the discovered dependencies to the provided consumer.
163 ///
164 /// OverlayFS should be based on the Worker's dependency scanning file-system
165 /// and can be used to provide any input specified on the command-line as
166 /// in-memory file. If no overlay file-system is provided, the Worker's
167 /// dependency scanning file-system is used instead.
168 ///
169 /// \returns false if any errors occurred (with diagnostics reported to
170 /// \c DiagConsumer), true otherwise.
172 StringRef WorkingDirectory, ArrayRef<ArrayRef<std::string>> CommandLines,
173 DependencyConsumer &DepConsumer, DependencyActionController &Controller,
174 DiagnosticConsumer &DiagConsumer,
176 nullptr);
177
178 llvm::vfs::FileSystem &getVFS() const { return *DepFS; }
179
180private:
181 /// The parent dependency scanning service.
183 std::shared_ptr<PCHContainerOperations> PCHContainerOps;
184 /// This is the caching (and optionally dependency-directives-providing) VFS
185 /// overlaid on top of the base VFS passed in the constructor.
187
189};
190} // end namespace dependencies
191} // end namespace clang
192
193#endif // LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
Defines the clang::FileManager interface and associated types.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Helper class for holding the data necessary to invoke the compiler.
Same as CompilerInvocation, but with copy-on-write optimization.
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Dependency scanner callbacks that are used during scanning to influence the behaviour of the scan - f...
virtual std::unique_ptr< DependencyActionController > clone() const =0
Creates a copy of the controller. The result must be both thread-safe.
virtual bool finalizeModuleInvocation(CompilerInstance &ScanInstance, CowCompilerInvocation &CI, const ModuleDeps &MD)
Modifies the resulting module invocation and the associated structure.
virtual void initializeScanInvocation(CompilerInvocation &ScanInvocation)
Initializes the scan invocation.
virtual std::string lookupModuleOutput(const ModuleDeps &MD, ModuleOutputKind Kind)=0
Provides output path for a given module dependency. Must be thread-safe.
virtual std::optional< std::string > getCacheKey(const CompilerInvocation &NewInvocation)
Returns the cache key for the resulting invocation, or nullopt.
virtual bool initialize(CompilerInstance &ScanInstance, CompilerInvocation &NewInvocation)
Initializes the scan instance and modifies the resulting TU invocation.
virtual bool initializeModuleBuild(CompilerInstance &ModuleScanInstance)
Initializes the module scan instance.
virtual bool finalize(CompilerInstance &ScanInstance, CompilerInvocation &NewInvocation)
Finalizes the scan instance and modifies the resulting TU invocation.
virtual bool finalizeModuleBuild(CompilerInstance &ModuleScanInstance)
Finalizes the module scan instance.
virtual void handleFileDependency(StringRef Filename)=0
virtual void handleDependencyOutputOpts(const DependencyOutputOptions &Opts)=0
virtual void handleModuleDependency(ModuleDeps MD)=0
virtual void handleVisibleModule(std::string ModuleName)=0
virtual void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD)=0
virtual void handleDirectModuleDependency(ModuleID MD)=0
virtual void handleContextHash(std::string Hash)=0
virtual void handleProvidedAndRequiredStdCXXModules(std::optional< P1689ModuleInfo > Provided, std::vector< P1689ModuleInfo > Requires)
The dependency scanning service contains shared configuration and state that is used by the individua...
DependencyScanningWorker(DependencyScanningService &Service)
Construct a dependency scanning worker.
bool computeDependencies(StringRef WorkingDirectory, ArrayRef< std::string > CommandLine, DependencyConsumer &DepConsumer, DependencyActionController &Controller, DiagnosticConsumer &DiagConsumer, llvm::IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem > OverlayFS=nullptr)
Run the dependency scanning worker for the given frontend command-line, and report the discovered dep...
ModuleOutputKind
An output from a module compilation, such as the path of the module file.
The JSON file list parser is used to communicate input to InstallAPI.
A command-line tool invocation that is part of building a TU.
std::vector< std::string > Arguments
This is used to identify a specific module.
Modular dependency that has already been built prior to the dependency scan.