clang 22.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 dependencies {
31
34
35/// A command-line tool invocation that is part of building a TU.
36///
37/// \see TranslationUnitDeps::Commands.
38struct Command {
39 std::string Executable;
40 std::vector<std::string> Arguments;
41};
42
44public:
46
48 std::optional<P1689ModuleInfo> Provided,
49 std::vector<P1689ModuleInfo> Requires) {}
50
51 virtual void handleBuildCommand(Command Cmd) {}
52
53 virtual void
55
56 virtual void handleFileDependency(StringRef Filename) = 0;
57
59
60 virtual void handleModuleDependency(ModuleDeps MD) = 0;
61
63
64 virtual void handleVisibleModule(std::string ModuleName) = 0;
65
66 virtual void handleContextHash(std::string Hash) = 0;
67};
68
69/// Dependency scanner callbacks that are used during scanning to influence the
70/// behaviour of the scan - for example, to customize the scanned invocations.
72public:
74
75 virtual std::string lookupModuleOutput(const ModuleDeps &MD,
76 ModuleOutputKind Kind) = 0;
77};
78
79/// An individual dependency scanning worker that is able to run on its own
80/// thread.
81///
82/// The worker computes the dependencies for the input files by preprocessing
83/// sources either using a fast mode where the source files are minimized, or
84/// using the regular processing run.
86public:
87 /// Construct a dependency scanning worker.
88 ///
89 /// @param Service The parent service. Must outlive the worker.
90 /// @param BaseFS The filesystem for the worker to use.
93
95
96 /// Run the dependency scanning worker for the given frontend command-line,
97 /// and report the discovered dependencies to the provided consumer.
98 ///
99 /// OverlayFS should be based on the Worker's dependency scanning file-system
100 /// and can be used to provide any input specified on the command-line as
101 /// in-memory file. If no overlay file-system is provided, the Worker's
102 /// dependency scanning file-system is used instead.
103 ///
104 /// \returns false if any errors occurred (with diagnostics reported to
105 /// \c DiagConsumer), true otherwise.
107 StringRef WorkingDirectory, ArrayRef<std::string> CommandLine,
108 DependencyConsumer &DepConsumer, DependencyActionController &Controller,
109 DiagnosticConsumer &DiagConsumer,
111 nullptr);
112
113 /// Run the dependency scanning tool for all given frontend command-lines,
114 /// and report the discovered dependencies to the provided consumer.
115 ///
116 /// OverlayFS should be based on the Worker's dependency scanning file-system
117 /// and can be used to provide any input specified on the command-line as
118 /// in-memory file. If no overlay file-system is provided, the Worker's
119 /// dependency scanning file-system is used instead.
120 ///
121 /// \returns false if any errors occurred (with diagnostics reported to
122 /// \c DiagConsumer), true otherwise.
124 StringRef WorkingDirectory, ArrayRef<ArrayRef<std::string>> CommandLines,
125 DependencyConsumer &DepConsumer, DependencyActionController &Controller,
126 DiagnosticConsumer &DiagConsumer,
128 nullptr);
129
130 /// The three method below implements a new interface for by name
131 /// dependency scanning. They together enable the dependency scanning worker
132 /// to more effectively perform scanning for a sequence of modules
133 /// by name when the CWD and CommandLine do not change across the queries.
134 /// The initialization function asks the client for a DiagnosticsConsumer
135 /// that it direct the diagnostics to.
136
137 /// @brief Initializing the context and the compiler instance.
138 /// @param CWD The current working directory used during the scan.
139 /// @param CommandLine The commandline used for the scan.
140 /// @return False if the initializaiton fails.
141 bool initializeCompilerInstanceWithContext(StringRef CWD,
142 ArrayRef<std::string> CommandLine,
144
145 /// @brief Initializing the context and the compiler instance.
146 /// @param CWD The current working directory used during the scan.
147 /// @param CommandLine The commandline used for the scan.
148 /// @param DiagEngineWithCmdAndOpts Preconfigured diagnostics engine and
149 /// options associated with the cc1 command line.
150 /// @param FS The overlay file system to use for this compiler instance.
151 /// @return False if the initializaiton fails.
153 StringRef CWD, ArrayRef<std::string> CommandLine,
154 std::unique_ptr<DiagnosticsEngineWithDiagOpts> DiagEngineWithCmdAndOpts,
156
157 /// @brief Performaces dependency scanning for the module whose name is
158 /// specified.
159 /// @param ModuleName The name of the module whose dependency will be
160 /// scanned.
161 /// @param Consumer The dependency consumer that stores the results.
162 /// @param Controller The controller for the dependency scanning action.
163 /// @return False if the scanner incurs errors.
164 bool
165 computeDependenciesByNameWithContext(StringRef ModuleName,
166 DependencyConsumer &Consumer,
167 DependencyActionController &Controller);
168
169 /// @brief Finalizes the diagnostics engine and deletes the compiler instance.
170 /// @return False if errors occur during finalization.
172
173 llvm::vfs::FileSystem &getVFS() const { return *DepFS; }
174
175private:
176 /// The parent dependency scanning service.
178 std::shared_ptr<PCHContainerOperations> PCHContainerOps;
179 /// This is the caching (and optionally dependency-directives-providing) VFS
180 /// overlaid on top of the base VFS passed in the constructor.
182
184 std::unique_ptr<CompilerInstanceWithContext> CIWithContext;
185};
186
187std::pair<IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>,
188 std::vector<std::string>>
190 ArrayRef<std::string> CommandLine,
191 StringRef WorkingDirectory,
192 llvm::MemoryBufferRef TUBuffer);
193
194std::pair<IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>,
195 std::vector<std::string>>
197 ArrayRef<std::string> CommandLine,
198 StringRef WorkingDirectory, StringRef ModuleName);
199
200} // end namespace dependencies
201} // end namespace clang
202
203#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.
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::string lookupModuleOutput(const ModuleDeps &MD, ModuleOutputKind Kind)=0
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...
A virtual file system optimized for the dependency discovery.
bool computeDependenciesByNameWithContext(StringRef ModuleName, DependencyConsumer &Consumer, DependencyActionController &Controller)
Performaces dependency scanning for the module whose name is specified.
DependencyScanningWorker(DependencyScanningService &Service, IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS)
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...
bool initializeCompilerInstanceWithContext(StringRef CWD, ArrayRef< std::string > CommandLine, DiagnosticConsumer &DC)
The three method below implements a new interface for by name dependency scanning.
bool finalizeCompilerInstanceWithContext()
Finalizes the diagnostics engine and deletes the compiler instance.
std::pair< IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem >, std::vector< std::string > > initVFSForTUBufferScanning(IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS, ArrayRef< std::string > CommandLine, StringRef WorkingDirectory, llvm::MemoryBufferRef TUBuffer)
ModuleOutputKind
An output from a module compilation, such as the path of the module file.
std::pair< IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem >, std::vector< std::string > > initVFSForByNameScanning(IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS, ArrayRef< std::string > CommandLine, StringRef WorkingDirectory, StringRef ModuleName)
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.