@@ -2,6 +2,127 @@ import CASTBridging
2
2
import SwiftDiagnostics
3
3
import SwiftSyntax
4
4
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
+
5
126
extension SourceManager {
6
127
private func diagnoseSingle(
7
128
message: String ,
0 commit comments