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/Support/Error.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 tool for a given clang driver command-line,
97 /// and report the discovered dependencies to the provided consumer. If
98 /// TUBuffer is not nullopt, it is used as TU input for the dependency
99 /// scanning. Otherwise, the input should be included as part of the
100 /// command-line.
101 ///
102 /// \returns false if clang errors occurred (with diagnostics reported to
103 /// \c DiagConsumer), true otherwise.
105 StringRef WorkingDirectory, ArrayRef<std::string> CommandLine,
106 DependencyConsumer &DepConsumer, DependencyActionController &Controller,
107 DiagnosticConsumer &DiagConsumer,
108 std::optional<llvm::MemoryBufferRef> TUBuffer = std::nullopt);
109
110 /// Run the dependency scanning tool for a given clang driver command-line
111 /// for a specific translation unit via file system or memory buffer.
112 ///
113 /// \returns A \c StringError with the diagnostic output if clang errors
114 /// occurred, success otherwise.
115 llvm::Error computeDependencies(
116 StringRef WorkingDirectory, ArrayRef<std::string> CommandLine,
117 DependencyConsumer &Consumer, DependencyActionController &Controller,
118 std::optional<llvm::MemoryBufferRef> TUBuffer = std::nullopt);
119
120 /// The three method below implements a new interface for by name
121 /// dependency scanning. They together enable the dependency scanning worker
122 /// to more effectively perform scanning for a sequence of modules
123 /// by name when the CWD and CommandLine do not change across the queries.
124 /// The initialization function asks the client for a DiagnosticsConsumer
125 /// that it direct the diagnostics to.
126
127 /// @brief Initializing the context and the compiler instance.
128 /// @param CWD The current working directory used during the scan.
129 /// @param CommandLine The commandline used for the scan.
130 /// @return False if the initializaiton fails.
131 bool initializeCompilerInstanceWithContext(StringRef CWD,
132 ArrayRef<std::string> CommandLine,
134
135 /// @brief Initializing the context and the compiler instance.
136 /// @param CWD The current working directory used during the scan.
137 /// @param CommandLine The commandline used for the scan.
138 /// @param DiagEngineWithCmdAndOpts Preconfigured diagnostics engine and
139 /// options associated with the cc1 command line.
140 /// @param FS The overlay file system to use for this compiler instance.
141 /// @return False if the initializaiton fails.
143 StringRef CWD, ArrayRef<std::string> CommandLine,
144 std::unique_ptr<DiagnosticsEngineWithDiagOpts> DiagEngineWithCmdAndOpts,
146
147 /// @brief Performaces dependency scanning for the module whose name is
148 /// specified.
149 /// @param ModuleName The name of the module whose dependency will be
150 /// scanned.
151 /// @param Consumer The dependency consumer that stores the results.
152 /// @param Controller The controller for the dependency scanning action.
153 /// @return False if the scanner incurs errors.
154 bool
155 computeDependenciesByNameWithContext(StringRef ModuleName,
156 DependencyConsumer &Consumer,
157 DependencyActionController &Controller);
158
159 /// @brief Finalizes the diagnostics engine and deletes the compiler instance.
160 /// @return False if errors occur during finalization.
162
163 llvm::vfs::FileSystem &getVFS() const { return *DepFS; }
164
165private:
166 /// The parent dependency scanning service.
168 std::shared_ptr<PCHContainerOperations> PCHContainerOps;
169 /// This is the caching (and optionally dependency-directives-providing) VFS
170 /// overlaid on top of the base VFS passed in the constructor.
172
174 std::unique_ptr<CompilerInstanceWithContext> CIWithContext;
175
176 /// Actually carries out the scan. If \c OverlayFS is provided, it must be
177 /// based on top of DepFS.
178 bool scanDependencies(
179 StringRef WorkingDirectory, ArrayRef<std::string> CommandLine,
180 DependencyConsumer &Consumer, DependencyActionController &Controller,
183};
184
185} // end namespace dependencies
186} // end namespace clang
187
188#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, std::optional< llvm::MemoryBufferRef > TUBuffer=std::nullopt)
Run the dependency scanning tool for a given clang driver 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.
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.