Skip to content

Commit 83012a5

Browse files
committed
[ASTGen] Move source manager diagnostics code into its own file
1 parent 6bd72bb commit 83012a5

File tree

4 files changed

+123
-119
lines changed

4 files changed

+123
-119
lines changed

lib/ASTGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ if (SWIFT_SWIFT_PARSER)
2929
Sources/ASTGen/Misc.swift
3030
Sources/ASTGen/SourceFile.swift
3131
Sources/ASTGen/SourceManager.swift
32+
Sources/ASTGen/SourceManager+Diagnostics.swift
3233
Sources/ASTGen/Stmts.swift
3334
Sources/ASTGen/Types.swift
3435
)

lib/ASTGen/Sources/ASTGen/Diagnostics.swift

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

5-
extension SourceManager {
6-
private func diagnoseSingle(
7-
message: String,
8-
severity: DiagnosticSeverity,
9-
node: some SyntaxProtocol,
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-
// Emit the diagnostic
23-
var mutableMessage = message
24-
let diag = mutableMessage.withUTF8 { messageBuffer in
25-
SwiftDiagnostic_create(
26-
cxxDiagnosticEngine, bridgedSeverity,
27-
cxxSourceLocation(for: node, at: position),
28-
messageBuffer.baseAddress, messageBuffer.count
29-
)
30-
}
31-
32-
// Emit highlights
33-
for highlight in highlights {
34-
SwiftDiagnostic_highlight(
35-
diag,
36-
cxxSourceLocation(for: highlight),
37-
cxxSourceLocation(for: highlight, at: highlight.endPosition)
38-
)
39-
}
40-
41-
// Emit changes for a Fix-It.
42-
for change in fixItChanges {
43-
let replaceStartLoc: CxxSourceLoc?
44-
let replaceEndLoc: CxxSourceLoc?
45-
var newText: String
46-
47-
switch change {
48-
case .replace(let oldNode, let newNode):
49-
replaceStartLoc = cxxSourceLocation(for: oldNode)
50-
replaceEndLoc = cxxSourceLocation(
51-
for: oldNode,
52-
at: oldNode.endPosition
53-
)
54-
newText = newNode.description
55-
56-
case .replaceLeadingTrivia(let oldToken, let newTrivia):
57-
replaceStartLoc = cxxSourceLocation(for: oldToken)
58-
replaceEndLoc = cxxSourceLocation(
59-
for: oldToken,
60-
at: oldToken.positionAfterSkippingLeadingTrivia
61-
)
62-
newText = newTrivia.description
63-
64-
case .replaceTrailingTrivia(let oldToken, let newTrivia):
65-
replaceStartLoc = cxxSourceLocation(
66-
for: oldToken,
67-
at: oldToken.endPositionBeforeTrailingTrivia)
68-
replaceEndLoc = cxxSourceLocation(
69-
for: oldToken,
70-
at: oldToken.endPosition
71-
)
72-
newText = newTrivia.description
73-
}
74-
75-
newText.withUTF8 { textBuffer in
76-
SwiftDiagnostic_fixItReplace(
77-
diag, replaceStartLoc, replaceEndLoc,
78-
textBuffer.baseAddress, textBuffer.count
79-
)
80-
}
81-
}
82-
83-
SwiftDiagnostic_finish(diag);
84-
}
85-
86-
/// Emit a diagnostic via the C++ diagnostic engine.
87-
func diagnose(
88-
diagnostic: Diagnostic,
89-
messageSuffix: String? = nil
90-
) {
91-
// Emit the main diagnostic.
92-
diagnoseSingle(
93-
message: diagnostic.diagMessage.message + (messageSuffix ?? ""),
94-
severity: diagnostic.diagMessage.severity,
95-
node: diagnostic.node,
96-
position: diagnostic.position,
97-
highlights: diagnostic.highlights
98-
)
99-
100-
// Emit Fix-Its.
101-
for fixIt in diagnostic.fixIts {
102-
diagnoseSingle(
103-
message: fixIt.message.message,
104-
severity: .note,
105-
node: diagnostic.node,
106-
position: diagnostic.position,
107-
fixItChanges: fixIt.changes.changes
108-
)
109-
}
110-
111-
// Emit any notes as follow-ons.
112-
for note in diagnostic.notes {
113-
diagnoseSingle(
114-
message: note.message,
115-
severity: .note,
116-
node: note.node,
117-
position: note.position
118-
)
119-
}
120-
}
121-
}
122-
1235
/// A set of queued diagnostics created by the C++ compiler and rendered
1246
/// via the swift-syntax renderer.
1257
struct QueuedDiagnostics {
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import CASTBridging
2+
import SwiftDiagnostics
3+
import SwiftSyntax
4+
5+
extension SourceManager {
6+
private func diagnoseSingle(
7+
message: String,
8+
severity: DiagnosticSeverity,
9+
node: some SyntaxProtocol,
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+
// Emit the diagnostic
23+
var mutableMessage = message
24+
let diag = mutableMessage.withUTF8 { messageBuffer in
25+
SwiftDiagnostic_create(
26+
cxxDiagnosticEngine, bridgedSeverity,
27+
cxxSourceLocation(for: node, at: position),
28+
messageBuffer.baseAddress, messageBuffer.count
29+
)
30+
}
31+
32+
// Emit highlights
33+
for highlight in highlights {
34+
SwiftDiagnostic_highlight(
35+
diag,
36+
cxxSourceLocation(for: highlight),
37+
cxxSourceLocation(for: highlight, at: highlight.endPosition)
38+
)
39+
}
40+
41+
// Emit changes for a Fix-It.
42+
for change in fixItChanges {
43+
let replaceStartLoc: CxxSourceLoc?
44+
let replaceEndLoc: CxxSourceLoc?
45+
var newText: String
46+
47+
switch change {
48+
case .replace(let oldNode, let newNode):
49+
replaceStartLoc = cxxSourceLocation(for: oldNode)
50+
replaceEndLoc = cxxSourceLocation(
51+
for: oldNode,
52+
at: oldNode.endPosition
53+
)
54+
newText = newNode.description
55+
56+
case .replaceLeadingTrivia(let oldToken, let newTrivia):
57+
replaceStartLoc = cxxSourceLocation(for: oldToken)
58+
replaceEndLoc = cxxSourceLocation(
59+
for: oldToken,
60+
at: oldToken.positionAfterSkippingLeadingTrivia
61+
)
62+
newText = newTrivia.description
63+
64+
case .replaceTrailingTrivia(let oldToken, let newTrivia):
65+
replaceStartLoc = cxxSourceLocation(
66+
for: oldToken,
67+
at: oldToken.endPositionBeforeTrailingTrivia)
68+
replaceEndLoc = cxxSourceLocation(
69+
for: oldToken,
70+
at: oldToken.endPosition
71+
)
72+
newText = newTrivia.description
73+
}
74+
75+
newText.withUTF8 { textBuffer in
76+
SwiftDiagnostic_fixItReplace(
77+
diag, replaceStartLoc, replaceEndLoc,
78+
textBuffer.baseAddress, textBuffer.count
79+
)
80+
}
81+
}
82+
83+
SwiftDiagnostic_finish(diag);
84+
}
85+
86+
/// Emit a diagnostic via the C++ diagnostic engine.
87+
func diagnose(
88+
diagnostic: Diagnostic,
89+
messageSuffix: String? = nil
90+
) {
91+
// Emit the main diagnostic.
92+
diagnoseSingle(
93+
message: diagnostic.diagMessage.message + (messageSuffix ?? ""),
94+
severity: diagnostic.diagMessage.severity,
95+
node: diagnostic.node,
96+
position: diagnostic.position,
97+
highlights: diagnostic.highlights
98+
)
99+
100+
// Emit Fix-Its.
101+
for fixIt in diagnostic.fixIts {
102+
diagnoseSingle(
103+
message: fixIt.message.message,
104+
severity: .note,
105+
node: diagnostic.node,
106+
position: diagnostic.position,
107+
fixItChanges: fixIt.changes.changes
108+
)
109+
}
110+
111+
// Emit any notes as follow-ons.
112+
for note in diagnostic.notes {
113+
diagnoseSingle(
114+
message: note.message,
115+
severity: .note,
116+
node: note.node,
117+
position: note.position
118+
)
119+
}
120+
}
121+
}

lib/ASTGen/Sources/ASTGen/SourceManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ extension SourceManager {
136136

137137
var detached = context.detach(node)
138138

139-
if let operatorTable {
139+
if let operatorTable = operatorTable {
140140
detached = operatorTable.foldAll(node) { _ in }.as(Node.self)!
141141
}
142142

0 commit comments

Comments
 (0)