Skip to content

Commit 6831c24

Browse files
authored
Merge pull request #71792 from artemcm/DepScanDiagConsumerIsolate
[Dependency Scanning] Guard access to shared state of the `DependencyScannerDiagnosticCollectingConsumer`
2 parents 63c0bf3 + 5c4bbc1 commit 6831c24

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

include/swift/DependencyScan/DependencyScanningTool.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ class DependencyScannerDiagnosticCollectingConsumer : public DiagnosticConsumer
3939

4040
void handleDiagnostic(SourceManager &SM,
4141
const DiagnosticInfo &Info) override;
42-
ScannerDiagnosticInfo convertDiagnosticInfo(SourceManager &SM,
43-
const DiagnosticInfo &Info);
4442
void addDiagnostic(SourceManager &SM, const DiagnosticInfo &Info);
4543
std::vector<ScannerDiagnosticInfo> Diagnostics;
44+
// FIXME: For now, we isolate access to shared state of this object
45+
// but we really should make sure that it doesn't get shared.
46+
llvm::sys::SmartMutex<true> ScanningDiagnosticConsumerStateLock;
4647
};
4748

4849

@@ -91,8 +92,8 @@ class DependencyScanningTool {
9192
bool loadCache(llvm::StringRef path);
9293
/// Discard the tool's current `SharedCache` and start anew.
9394
void resetCache();
94-
95-
const std::vector<DependencyScannerDiagnosticCollectingConsumer::ScannerDiagnosticInfo>& getDiagnostics() const { return CDC.Diagnostics; }
95+
/// Query diagnostics consumed so far.
96+
std::vector<DependencyScannerDiagnosticCollectingConsumer::ScannerDiagnosticInfo> getDiagnostics();
9697
/// Discared the collection of diagnostics encountered so far.
9798
void resetDiagnostics();
9899

lib/DependencyScan/DependencyScanningTool.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void DependencyScannerDiagnosticCollectingConsumer::handleDiagnostic(SourceManag
7777
}
7878

7979
void DependencyScannerDiagnosticCollectingConsumer::addDiagnostic(SourceManager &SM, const DiagnosticInfo &Info) {
80+
llvm::sys::SmartScopedLock<true> Lock(ScanningDiagnosticConsumerStateLock);
8081
// Determine what kind of diagnostic we're emitting.
8182
llvm::SourceMgr::DiagKind SMKind;
8283
switch (Info.Kind) {
@@ -219,6 +220,13 @@ void DependencyScanningTool::resetCache() {
219220
ScanningService.reset(new SwiftDependencyScanningService());
220221
}
221222

223+
std::vector<
224+
DependencyScannerDiagnosticCollectingConsumer::ScannerDiagnosticInfo>
225+
DependencyScanningTool::getDiagnostics() {
226+
llvm::sys::SmartScopedLock<true> Lock(DependencyScanningToolStateLock);
227+
return CDC.Diagnostics;
228+
}
229+
222230
void DependencyScanningTool::resetDiagnostics() {
223231
llvm::sys::SmartScopedLock<true> Lock(DependencyScanningToolStateLock);
224232
CDC.reset();

tools/libSwiftScan/libSwiftScan.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,14 +613,15 @@ swiftscan_compiler_supported_features_query() {
613613
swiftscan_diagnostic_set_t*
614614
swiftscan_scanner_diagnostics_query(swiftscan_scanner_t scanner) {
615615
DependencyScanningTool *ScanningTool = unwrap(scanner);
616-
auto NumDiagnostics = ScanningTool->getDiagnostics().size();
617-
616+
auto Diagnostics = ScanningTool->getDiagnostics();
617+
auto NumDiagnostics = Diagnostics.size();
618+
618619
swiftscan_diagnostic_set_t *Result = new swiftscan_diagnostic_set_t;
619620
Result->count = NumDiagnostics;
620621
Result->diagnostics = new swiftscan_diagnostic_info_t[NumDiagnostics];
621622

622623
for (size_t i = 0; i < NumDiagnostics; ++i) {
623-
const auto &Diagnostic = ScanningTool->getDiagnostics()[i];
624+
const auto &Diagnostic = Diagnostics[i];
624625
swiftscan_diagnostic_info_s *DiagnosticInfo = new swiftscan_diagnostic_info_s;
625626
DiagnosticInfo->message = swift::c_string_utils::create_clone(Diagnostic.Message.c_str());
626627
switch (Diagnostic.Severity) {

0 commit comments

Comments
 (0)