Skip to content

Commit 4dc6ce3

Browse files
committed
Bridge Parser Diagnostics into the C++ Diagnostic Engine
1 parent 3c11671 commit 4dc6ce3

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Sources/SwiftCompilerSupport/ConsistencyCheck.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ extension Syntax {
3939
@_spi(SwiftCompiler)
4040
public func _parserConsistencyCheck(
4141
bufferPtr: UnsafePointer<UInt8>, bufferLength: Int,
42-
filename: UnsafePointer<UInt8>, flags: CUnsignedInt
42+
filename: UnsafePointer<UInt8>, flags: CUnsignedInt,
43+
ctx: OpaquePointer,
44+
hook: @convention(c) (Int, UnsafePointer<Int8>, OpaquePointer) -> Void
4345
) -> CInt {
4446
let buffer = UnsafeBufferPointer<UInt8>(
4547
start: bufferPtr, count: bufferLength)
@@ -62,7 +64,9 @@ public func _parserConsistencyCheck(
6264
var anyDiags = false
6365

6466
let sourceFile = Syntax(raw: rawSourceFile.raw).as(SourceFileSyntax.self)!
67+
let fileName = String(cString: filename)
6568

69+
let converter = SourceLocationConverter(file: fileName, tree: sourceFile)
6670
let diags = ParseDiagnosticsGenerator.diagnostics(
6771
for: sourceFile)
6872
for diag in diags {
@@ -73,7 +77,9 @@ public func _parserConsistencyCheck(
7377
continue
7478
}
7579

76-
print("\(String(cString: filename)): error: \(diag.debugDescription)")
80+
diag.debugDescription.withCString { diagText in
81+
hook(diag.location(converter: converter).offset, diagText, ctx)
82+
}
7783
anyDiags = true
7884
}
7985

Sources/SwiftCompilerSupport/SwiftCompilerSupport.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ enum SwiftConsistencyCheckFlags {
3232
SCC_FoldSequences = 0x04,
3333
};
3434

35+
/// A callback function that is called once per emitted diagnostic with a
36+
/// byte offset and message text.
37+
typedef void (*swift_parser_diagnostic_hook_t)(ptrdiff_t off, const char *text, void *ctx);
38+
3539
/// Entry point for the Swift compiler to use for consistency checking.
3640
///
3741
/// - Parameters:
@@ -43,7 +47,8 @@ enum SwiftConsistencyCheckFlags {
4347
/// - Returns: 0 if all requested consistency checks passed, nonzero otherwise.
4448
int swift_parser_consistencyCheck(
4549
const char *buffer, ptrdiff_t bufferLength, const char *filename,
46-
unsigned int flags);
50+
unsigned int flags,
51+
void *hookCtx, swift_parser_diagnostic_hook_t hook);
4752

4853
#ifdef __cplusplus
4954
}

0 commit comments

Comments
 (0)