Skip to content

Bridge Parser Diagnostics into the C++ Diagnostic Engine #976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Sources/SwiftCompilerSupport/ConsistencyCheck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ extension Syntax {
/// - flags: Flags that indicate what checks should be performed.
/// 0x01: Perform round-trip checking.
/// 0x02: Check for parser diagnostics.
/// - hookCtx: A caller-provided context parameter to be passed back in the diagnostic hook.
/// - diagnosticHook: A callback invoked once per diagnostic emitted by the parser.
/// - Returns: 0 if all requested consistency checks passed, nonzero otherwise.
@_cdecl("swift_parser_consistencyCheck")
@_spi(SwiftCompiler)
public func _parserConsistencyCheck(
bufferPtr: UnsafePointer<UInt8>, bufferLength: Int,
filename: UnsafePointer<UInt8>, flags: CUnsignedInt
filename: UnsafePointer<UInt8>, flags: CUnsignedInt,
hookCtx: OpaquePointer,
diagnosticHook: @convention(c) (Int, UnsafePointer<Int8>, OpaquePointer) -> Void
) -> CInt {
let buffer = UnsafeBufferPointer<UInt8>(
start: bufferPtr, count: bufferLength)
Expand All @@ -62,7 +66,6 @@ public func _parserConsistencyCheck(
var anyDiags = false

let sourceFile = Syntax(raw: rawSourceFile.raw).as(SourceFileSyntax.self)!

let diags = ParseDiagnosticsGenerator.diagnostics(
for: sourceFile)
for diag in diags {
Expand All @@ -73,7 +76,9 @@ public func _parserConsistencyCheck(
continue
}

print("\(String(cString: filename)): error: \(diag.debugDescription)")
diag.message.withCString { diagText in
diagnosticHook(diag.position.utf8Offset, diagText, hookCtx)
}
anyDiags = true
}

Expand Down
9 changes: 8 additions & 1 deletion Sources/SwiftCompilerSupport/SwiftCompilerSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ enum SwiftConsistencyCheckFlags {
SCC_FoldSequences = 0x04,
};

/// A callback function that is called once per emitted diagnostic with a
/// byte offset and message text.
typedef void (*swift_parser_diagnostic_hook_t)(ptrdiff_t off, const char *text, void *ctx);

/// Entry point for the Swift compiler to use for consistency checking.
///
/// - Parameters:
Expand All @@ -40,10 +44,13 @@ enum SwiftConsistencyCheckFlags {
/// - filename: The name of the source file, which is used only for diagnostics
/// - flags: Flags that indicate what checks should be performed.
/// 0x01: Perform round-trip checking.
/// - hookCtx: A caller-provided context parameter to be passed back in the diagnostic hook.
/// - diagnosticHook: A callback invoked once per diagnostic emitted by the parser.
/// - Returns: 0 if all requested consistency checks passed, nonzero otherwise.
int swift_parser_consistencyCheck(
const char *buffer, ptrdiff_t bufferLength, const char *filename,
unsigned int flags);
unsigned int flags,
void *hookCtx, swift_parser_diagnostic_hook_t hook);

#ifdef __cplusplus
}
Expand Down