clang 22.0.0git
DependencyScannerImpl.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_DEPENDENCYSCANNERIMPL_H
10#define LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNERIMPL_H
11
15#include "clang/Driver/Driver.h"
20#include "llvm/Support/VirtualFileSystem.h"
21
22namespace clang {
24
25namespace dependencies {
28
32
34public:
36 DependencyScanningService &Service, StringRef WorkingDirectory,
39 std::optional<StringRef> ModuleName = std::nullopt)
40 : Service(Service), WorkingDirectory(WorkingDirectory),
41 Consumer(Consumer), Controller(Controller), DepFS(std::move(DepFS)) {}
42 bool runInvocation(std::string Executable,
43 std::unique_ptr<CompilerInvocation> Invocation,
45 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
46 DiagnosticConsumer *DiagConsumer);
47
48 bool hasScanned() const { return Scanned; }
49 bool hasDiagConsumerFinished() const { return DiagConsumerFinished; }
50
51private:
53 StringRef WorkingDirectory;
54 DependencyConsumer &Consumer;
57 std::optional<CompilerInstance> ScanInstanceStorage;
58 std::shared_ptr<ModuleDepCollector> MDC;
59 bool Scanned = false;
60 bool DiagConsumerFinished = false;
61};
62
63// Helper functions and data types.
64std::unique_ptr<DiagnosticOptions>
66
68 // We need to bound the lifetime of the DiagOpts used to create the
69 // DiganosticsEngine with the DiagnosticsEngine itself.
70 std::unique_ptr<DiagnosticOptions> DiagOpts;
72
76};
77
79 // We need to bound the lifetime of the data that supports the DiagPrinter
80 // with it together so they have the same lifetime.
81 std::string DiagnosticOutput;
82 llvm::raw_string_ostream DiagnosticsOS;
83 std::unique_ptr<DiagnosticOptions> DiagOpts;
85
90};
91
92std::pair<std::unique_ptr<driver::Driver>, std::unique_ptr<driver::Compilation>>
95 llvm::BumpPtrAllocator &Alloc);
96
97std::unique_ptr<CompilerInvocation>
99 DiagnosticsEngine &Diags);
100
101std::pair<IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>,
102 std::vector<std::string>>
104 ArrayRef<std::string> CommandLine,
105 StringRef WorkingDirectory,
106 llvm::MemoryBufferRef TUBuffer);
107
108std::pair<IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem>,
109 std::vector<std::string>>
111 ArrayRef<std::string> CommandLine,
112 StringRef WorkingDirectory, StringRef ModuleName);
113
115 CompilerInstance &ScanInstance,
117 DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service,
119
121getInitialStableDirs(const CompilerInstance &ScanInstance);
122
123std::optional<PrebuiltModulesAttrsMap>
125 SmallVector<StringRef> &StableDirs);
126
127/// Create the dependency collector that will collect the produced
128/// dependencies. May return the created ModuleDepCollector depending
129/// on the scanning format.
130std::shared_ptr<ModuleDepCollector> initializeScanInstanceDependencyCollector(
131 CompilerInstance &ScanInstance,
132 std::unique_ptr<DependencyOutputOptions> DepOutputOpts,
133 StringRef WorkingDirectory, DependencyConsumer &Consumer,
134 DependencyScanningService &Service, CompilerInvocation &Inv,
135 DependencyActionController &Controller,
136 PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
137 llvm::SmallVector<StringRef> &StableDirs);
138
140 // Context
142 llvm::StringRef CWD;
143 std::vector<std::string> CommandLine;
144
145 // Context - Diagnostics engine.
146 DiagnosticConsumer *DiagConsumer = nullptr;
147 std::unique_ptr<DiagnosticsEngineWithDiagOpts> DiagEngineWithCmdAndOpts;
148
149 // Context - compiler invocation
150 std::unique_ptr<CompilerInvocation> OriginalInvocation;
151
152 // Context - output options
153 std::unique_ptr<DependencyOutputOptions> OutputOpts;
154
155 // Context - stable directory handling
157 PrebuiltModulesAttrsMap PrebuiltModuleASTMap;
158
159 // Compiler Instance
160 std::unique_ptr<CompilerInstance> CIPtr;
161
162 // Source location offset.
163 int32_t SrcLocOffset = 0;
164
165public:
167 const std::vector<std::string> &CMD)
168 : Worker(Worker), CWD(CWD), CommandLine(CMD) {};
169
170 // The three methods below returns false when they fail, with the detail
171 // accumulated in \c DiagEngineWithDiagOpts's diagnostic consumer.
172 bool initialize(
173 std::unique_ptr<DiagnosticsEngineWithDiagOpts> DiagEngineWithDiagOpts,
175 bool computeDependencies(StringRef ModuleName, DependencyConsumer &Consumer,
176 DependencyActionController &Controller);
177 bool finalize();
178};
179} // namespace dependencies
180} // namespace clang
181
182#endif // LLVM_CLANG_DEPENDENCYSCANNING_DEPENDENCYSCANNERIMPL_H
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 initialize(std::unique_ptr< DiagnosticsEngineWithDiagOpts > DiagEngineWithDiagOpts, IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem > OverlayFS)
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...
DependencyScanningAction(DependencyScanningService &Service, StringRef WorkingDirectory, DependencyConsumer &Consumer, DependencyActionController &Controller, IntrusiveRefCntPtr< DependencyScanningWorkerFilesystem > DepFS, std::optional< StringRef > ModuleName=std::nullopt)
bool runInvocation(std::string Executable, std::unique_ptr< CompilerInvocation > Invocation, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticConsumer *DiagConsumer)
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.
SmallVector< StringRef > getInitialStableDirs(const CompilerInstance &ScanInstance)
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)
void initializeScanCompilerInstance(CompilerInstance &ScanInstance, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS, DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service, IntrusiveRefCntPtr< DependencyScanningWorkerFilesystem > DepFS)
llvm::StringMap< PrebuiltModuleASTAttrs > PrebuiltModulesAttrsMap
Attributes loaded from AST files of prebuilt modules collected prior to ModuleDepCollector creation.
std::unique_ptr< CompilerInvocation > createCompilerInvocation(ArrayRef< std::string > CommandLine, DiagnosticsEngine &Diags)
std::optional< PrebuiltModulesAttrsMap > computePrebuiltModulesASTMap(CompilerInstance &ScanInstance, SmallVector< StringRef > &StableDirs)
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::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)
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< DiagnosticOptions > createDiagOptions(ArrayRef< std::string > CommandLine)
The JSON file list parser is used to communicate input to InstallAPI.
IntrusiveRefCntPtr< DiagnosticsEngine > DiagEngine
DiagnosticsEngineWithDiagOpts(ArrayRef< std::string > CommandLine, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS, DiagnosticConsumer &DC)
TextDiagnosticsPrinterWithOutput(ArrayRef< std::string > CommandLine)