Skip to content

Commit 926fc78

Browse files
authored
Merge pull request #84191 from artemcm/InMemoryDepScanSerializedDiagnostics
[Dependency Scanning] Configure a serialized diagnostics consumer for in-memory scans
2 parents 798621e + 50c0dd8 commit 926fc78

File tree

9 files changed

+506
-255
lines changed

9 files changed

+506
-255
lines changed

include/swift/DependencyScan/DependencyScanningTool.h

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,50 @@
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

2426
namespace swift {
2527
namespace dependencies {
2628
class 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

6296
public:
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

105142
private:
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

include/swift/DependencyScan/ScanDependencies.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ using ModuleDependencyIDSet =
4343
class SwiftDependencyScanningService;
4444

4545
namespace dependencies {
46-
class DependencyScanDiagnosticCollector;
46+
struct ScanQueryContext;
4747

4848
using CompilerArgInstanceCacheMap =
4949
llvm::StringMap<std::tuple<std::unique_ptr<CompilerInstance>,
@@ -62,16 +62,15 @@ bool prescanDependencies(CompilerInstance &instance);
6262
/// Scans the dependencies of the main module of \c instance.
6363
llvm::ErrorOr<swiftscan_dependency_graph_t>
6464
performModuleScan(SwiftDependencyScanningService &service,
65-
CompilerInstance &instance,
6665
ModuleDependenciesCache &cache,
67-
DependencyScanDiagnosticCollector *diagnostics = nullptr);
66+
ScanQueryContext &queryContext);
6867

6968
/// Scans the main module of \c instance for all direct module imports
7069
llvm::ErrorOr<swiftscan_import_set_t>
7170
performModulePrescan(SwiftDependencyScanningService &service,
72-
CompilerInstance &instance,
7371
ModuleDependenciesCache &cache,
74-
DependencyScanDiagnosticCollector *diagnostics = nullptr);
72+
ScanQueryContext &queryContext);
73+
7574

7675
namespace incremental {
7776
/// For the given module dependency graph captured in the 'cache',

include/swift/Frontend/SerializedDiagnosticConsumer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ namespace swift {
3737
/// \returns A new diagnostic consumer that serializes diagnostics.
3838
std::unique_ptr<DiagnosticConsumer>
3939
createConsumer(llvm::StringRef outputPath, bool emitMacroExpansionFiles);
40+
41+
/// Create a thread-safe DiagnosticConsumer that serializes diagnostics to a file,
42+
/// using Clang's serialized diagnostics format.
43+
///
44+
/// \param outputPath the file path to write the diagnostics to.
45+
///
46+
/// \returns A new diagnostic consumer that serializes diagnostics.
47+
std::unique_ptr<DiagnosticConsumer>
48+
createThreadSafeConsumer(llvm::StringRef outputPath, bool emitMacroExpansionFiles);
4049
}
4150
}
4251

include/swift/Option/Options.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,13 @@ def emit_module_serialize_diagnostics_path:
21282128
"<path>">,
21292129
MetaVarName<"<path>">;
21302130

2131+
def dependency_scan_serialize_diagnostics_path:
2132+
Separate<["-"], "dependency-scan-serialize-diagnostics-path">,
2133+
Flags<[ArgumentIsPath, SupplementaryOutput, NewDriverOnlyOption]>,
2134+
HelpText<"Emit a serialized diagnostics file for the dependency scanning task to "
2135+
"<path>">,
2136+
MetaVarName<"<path>">;
2137+
21312138
def emit_module_dependencies_path:
21322139
Separate<["-"], "emit-module-dependencies-path">,
21332140
Flags<[ArgumentIsPath, SupplementaryOutput, NewDriverOnlyOption]>,

0 commit comments

Comments
 (0)