Skip to content

Commit ba2b0f0

Browse files
committed
Revert "Switch all ASTGen-produced diagnostics over to SourceManager"
This reverts commit 6bd72bb.
1 parent 28f0ead commit ba2b0f0

File tree

2 files changed

+127
-4
lines changed

2 files changed

+127
-4
lines changed

lib/ASTGen/Sources/ASTGen/Diagnostics.swift

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,127 @@ import CASTBridging
22
import SwiftDiagnostics
33
import SwiftSyntax
44

5+
fileprivate func emitDiagnosticParts(
6+
diagEnginePtr: UnsafeMutablePointer<UInt8>,
7+
sourceFileBuffer: UnsafeMutableBufferPointer<UInt8>,
8+
message: String,
9+
severity: DiagnosticSeverity,
10+
position: AbsolutePosition,
11+
highlights: [Syntax] = [],
12+
fixItChanges: [FixIt.Change] = []
13+
) {
14+
// Map severity
15+
let bridgedSeverity: BridgedDiagnosticSeverity
16+
switch severity {
17+
case .error: bridgedSeverity = .error
18+
case .note: bridgedSeverity = .note
19+
case .warning: bridgedSeverity = .warning
20+
}
21+
22+
// Form a source location for the given absolute position
23+
func sourceLoc(
24+
at position: AbsolutePosition
25+
) -> UnsafeMutablePointer<UInt8>? {
26+
if let sourceFileBase = sourceFileBuffer.baseAddress,
27+
position.utf8Offset >= 0 &&
28+
position.utf8Offset < sourceFileBuffer.count {
29+
return sourceFileBase + position.utf8Offset
30+
}
31+
32+
return nil
33+
}
34+
35+
// Emit the diagnostic
36+
var mutableMessage = message
37+
let diag = mutableMessage.withUTF8 { messageBuffer in
38+
SwiftDiagnostic_create(
39+
diagEnginePtr, bridgedSeverity, sourceLoc(at: position),
40+
messageBuffer.baseAddress, messageBuffer.count
41+
)
42+
}
43+
44+
// Emit highlights
45+
for highlight in highlights {
46+
SwiftDiagnostic_highlight(
47+
diag, sourceLoc(at: highlight.position),
48+
sourceLoc(at: highlight.endPosition)
49+
)
50+
}
51+
52+
// Emit changes for a Fix-It.
53+
for change in fixItChanges {
54+
let replaceStartLoc: UnsafeMutablePointer<UInt8>?
55+
let replaceEndLoc: UnsafeMutablePointer<UInt8>?
56+
var newText: String
57+
58+
switch change {
59+
case .replace(let oldNode, let newNode):
60+
replaceStartLoc = sourceLoc(at: oldNode.position)
61+
replaceEndLoc = sourceLoc(at: oldNode.endPosition)
62+
newText = newNode.description
63+
64+
case .replaceLeadingTrivia(let oldToken, let newTrivia):
65+
replaceStartLoc = sourceLoc(at: oldToken.position)
66+
replaceEndLoc = sourceLoc(
67+
at: oldToken.positionAfterSkippingLeadingTrivia)
68+
newText = newTrivia.description
69+
70+
case .replaceTrailingTrivia(let oldToken, let newTrivia):
71+
replaceStartLoc = sourceLoc(at: oldToken.endPositionBeforeTrailingTrivia)
72+
replaceEndLoc = sourceLoc(at: oldToken.endPosition)
73+
newText = newTrivia.description
74+
}
75+
76+
newText.withUTF8 { textBuffer in
77+
SwiftDiagnostic_fixItReplace(
78+
diag, replaceStartLoc, replaceEndLoc,
79+
textBuffer.baseAddress, textBuffer.count
80+
)
81+
}
82+
}
83+
84+
SwiftDiagnostic_finish(diag);
85+
}
86+
87+
/// Emit the given diagnostic via the diagnostic engine.
88+
func emitDiagnostic(
89+
diagEnginePtr: UnsafeMutablePointer<UInt8>,
90+
sourceFileBuffer: UnsafeMutableBufferPointer<UInt8>,
91+
diagnostic: Diagnostic,
92+
messageSuffix: String? = nil
93+
) {
94+
// Emit the main diagnostic
95+
emitDiagnosticParts(
96+
diagEnginePtr: diagEnginePtr,
97+
sourceFileBuffer: sourceFileBuffer,
98+
message: diagnostic.diagMessage.message + (messageSuffix ?? ""),
99+
severity: diagnostic.diagMessage.severity,
100+
position: diagnostic.position,
101+
highlights: diagnostic.highlights
102+
)
103+
104+
// Emit Fix-Its.
105+
for fixIt in diagnostic.fixIts {
106+
emitDiagnosticParts(
107+
diagEnginePtr: diagEnginePtr,
108+
sourceFileBuffer: sourceFileBuffer,
109+
message: fixIt.message.message,
110+
severity: .note, position: diagnostic.position,
111+
fixItChanges: fixIt.changes.changes
112+
)
113+
}
114+
115+
// Emit any notes as follow-ons.
116+
for note in diagnostic.notes {
117+
emitDiagnosticParts(
118+
diagEnginePtr: diagEnginePtr,
119+
sourceFileBuffer: sourceFileBuffer,
120+
message: note.message,
121+
severity: .note, position: note.position
122+
)
123+
}
124+
}
125+
5126
extension SourceManager {
6127
private func diagnoseSingle(
7128
message: String,

lib/ASTGen/Sources/ASTGen/SourceFile.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ public func emitParserDiagnostics(
8383
) { sourceFile in
8484
var anyDiags = false
8585

86-
let sourceManager = SourceManager(cxxDiagnosticEngine: diagEnginePtr)
87-
sourceManager.insert(sourceFile)
88-
8986
let diags = ParseDiagnosticsGenerator.diagnostics(
9087
for: sourceFile.pointee.syntax
9188
)
@@ -97,7 +94,12 @@ public func emitParserDiagnostics(
9794
continue
9895
}
9996

100-
sourceManager.diagnose(diagnostic: diag)
97+
emitDiagnostic(
98+
diagEnginePtr: diagEnginePtr,
99+
sourceFileBuffer: UnsafeMutableBufferPointer(
100+
mutating: sourceFile.pointee.buffer),
101+
diagnostic: diag
102+
)
101103
anyDiags = true
102104
}
103105

0 commit comments

Comments
 (0)