Skip to content

Commit c09d8a1

Browse files
authored
Merge pull request #976 from CodaFi/locavore
Bridge Parser Diagnostics into the C++ Diagnostic Engine
2 parents add30e7 + ef690f3 commit c09d8a1

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

Sources/SwiftCompilerSupport/ConsistencyCheck.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ extension Syntax {
3434
/// - flags: Flags that indicate what checks should be performed.
3535
/// 0x01: Perform round-trip checking.
3636
/// 0x02: Check for parser diagnostics.
37+
/// - hookCtx: A caller-provided context parameter to be passed back in the diagnostic hook.
38+
/// - diagnosticHook: A callback invoked once per diagnostic emitted by the parser.
3739
/// - Returns: 0 if all requested consistency checks passed, nonzero otherwise.
3840
@_cdecl("swift_parser_consistencyCheck")
3941
@_spi(SwiftCompiler)
4042
public func _parserConsistencyCheck(
4143
bufferPtr: UnsafePointer<UInt8>, bufferLength: Int,
42-
filename: UnsafePointer<UInt8>, flags: CUnsignedInt
44+
filename: UnsafePointer<UInt8>, flags: CUnsignedInt,
45+
hookCtx: OpaquePointer,
46+
diagnosticHook: @convention(c) (Int, UnsafePointer<Int8>, OpaquePointer) -> Void
4347
) -> CInt {
4448
let buffer = UnsafeBufferPointer<UInt8>(
4549
start: bufferPtr, count: bufferLength)
@@ -62,7 +66,6 @@ public func _parserConsistencyCheck(
6266
var anyDiags = false
6367

6468
let sourceFile = Syntax(raw: rawSourceFile.raw).as(SourceFileSyntax.self)!
65-
6669
let diags = ParseDiagnosticsGenerator.diagnostics(
6770
for: sourceFile)
6871
for diag in diags {
@@ -73,7 +76,9 @@ public func _parserConsistencyCheck(
7376
continue
7477
}
7578

76-
print("\(String(cString: filename)): error: \(diag.debugDescription)")
79+
diag.message.withCString { diagText in
80+
diagnosticHook(diag.position.utf8Offset, diagText, hookCtx)
81+
}
7782
anyDiags = true
7883
}
7984

Sources/SwiftCompilerSupport/SwiftCompilerSupport.h

Lines changed: 8 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:
@@ -40,10 +44,13 @@ enum SwiftConsistencyCheckFlags {
4044
/// - filename: The name of the source file, which is used only for diagnostics
4145
/// - flags: Flags that indicate what checks should be performed.
4246
/// 0x01: Perform round-trip checking.
47+
/// - hookCtx: A caller-provided context parameter to be passed back in the diagnostic hook.
48+
/// - diagnosticHook: A callback invoked once per diagnostic emitted by the parser.
4349
/// - Returns: 0 if all requested consistency checks passed, nonzero otherwise.
4450
int swift_parser_consistencyCheck(
4551
const char *buffer, ptrdiff_t bufferLength, const char *filename,
46-
unsigned int flags);
52+
unsigned int flags,
53+
void *hookCtx, swift_parser_diagnostic_hook_t hook);
4754

4855
#ifdef __cplusplus
4956
}

0 commit comments

Comments
 (0)