[SwiftSyntax] Add debug print when the JSONDecoder fails to decode JSON (#14056)

from the compiler's '-emit-syntax'.

Investigating: rdar://problem/36379512
This commit is contained in:
Rintaro Ishizaki
2018-01-22 23:44:18 +09:00
committed by GitHub
parent 99229582ee
commit 840166c3ea
2 changed files with 11 additions and 5 deletions

View File

@@ -25,6 +25,7 @@ import Glibc
public enum ParserError: Error {
case swiftcFailed(Int, String)
case invalidFile
case jsonDecodeError(input: String, originalError: Error);
}
extension Syntax {
@@ -42,8 +43,16 @@ extension Syntax {
guard result.wasSuccessful else {
throw ParserError.swiftcFailed(result.exitCode, result.stderr)
}
let jsonData = result.stdoutData
let decoder = JSONDecoder()
let raw = try decoder.decode(RawSyntax.self, from: result.stdoutData)
let raw: RawSyntax
do {
raw = try decoder.decode(RawSyntax.self, from: jsonData)
} catch let err {
throw ParserError.jsonDecodeError(
input: String(data: jsonData, encoding: .utf8) ?? jsonData.base64EncodedString(),
originalError: err)
}
guard let file = Syntax.fromRaw(raw) as? SourceFileSyntax else {
throw ParserError.invalidFile
}