1515
1616#include " swift-c/DependencyScan/DependencyScan.h"
1717#include " swift/Frontend/Frontend.h"
18+ #include " swift/AST/DiagnosticConsumer.h"
1819#include " swift/AST/ModuleDependencies.h"
1920#include " swift/DependencyScan/ScanDependencies.h"
2021#include " swift/Frontend/PrintingDiagnosticConsumer.h"
22+ #include " swift/Frontend/SerializedDiagnosticConsumer.h"
2123#include " llvm/Support/Error.h"
2224#include " llvm/Support/StringSaver.h"
2325
2426namespace swift {
2527namespace dependencies {
2628class DependencyScanningTool ;
27- class DependencyScanDiagnosticCollector ;
29+ class DepScanInMemoryDiagnosticCollector ;
2830
29- struct ScanQueryInstance {
31+ struct ScanQueryContext {
32+ // / Primary CompilerInstance configured for this scanning action
3033 std::unique_ptr<CompilerInstance> ScanInstance;
31- std::shared_ptr<DependencyScanDiagnosticCollector> ScanDiagnostics;
34+ // / An thread-safe diagnostic consumer which collects all emitted
35+ // / diagnostics in the scan to be reporte via libSwiftScan API
36+ std::unique_ptr<DepScanInMemoryDiagnosticCollector> InMemoryDiagnosticCollector;
37+ // / A thread-safe serialized diagnostics consumer.
38+ // / Note, although type-erased, this must be an instance of
39+ // / 'ThreadSafeSerializedDiagnosticConsumer'
40+ std::unique_ptr<DiagnosticConsumer> SerializedDiagnosticConsumer;
41+
42+ ScanQueryContext (
43+ std::unique_ptr<CompilerInstance> ScanInstance,
44+ std::unique_ptr<DepScanInMemoryDiagnosticCollector>
45+ InMemoryDiagnosticCollector,
46+ std::unique_ptr<DiagnosticConsumer> SerializedDiagnosticConsumer)
47+ : ScanInstance(std::move(ScanInstance)),
48+ InMemoryDiagnosticCollector (std::move(InMemoryDiagnosticCollector)),
49+ SerializedDiagnosticConsumer(std::move(SerializedDiagnosticConsumer)) {}
50+
51+ ScanQueryContext (ScanQueryContext &&other)
52+ : ScanInstance(std::move(other.ScanInstance)),
53+ InMemoryDiagnosticCollector(
54+ std::move (other.InMemoryDiagnosticCollector)),
55+ SerializedDiagnosticConsumer(
56+ std::move (other.SerializedDiagnosticConsumer)) {}
57+
58+ ~ScanQueryContext () {
59+ if (SerializedDiagnosticConsumer)
60+ SerializedDiagnosticConsumer->finishProcessing ();
61+ }
3262};
3363
3464// / Pure virtual Diagnostic consumer intended for collecting
@@ -47,12 +77,16 @@ class ThreadSafeDiagnosticCollector : public DiagnosticConsumer {
4777};
4878
4979// / Diagnostic consumer that simply collects the diagnostics emitted so-far
50- class DependencyScanDiagnosticCollector : public ThreadSafeDiagnosticCollector {
51- private:
80+ // / and uses a representation agnostic from any specific CompilerInstance state
81+ // / which may have been used to emit the diagnostic
82+ class DepScanInMemoryDiagnosticCollector
83+ : public ThreadSafeDiagnosticCollector {
84+ public:
5285 struct ScannerDiagnosticInfo {
5386 std::string Message;
5487 llvm::SourceMgr::DiagKind Severity;
55- std::optional<ScannerImportStatementInfo::ImportDiagnosticLocationInfo> ImportLocation;
88+ std::optional<ScannerImportStatementInfo::ImportDiagnosticLocationInfo>
89+ ImportLocation;
5690 };
5791 std::vector<ScannerDiagnosticInfo> Diagnostics;
5892
@@ -61,9 +95,12 @@ class DependencyScanDiagnosticCollector : public ThreadSafeDiagnosticCollector {
6195
6296public:
6397 friend DependencyScanningTool;
64- DependencyScanDiagnosticCollector () {}
98+ DepScanInMemoryDiagnosticCollector () {}
6599 void reset () { Diagnostics.clear (); }
66- const std::vector<ScannerDiagnosticInfo> &getDiagnostics () const {
100+ std::vector<ScannerDiagnosticInfo> getDiagnostics () const {
101+ return Diagnostics;
102+ }
103+ const std::vector<ScannerDiagnosticInfo> &getDiagnosticsRef () const {
67104 return Diagnostics;
68105 }
69106};
@@ -97,10 +134,10 @@ class DependencyScanningTool {
97134
98135 // / Using the specified invocation command, instantiate a CompilerInstance
99136 // / that will be used for this scan.
100- llvm::ErrorOr<ScanQueryInstance>
101- initCompilerInstanceForScan ( ArrayRef<const char *> Command,
102- StringRef WorkingDirectory,
103- std::shared_ptr<DependencyScanDiagnosticCollector> scannerDiagnosticsCollector );
137+ llvm::ErrorOr<ScanQueryContext> createScanQueryContext (
138+ ArrayRef<const char *> Command, StringRef WorkingDirectory ,
139+ std::vector<DepScanInMemoryDiagnosticCollector::ScannerDiagnosticInfo>
140+ &initializationDiagnostics );
104141
105142private:
106143 // / Shared cache of module dependencies, re-used by individual full-scan queries
@@ -113,7 +150,9 @@ class DependencyScanningTool {
113150 llvm::StringSaver Saver;
114151};
115152
116- swiftscan_diagnostic_set_t *mapCollectedDiagnosticsForOutput (const DependencyScanDiagnosticCollector *diagnosticCollector);
153+ swiftscan_diagnostic_set_t *mapCollectedDiagnosticsForOutput (
154+ ArrayRef<DepScanInMemoryDiagnosticCollector::ScannerDiagnosticInfo>
155+ diagnostics);
117156
118157} // end namespace dependencies
119158} // end namespace swift
0 commit comments