99#ifndef LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNER_H
1010#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNING_DEPENDENCYSCANNER_H
1111
12+ #include " clang/Driver/Compilation.h"
1213#include " clang/Frontend/CompilerInstance.h"
1314#include " clang/Frontend/CompilerInvocation.h"
15+ #include " clang/Frontend/TextDiagnosticPrinter.h"
1416#include " clang/Serialization/ObjectFilePCHContainerReader.h"
1517#include " clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h"
1618#include " clang/Tooling/DependencyScanning/ModuleDepCollector.h"
@@ -30,12 +32,12 @@ class DependencyScanningAction {
3032 DependencyScanningAction (
3133 DependencyScanningService &Service, StringRef WorkingDirectory,
3234 DependencyConsumer &Consumer, DependencyActionController &Controller,
33- llvm:: IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS,
35+ IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS,
3436 std::optional<StringRef> ModuleName = std::nullopt )
3537 : Service(Service), WorkingDirectory(WorkingDirectory),
3638 Consumer (Consumer), Controller(Controller), DepFS(std::move(DepFS)),
3739 ModuleName(ModuleName) {}
38- bool runInvocation (std::shared_ptr <CompilerInvocation> Invocation,
40+ bool runInvocation (std::unique_ptr <CompilerInvocation> Invocation,
3941 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
4042 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
4143 DiagnosticConsumer *DiagConsumer);
@@ -63,7 +65,7 @@ class DependencyScanningAction {
6365 StringRef WorkingDirectory;
6466 DependencyConsumer &Consumer;
6567 DependencyActionController &Controller;
66- llvm:: IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
68+ IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS;
6769 std::optional<StringRef> ModuleName;
6870 std::optional<CompilerInstance> ScanInstanceStorage;
6971 std::shared_ptr<ModuleDepCollector> MDC;
@@ -72,9 +74,81 @@ class DependencyScanningAction {
7274 bool DiagConsumerFinished = false ;
7375};
7476
75- // Helper functions
76- void sanitizeDiagOpts (DiagnosticOptions &DiagOpts);
77+ // Helper functions and data types.
78+ std::unique_ptr<DiagnosticOptions>
79+ createDiagOptions (ArrayRef<std::string> CommandLine);
7780
81+ struct DignosticsEngineWithDiagOpts {
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;
85+ IntrusiveRefCntPtr<DiagnosticsEngine> DiagEngine;
86+
87+ DignosticsEngineWithDiagOpts (ArrayRef<std::string> CommandLine,
88+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
89+ DiagnosticConsumer &DC);
90+ };
91+
92+ struct TextDiagnosticsPrinterWithOutput {
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;
98+ TextDiagnosticPrinter DiagPrinter;
99+
100+ TextDiagnosticsPrinterWithOutput (ArrayRef<std::string> CommandLine)
101+ : DiagnosticsOS(DiagnosticOutput),
102+ DiagOpts (createDiagOptions(CommandLine)),
103+ DiagPrinter(DiagnosticsOS, *DiagOpts) {}
104+ };
105+
106+ std::pair<std::unique_ptr<driver::Driver>, std::unique_ptr<driver::Compilation>>
107+ buildCompilation (ArrayRef<std::string> ArgStrs, DiagnosticsEngine &Diags,
108+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS);
109+
110+ std::unique_ptr<CompilerInvocation>
111+ createCompilerInvocation (ArrayRef<std::string> CommandLine,
112+ DiagnosticsEngine &Diags);
113+
114+ std::pair<IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::vector<std::string>>
115+ initVFSForTUBuferScanning (IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
116+ ArrayRef<std::string> CommandLine,
117+ StringRef WorkingDirectory,
118+ llvm::MemoryBufferRef TUBuffer);
119+
120+ std::pair<IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::vector<std::string>>
121+ initVFSForByNameScanning (IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
122+ ArrayRef<std::string> CommandLine,
123+ StringRef WorkingDirectory, StringRef ModuleName);
124+
125+ bool initializeScanCompilerInstance (
126+ CompilerInstance &ScanInstance,
127+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
128+ DiagnosticConsumer *DiagConsumer, DependencyScanningService &Service,
129+ IntrusiveRefCntPtr<DependencyScanningWorkerFilesystem> DepFS);
130+
131+ SmallVector<StringRef>
132+ getInitialStableDirs (const CompilerInstance &ScanInstance);
133+
134+ std::optional<PrebuiltModulesAttrsMap>
135+ computePrebuiltModulesASTMap (CompilerInstance &ScanInstance,
136+ SmallVector<StringRef> &StableDirs);
137+
138+ std::unique_ptr<DependencyOutputOptions>
139+ takeDependencyOutputOptionsFrom (CompilerInstance &ScanInstance);
140+
141+ // / Create the dependency collector that will collect the produced
142+ // / dependencies. May return the created ModuleDepCollector depending
143+ // / on the scanning format.
144+ std::shared_ptr<ModuleDepCollector> initializeScanInstanceDependencyCollector (
145+ CompilerInstance &ScanInstance,
146+ std::unique_ptr<DependencyOutputOptions> DepOutputOpts,
147+ StringRef WorkingDirectory, DependencyConsumer &Consumer,
148+ DependencyScanningService &Service, CompilerInvocation &Inv,
149+ DependencyActionController &Controller,
150+ PrebuiltModulesAttrsMap PrebuiltModulesASTMap,
151+ llvm::SmallVector<StringRef> &StableDirs);
78152} // namespace dependencies
79153} // namespace tooling
80154} // namespace clang
0 commit comments