15
15
16
16
#include " swift-c/DependencyScan/DependencyScan.h"
17
17
#include " swift/Frontend/Frontend.h"
18
+ #include " swift/AST/DiagnosticConsumer.h"
18
19
#include " swift/AST/ModuleDependencies.h"
19
20
#include " swift/DependencyScan/ScanDependencies.h"
20
21
#include " swift/Frontend/PrintingDiagnosticConsumer.h"
22
+ #include " swift/Frontend/SerializedDiagnosticConsumer.h"
21
23
#include " llvm/Support/Error.h"
22
24
#include " llvm/Support/StringSaver.h"
23
25
24
26
namespace swift {
25
27
namespace dependencies {
26
28
class DependencyScanningTool ;
27
- class DependencyScanDiagnosticCollector ;
29
+ class DepScanInMemoryDiagnosticCollector ;
28
30
29
- struct ScanQueryInstance {
31
+ struct ScanQueryContext {
32
+ // / Primary CompilerInstance configured for this scanning action
30
33
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
+ }
32
62
};
33
63
34
64
// / Pure virtual Diagnostic consumer intended for collecting
@@ -47,12 +77,16 @@ class ThreadSafeDiagnosticCollector : public DiagnosticConsumer {
47
77
};
48
78
49
79
// / 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:
52
85
struct ScannerDiagnosticInfo {
53
86
std::string Message;
54
87
llvm::SourceMgr::DiagKind Severity;
55
- std::optional<ScannerImportStatementInfo::ImportDiagnosticLocationInfo> ImportLocation;
88
+ std::optional<ScannerImportStatementInfo::ImportDiagnosticLocationInfo>
89
+ ImportLocation;
56
90
};
57
91
std::vector<ScannerDiagnosticInfo> Diagnostics;
58
92
@@ -61,9 +95,12 @@ class DependencyScanDiagnosticCollector : public ThreadSafeDiagnosticCollector {
61
95
62
96
public:
63
97
friend DependencyScanningTool;
64
- DependencyScanDiagnosticCollector () {}
98
+ DepScanInMemoryDiagnosticCollector () {}
65
99
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 {
67
104
return Diagnostics;
68
105
}
69
106
};
@@ -97,10 +134,10 @@ class DependencyScanningTool {
97
134
98
135
// / Using the specified invocation command, instantiate a CompilerInstance
99
136
// / 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 );
104
141
105
142
private:
106
143
// / Shared cache of module dependencies, re-used by individual full-scan queries
@@ -113,7 +150,9 @@ class DependencyScanningTool {
113
150
llvm::StringSaver Saver;
114
151
};
115
152
116
- swiftscan_diagnostic_set_t *mapCollectedDiagnosticsForOutput (const DependencyScanDiagnosticCollector *diagnosticCollector);
153
+ swiftscan_diagnostic_set_t *mapCollectedDiagnosticsForOutput (
154
+ ArrayRef<DepScanInMemoryDiagnosticCollector::ScannerDiagnosticInfo>
155
+ diagnostics);
117
156
118
157
} // end namespace dependencies
119
158
} // end namespace swift
0 commit comments