clang 22.0.0git
DependencyScannerImpl.h
Go to the documentation of this file.
1//===- DependencyScannerImpl.h - Implements dependency scanning *- 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_DEPENDENCYSCANNER_H
10#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNER_H
11
19
20namespace clang {
22
23namespace tooling {
24namespace dependencies {
27
31
33public:
35 DependencyScanningService &Service, StringRef WorkingDirectory,
38 std::optional<StringRef> ModuleName = std::nullopt)
39 : Service(Service), WorkingDirectory(WorkingDirectory),
40 Consumer(Consumer), Controller(Controller), DepFS(std::move(DepFS)) {}
41 bool runInvocation(std::unique_ptr<CompilerInvocation> Invocation,
43 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
44 DiagnosticConsumer *DiagConsumer);
45
46 bool hasScanned() const { return Scanned; }
47 bool hasDiagConsumerFinished() const { return DiagConsumerFinished; }
48
49 /// Take the cc1 arguments corresponding to the most recent invocation used
50 /// with this action. Any modifications implied by the discovered dependencies
51 /// will have already been applied.
52 std::vector<std::string> takeLastCC1Arguments() {
53 std::vector<std::string> Result;
54 std::swap(Result, LastCC1Arguments); // Reset LastCC1Arguments to empty.
55 return Result;
56 }
57
58private:
59 void setLastCC1Arguments(CompilerInvocation &&CI) {
60 if (MDC)
61 MDC->applyDiscoveredDependencies(CI);
62 LastCC1Arguments = CI.getCC1CommandLine();
63 }
64
65 DependencyScanningService &Service;
66 StringRef WorkingDirectory;
67 DependencyConsumer &Consumer;
68 DependencyActionController &Controller;
70 std::optional<CompilerInstance> ScanInstanceStorage;
71 std::shared_ptr<ModuleDepCollector> MDC;
72 std::vector<std::string> LastCC1Arguments;
73 bool Scanned = false;
74 bool DiagConsumerFinished = false;
75};
76
77// Helper functions and data types.
78std::unique_ptr<DiagnosticOptions>
80
82 // We need to bound the lifetime of the DiagOpts used to create the
83 // DiganosticsEngine with the DiagnosticsEngine itself.
84 std::unique_ptr<DiagnosticOptions> DiagOpts;
86
90};
91
93 // We need to bound the lifetime of the data that supports the DiagPrinter
94 // with it together so they have the same lifetime.
95 std::string DiagnosticOutput;
96 llvm::raw_string_ostream DiagnosticsOS;
97 std::unique_ptr<DiagnosticOptions> DiagOpts;
99
104};
105
106std::pair<std::unique_ptr<driver::Driver>, std::unique_ptr<driver::Compilation>>
109 llvm::BumpPtrAllocator &Alloc);
110
111std::unique_ptr<CompilerInvocation>
113 DiagnosticsEngine &Diags);
114
115std::pair<IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::vector<std::string>>
117 ArrayRef<std::string> CommandLine,
118 StringRef WorkingDirectory,
119 llvm::MemoryBufferRef TUBuffer);
120
121std::pair<IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>,
122 std::vector<std::string>>
124 ArrayRef<std::string> CommandLine,
125 StringRef WorkingDirectory, StringRef ModuleName);
126
128 CompilerInstance &ScanInstance,
130 DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service,
132
134getInitialStableDirs(const CompilerInstance &ScanInstance);
135
136std::optional<PrebuiltModulesAttrsMap>
138 SmallVector<StringRef> &StableDirs);
139
140std::unique_ptr<DependencyOutputOptions>
142
143/// Create the dependency collector that will collect the produced
144/// dependencies. May return the created ModuleDepCollector depending
145/// on the scanning format.
146std::shared_ptr<ModuleDepCollector> initializeScanInstanceDependencyCollector(
147 CompilerInstance &ScanInstance,
148 std::unique_ptr<DependencyOutputOptions> DepOutputOpts,
149 StringRef WorkingDirectory, DependencyConsumer &Consumer,
150 DependencyScanningService &Service, CompilerInvocation &Inv,
151 DependencyActionController &Controller,
152 PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
153 llvm::SmallVector<StringRef> &StableDirs);
154
156 // Context
158 llvm::StringRef CWD;
159 std::vector<std::string> CommandLine;
160
161 // Context - file systems
163
164 // Context - Diagnostics engine.
165 std::unique_ptr<TextDiagnosticsPrinterWithOutput> DiagPrinterWithOS;
166 // DiagConsumer may points to DiagPrinterWithOS->DiagPrinter, or a custom
167 // DiagnosticConsumer passed in from initialize.
168 DiagnosticConsumer *DiagConsumer = nullptr;
169 std::unique_ptr<DignosticsEngineWithDiagOpts> DiagEngineWithCmdAndOpts;
170
171 // Context - compiler invocation
172 // Compilation's command's arguments may be owned by Alloc when expanded from
173 // response files, so we need to keep Alloc alive in the context.
174 llvm::BumpPtrAllocator Alloc;
175 std::unique_ptr<clang::driver::Driver> Driver;
176 std::unique_ptr<clang::driver::Compilation> Compilation;
177 std::unique_ptr<CompilerInvocation> OriginalInvocation;
178
179 // Context - output options
180 std::unique_ptr<DependencyOutputOptions> OutputOpts;
181
182 // Context - stable directory handling
184 PrebuiltModulesAttrsMap PrebuiltModuleASTMap;
185
186 // Compiler Instance
187 std::unique_ptr<CompilerInstance> CIPtr;
188
189 // Source location offset.
190 int32_t SrcLocOffset = 0;
191
192public:
194 const std::vector<std::string> &CMD)
195 : Worker(Worker), CWD(CWD), CommandLine(CMD) {};
196
197 // The three methods below returns false when they fail, with the detail
198 // accumulated in DiagConsumer.
200 bool computeDependencies(StringRef ModuleName, DependencyConsumer &Consumer,
201 DependencyActionController &Controller);
202 bool finalize();
203
204 // The method below turns the return status from the above methods
205 // into an llvm::Error using a default DiagnosticConsumer.
206 llvm::Error handleReturnStatus(bool Success);
207};
208} // namespace dependencies
209} // namespace tooling
210} // namespace clang
211
212#endif
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Helper class for holding the data necessary to invoke the compiler.
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:232
bool computeDependencies(StringRef ModuleName, DependencyConsumer &Consumer, DependencyActionController &Controller)
CompilerInstanceWithContext(DependencyScanningWorker &Worker, StringRef CWD, const std::vector< std::string > &CMD)
Dependency scanner callbacks that are used during scanning to influence the behaviour of the scan - f...
bool runInvocation(std::unique_ptr< CompilerInvocation > Invocation, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticConsumer *DiagConsumer)
std::vector< std::string > takeLastCC1Arguments()
Take the cc1 arguments corresponding to the most recent invocation used with this action.
DependencyScanningAction(DependencyScanningService &Service, StringRef WorkingDirectory, DependencyConsumer &Consumer, DependencyActionController &Controller, IntrusiveRefCntPtr< DependencyScanningWorkerFilesystem > DepFS, std::optional< StringRef > ModuleName=std::nullopt)
The dependency scanning service contains shared configuration and state that is used by the individua...
A virtual file system optimized for the dependency discovery.
An individual dependency scanning worker that is able to run on its own thread.
llvm::SmallVector< StringRef > getInitialStableDirs(const CompilerInstance &ScanInstance)
std::unique_ptr< DiagnosticOptions > createDiagOptions(ArrayRef< std::string > CommandLine)
bool initializeScanCompilerInstance(CompilerInstance &ScanInstance, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS, DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service, IntrusiveRefCntPtr< DependencyScanningWorkerFilesystem > DepFS)
std::pair< IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem >, std::vector< std::string > > initVFSForByNameScanning(IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS, ArrayRef< std::string > CommandLine, StringRef WorkingDirectory, StringRef ModuleName)
std::unique_ptr< CompilerInvocation > createCompilerInvocation(ArrayRef< std::string > CommandLine, DiagnosticsEngine &Diags)
std::shared_ptr< ModuleDepCollector > initializeScanInstanceDependencyCollector(CompilerInstance &ScanInstance, std::unique_ptr< DependencyOutputOptions > DepOutputOpts, StringRef WorkingDirectory, DependencyConsumer &Consumer, DependencyScanningService &Service, CompilerInvocation &Inv, DependencyActionController &Controller, PrebuiltModulesAttrsMap PrebuiltModulesASTMap, llvm::SmallVector< StringRef > &StableDirs)
Create the dependency collector that will collect the produced dependencies.
std::unique_ptr< DependencyOutputOptions > takeAndUpdateDependencyOutputOptionsFrom(CompilerInstance &ScanInstance)
std::optional< PrebuiltModulesAttrsMap > computePrebuiltModulesASTMap(CompilerInstance &ScanInstance, llvm::SmallVector< StringRef > &StableDirs)
llvm::StringMap< PrebuiltModuleASTAttrs > PrebuiltModulesAttrsMap
Attributes loaded from AST files of prebuilt modules collected prior to ModuleDepCollector creation.
std::pair< IntrusiveRefCntPtr< llvm::vfs::FileSystem >, std::vector< std::string > > initVFSForTUBuferScanning(IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS, ArrayRef< std::string > CommandLine, StringRef WorkingDirectory, llvm::MemoryBufferRef TUBuffer)
std::pair< std::unique_ptr< driver::Driver >, std::unique_ptr< driver::Compilation > > buildCompilation(ArrayRef< std::string > ArgStrs, DiagnosticsEngine &Diags, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS, llvm::BumpPtrAllocator &Alloc)
The JSON file list parser is used to communicate input to InstallAPI.
@ Success
Annotation was successful.
Definition Parser.h:65
@ Result
The result type of a method or function.
Definition TypeBase.h:905
DignosticsEngineWithDiagOpts(ArrayRef< std::string > CommandLine, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS, DiagnosticConsumer &DC)