clang 22.0.0git
DependencyScanningWorker.h
Go to the documentation of this file.
1//===- DependencyScanningWorker.h - clang-scan-deps worker ===---*- C++ -*-===//
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_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
10#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNINGWORKER_H
11
14#include "clang/Basic/LLVM.h"
18#include "llvm/Support/Error.h"
19#include "llvm/Support/FileSystem.h"
20#include "llvm/Support/MemoryBufferRef.h"
21#include <optional>
22#include <string>
23
24namespace clang {
25
27
28namespace tooling {
29namespace dependencies {
30
33
34/// A command-line tool invocation that is part of building a TU.
35///
36/// \see TranslationUnitDeps::Commands.
37struct Command {
38 std::string Executable;
39 std::vector<std::string> Arguments;
40};
41
43public:
45
47 std::optional<P1689ModuleInfo> Provided,
48 std::vector<P1689ModuleInfo> Requires) {}
49
50 virtual void handleBuildCommand(Command Cmd) {}
51
52 virtual void
54
55 virtual void handleFileDependency(StringRef Filename) = 0;
56
58
59 virtual void handleModuleDependency(ModuleDeps MD) = 0;
60
62
63 virtual void handleVisibleModule(std::string ModuleName) = 0;
64
65 virtual void handleContextHash(std::string Hash) = 0;
66};
67
68/// Dependency scanner callbacks that are used during scanning to influence the
69/// behaviour of the scan - for example, to customize the scanned invocations.
71public:
73
74 virtual std::string lookupModuleOutput(const ModuleDeps &MD,
75 ModuleOutputKind Kind) = 0;
76};
77
78/// An individual dependency scanning worker that is able to run on its own
79/// thread.
80///
81/// The worker computes the dependencies for the input files by preprocessing
82/// sources either using a fast mode where the source files are minimized, or
83/// using the regular processing run.
85public:
86 /// Construct a dependency scanning worker.
87 ///
88 /// @param Service The parent service. Must outlive the worker.
89 /// @param FS The filesystem for the worker to use.
92
94
95 /// Run the dependency scanning tool for a given clang driver command-line,
96 /// and report the discovered dependencies to the provided consumer. If
97 /// TUBuffer is not nullopt, it is used as TU input for the dependency
98 /// scanning. Otherwise, the input should be included as part of the
99 /// command-line.
100 ///
101 /// \returns false if clang errors occurred (with diagnostics reported to
102 /// \c DiagConsumer), true otherwise.
104 StringRef WorkingDirectory, const std::vector<std::string> &CommandLine,
105 DependencyConsumer &DepConsumer, DependencyActionController &Controller,
106 DiagnosticConsumer &DiagConsumer,
107 std::optional<llvm::MemoryBufferRef> TUBuffer = std::nullopt);
108
109 /// Run the dependency scanning tool for a given clang driver command-line
110 /// for a specific translation unit via file system or memory buffer.
111 ///
112 /// \returns A \c StringError with the diagnostic output if clang errors
113 /// occurred, success otherwise.
114 llvm::Error computeDependencies(
115 StringRef WorkingDirectory, const std::vector<std::string> &CommandLine,
116 DependencyConsumer &Consumer, DependencyActionController &Controller,
117 std::optional<llvm::MemoryBufferRef> TUBuffer = std::nullopt);
118
119 /// The three method below implements a new interface for by name
120 /// dependency scanning. They together enable the dependency scanning worker
121 /// to more effectively perform scanning for a sequence of modules
122 /// by name when the CWD and CommandLine do not change across the queries.
123
124 /// @brief Initializing the context and the compiler instance.
125 /// @param CWD The current working directory used during the scan.
126 /// @param CommandLine The commandline used for the scan.
127 /// @return Error if the initializaiton fails.
129 StringRef CWD, const std::vector<std::string> &CommandLine);
130
131 /// @brief Performaces dependency scanning for the module whose name is
132 /// specified.
133 /// @param ModuleName The name of the module whose dependency will be
134 /// scanned.
135 /// @param Consumer The dependency consumer that stores the results.
136 /// @param Controller The controller for the dependency scanning action.
137 /// @return Error if the scanner incurs errors.
139 StringRef ModuleName, DependencyConsumer &Consumer,
140 DependencyActionController &Controller);
141
142 /// @brief Finalizes the diagnostics engine and deletes the compiler instance.
143 /// @return Error if errors occur during finalization.
145
146 /// The three methods below provides the same functionality as the
147 /// three methods above. Instead of returning `llvm::Error`s, these
148 /// three methods return a flag to indicate if the call is successful.
149 /// The initialization function asks the client for a DiagnosticsConsumer
150 /// that it direct the diagnostics to.
152 StringRef CWD, const std::vector<std::string> &CommandLine,
153 DiagnosticConsumer *DC = nullptr);
154 bool
155 computeDependenciesByNameWithContext(StringRef ModuleName,
156 DependencyConsumer &Consumer,
157 DependencyActionController &Controller);
159
160 llvm::vfs::FileSystem &getVFS() const { return *BaseFS; }
161
162private:
163 /// The parent dependency scanning service.
165 std::shared_ptr<PCHContainerOperations> PCHContainerOps;
166 /// The file system to be used during the scan.
167 /// This is either \c FS passed in the constructor (when performing canonical
168 /// preprocessing), or \c DepFS (when performing dependency directives scan).
170 /// When performing dependency directives scan, this is the caching (and
171 /// dependency-directives-extracting) filesystem overlaid on top of \c FS
172 /// (passed in the constructor).
174
176 std::unique_ptr<CompilerInstanceWithContext> CIWithContext;
177
178 /// Private helper functions.
179 bool scanDependencies(StringRef WorkingDirectory,
180 const std::vector<std::string> &CommandLine,
181 DependencyConsumer &Consumer,
182 DependencyActionController &Controller,
185};
186
187} // end namespace dependencies
188} // end namespace tooling
189} // end namespace clang
190
191#endif // LLVM_CLANG_TOOLING_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 handleModuleDependency(ModuleDeps MD)=0
virtual void handlePrebuiltModuleDependency(PrebuiltModuleDep PMD)=0
virtual void handleDependencyOutputOpts(const DependencyOutputOptions &Opts)=0
virtual void handleProvidedAndRequiredStdCXXModules(std::optional< P1689ModuleInfo > Provided, std::vector< P1689ModuleInfo > Requires)
virtual void handleDirectModuleDependency(ModuleID MD)=0
virtual void handleVisibleModule(std::string ModuleName)=0
virtual void handleFileDependency(StringRef Filename)=0
virtual void handleContextHash(std::string Hash)=0
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)
llvm::Error finalizeCompilerInstanceWithContextOrError()
Finalizes the diagnostics engine and deletes the compiler instance.
bool initializeCompilerInstanceWithContext(StringRef CWD, const std::vector< std::string > &CommandLine, DiagnosticConsumer *DC=nullptr)
The three methods below provides the same functionality as the three methods above.
bool computeDependencies(StringRef WorkingDirectory, const std::vector< 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...
llvm::Error initializeCompilerInstanceWithContextOrError(StringRef CWD, const std::vector< std::string > &CommandLine)
The three method below implements a new interface for by name dependency scanning.
llvm::Error computeDependenciesByNameWithContextOrError(StringRef ModuleName, DependencyConsumer &Consumer, DependencyActionController &Controller)
Performaces dependency scanning for the module whose name is specified.
DependencyScanningWorker(DependencyScanningService &Service, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS)
Construct a dependency scanning worker.
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.
This is used to identify a specific module.
Modular dependency that has already been built prior to the dependency scan.