1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
//===--- SourceManager.swift ----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import ASTBridging
import BasicBridging
import SwiftOperators
import SwiftSyntax
import SwiftDiagnostics
import swiftASTGen
/// A source manager that keeps track of the source files in the program.
class SourceManager {
init(cxxDiagnosticEngine: UnsafeMutableRawPointer) {
self.bridgedDiagEngine = BridgedDiagnosticEngine(raw: cxxDiagnosticEngine)
}
/// The bridged diagnostic engine (just the wrapped C++ `DiagnosticEngine`).
let bridgedDiagEngine: BridgedDiagnosticEngine
/// The set of source files that have been exported to the C++ code of
/// the program.
var exportedSourceFilesBySyntax: [Syntax: UnsafePointer<ExportedSourceFile>] = [:]
/// The set of nodes that have been detached from their parent nodes.
///
/// The keys are the detached nodes, while the values are (parent node,
/// offset of detached node while it was attached).
private var detachedNodes: [Syntax: (Syntax, Int)] = [:]
}
/// MARK: Source file management
extension SourceManager {
/// Inserts a new source file into the source manager.
///
/// - Returns: `true` if the source file was inserted, `false` if it was
/// already there.
@discardableResult
func insert(_ sourceFile: UnsafePointer<ExportedSourceFile>) -> Bool {
let syntax = Syntax(sourceFile.pointee.syntax)
if exportedSourceFilesBySyntax[syntax] != nil {
return false
}
exportedSourceFilesBySyntax[syntax] = sourceFile
return true
}
}
/// MARK: Syntax source location mapping
extension SourceManager {
/// Detach a given node from its parent, keeping track of where it
/// occurred in the program.
func detach<Node: SyntaxProtocol>(
_ node: Node,
foldingWith operatorTable: OperatorTable? = nil
) -> Node {
// Already detached
if node.parent == nil { return node }
let detached: Node
if let operatorTable = operatorTable {
detached = operatorTable.foldAll(node) { _ in }.as(Node.self)!.detached
} else {
detached = node.detached
}
detachedNodes[Syntax(detached)] = (node.root, node.position.utf8Offset)
return detached
}
/// Find the root source file and offset from within that file for the given
/// syntax node.
func rootSyntax<Node: SyntaxProtocol>(
of node: Node
) -> (Syntax, AbsolutePosition) {
var root = node.root
var offset = node.position
// If the root isn't a detached node we know about, there's nothing we
// can do.
while let (parent, parentOffset) = detachedNodes[root] {
root = parent.root
offset += SourceLength(utf8Length: parentOffset)
}
return (root, offset)
}
/// Produce the C++ source location for a given position based on a
/// syntax node.
func bridgedSourceLoc<Node: SyntaxProtocol>(
for node: Node,
at position: AbsolutePosition? = nil
) -> BridgedSourceLoc {
// Find the source file and this node's position within it.
let (rootNode, rootPosition) = rootSyntax(of: node)
// Find the corresponding exported source file.
guard let exportedSourceFile = exportedSourceFilesBySyntax[rootNode] else {
return nil
}
// Find the offset of the given position based on the root of the given
// node.
let position = position ?? node.position
let nodeOffset = SourceLength(utf8Length: position.utf8Offset - node.position.utf8Offset)
let realPosition = rootPosition + nodeOffset
return BridgedSourceLoc(at: realPosition, in: exportedSourceFile.pointee.buffer)
}
}
extension SourceManager {
private func diagnoseSingle<Node: SyntaxProtocol>(
message: String,
severity: DiagnosticSeverity,
node: Node,
position: AbsolutePosition,
highlights: [Syntax] = [],
fixItChanges: [FixIt.Change] = []
) {
// Map severity
let bridgedSeverity = severity.bridged
// Emit the diagnostic
var mutableMessage = message
let diag = mutableMessage.withBridgedString { bridgedMessage in
BridgedDiagnostic(
at: bridgedSourceLoc(for: node, at: position),
message: bridgedMessage,
severity: bridgedSeverity,
engine: bridgedDiagEngine
)
}
// Emit highlights
for highlight in highlights {
diag.highlight(
start: bridgedSourceLoc(for: highlight, at: highlight.positionAfterSkippingLeadingTrivia),
end: bridgedSourceLoc(for: highlight, at: highlight.endPositionBeforeTrailingTrivia)
)
}
// Emit changes for a Fix-It.
for change in fixItChanges {
let replaceStartLoc: BridgedSourceLoc
let replaceEndLoc: BridgedSourceLoc
var newText: String
switch change {
case .replace(let oldNode, let newNode):
replaceStartLoc = bridgedSourceLoc(
for: oldNode,
at: oldNode.positionAfterSkippingLeadingTrivia
)
replaceEndLoc = bridgedSourceLoc(
for: oldNode,
at: oldNode.endPositionBeforeTrailingTrivia
)
newText = newNode.description
case .replaceLeadingTrivia(let oldToken, let newTrivia):
replaceStartLoc = bridgedSourceLoc(for: oldToken)
replaceEndLoc = bridgedSourceLoc(
for: oldToken,
at: oldToken.positionAfterSkippingLeadingTrivia
)
newText = newTrivia.description
case .replaceTrailingTrivia(let oldToken, let newTrivia):
replaceStartLoc = bridgedSourceLoc(
for: oldToken,
at: oldToken.endPositionBeforeTrailingTrivia
)
replaceEndLoc = bridgedSourceLoc(
for: oldToken,
at: oldToken.endPosition
)
newText = newTrivia.description
case .replaceChild(let replacingChildData):
let replacementRange = replacingChildData.replacementRange
replaceStartLoc = bridgedSourceLoc(
for: replacingChildData.parent,
at: replacementRange.lowerBound
)
replaceEndLoc = bridgedSourceLoc(
for: replacingChildData.parent,
at: replacementRange.upperBound
)
newText = replacingChildData.newChild.description
}
newText.withBridgedString { bridgedMessage in
diag.fixItReplace(
start: replaceStartLoc,
end: replaceEndLoc,
replacement: bridgedMessage
)
}
}
diag.finish();
}
/// Emit a diagnostic via the C++ diagnostic engine.
func diagnose(
diagnostic: Diagnostic,
messageSuffix: String? = nil
) {
// Emit the main diagnostic.
diagnoseSingle(
message: diagnostic.diagMessage.message + (messageSuffix ?? ""),
severity: diagnostic.diagMessage.severity,
node: diagnostic.node,
position: diagnostic.position,
highlights: diagnostic.highlights
)
// Emit Fix-Its.
for fixIt in diagnostic.fixIts {
diagnoseSingle(
message: fixIt.message.message,
severity: .note,
node: diagnostic.node,
position: diagnostic.position,
fixItChanges: fixIt.changes
)
}
// Emit any notes as follow-ons.
for note in diagnostic.notes {
diagnoseSingle(
message: note.message,
severity: .note,
node: note.node,
position: note.position
)
}
}
}
|