Skip to content

Commit 411c1c6

Browse files
committed
Emit compiler diagnostics
Check for error,warning,note and remark in a format compatible with clang and swiftc, see: https://github.com/apple/swift/blob/main/docs/Diagnostics.md
1 parent 86bd1fe commit 411c1c6

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

Sources/Build/BuildOperationBuildSystemDelegateHandler.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,44 @@ final class BuildOperationBuildSystemDelegateHandler: LLBuildBuildSystemDelegate
612612
// next we want to try and scoop out any errors from the output (if reasonable size, otherwise this will be very slow),
613613
// so they can later be passed to the advice provider in case of failure.
614614
if output.utf8.count < 1024 * 10 {
615-
let regex = try! RegEx(pattern: #".*(error:[^\n]*)\n.*"#, options: .dotMatchesLineSeparators)
615+
616+
do {
617+
let regex = try! RegEx(pattern: #"^(.*):(\d+):(\d+):\s*(error|warning|note|remark)\S?\s*(.*)$"#, options: [.anchorsMatchLines])
618+
for match in regex.matchGroups(in: output) {
619+
if let filePath = try? AbsolutePath(validating: match[0]) {
620+
621+
var severity: Basics.Diagnostic.Severity {
622+
switch match[3] {
623+
case "error":
624+
return .error
625+
case "warning":
626+
return .warning
627+
case "note":
628+
return .info
629+
case "remark":
630+
return .info
631+
default:
632+
return .debug
633+
}
634+
}
635+
636+
var metadata = ObservabilityMetadata()
637+
metadata.fileLocation = FileLocation(filePath, line: Int(match[1]), column: Int(match[2]))
638+
self.observabilityScope.emit(
639+
Basics.Diagnostic(
640+
severity: severity,
641+
message: match[4],
642+
metadata: metadata
643+
)
644+
)
645+
}
646+
}
647+
}
648+
649+
let regex = try! RegEx(pattern: #"(error:[^\n]*)\n.*"#, options: .dotMatchesLineSeparators)
616650
for match in regex.matchGroups(in: output) {
617651
self.errorMessagesByTarget[parser.targetName] = (self.errorMessagesByTarget[parser.targetName] ?? []) + [match[0]]
652+
618653
}
619654
}
620655
}

Sources/SPMBuildCore/PluginInvocation.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,13 +858,24 @@ extension ObservabilityMetadata {
858858
public struct FileLocation: Equatable, CustomStringConvertible {
859859
public let file: AbsolutePath
860860
public let line: Int?
861+
public let column: Int?
861862

862-
public init(_ file: AbsolutePath, line: Int?) {
863+
public init(_ file: AbsolutePath, line: Int?, column: Int? = nil) {
863864
self.file = file
864865
self.line = line
866+
self.column = column
865867
}
866868

867869
public var description: String {
868-
"\(self.file)\(self.line?.description.appending(" ") ?? "")"
870+
var desc = self.file.description
871+
if let line = line {
872+
desc += ":\(line)"
873+
}
874+
875+
if let column = column {
876+
desc += ":\(column)"
877+
}
878+
879+
return desc
869880
}
870881
}

0 commit comments

Comments
 (0)