Skip to content

Commit

Permalink
Merge pull request swiftlang#131 from nkcsgexi/diagnose-parse
Browse files Browse the repository at this point in the history
lit-tester: add an action to diagnose regular parser errors and unknown syntax nodes
  • Loading branch information
nkcsgexi committed Jun 19, 2019
2 parents 0c74466 + aff63a9 commit aa56781
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/SyntaxParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public enum SyntaxParser {
let source = fileData.withUnsafeBytes { buf in
return String.fromBuffer(buf.bindMemory(to: UInt8.self))
}
return try parse(source: source, filenameForDiagnostics: url.absoluteString,
return try parse(source: source, filenameForDiagnostics: url.path,
diagnosticEngine: diagnosticEngine)
}

Expand Down
28 changes: 28 additions & 0 deletions Sources/lit-test-helper/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,32 @@ func printParserDiags(args: CommandLineArguments) throws {
_ = try SyntaxParser.parse(treeURL, diagnosticEngine: diagEngine)
}

func diagnose(args: CommandLineArguments) throws {
let treeURL = URL(fileURLWithPath: try args.getRequired("-source-file"))
let diagEngine = DiagnosticEngine()
diagEngine.addConsumer(PrintingDiagnosticConsumer())
let tree = try SyntaxParser.parse(treeURL, diagnosticEngine: diagEngine)
struct DiagnoseUnknown: SyntaxAnyVisitor {
let diagEngine: DiagnosticEngine
let converter: SourceLocationConverter
init(_ diagEngine: DiagnosticEngine, _ converter: SourceLocationConverter) {
self.diagEngine = diagEngine
self.converter = converter
}
func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind {
if node.isUnknown {
diagEngine.diagnose(Diagnostic.Message(.warning, "unknown syntax exists"),
location: node.startLocation(converter: converter,
afterLeadingTrivia: true))
}
return .visitChildren
}
}
var visitor = DiagnoseUnknown(diagEngine,
SourceLocationConverter(file: treeURL.path, tree: tree))
tree.walk(&visitor)
}

do {
let args = try CommandLineArguments.parse(CommandLine.arguments.dropFirst())

Expand All @@ -458,6 +484,8 @@ do {
try printSyntaxTree(args: args)
} else if args.has("-dump-diags") {
try printParserDiags(args: args)
} else if args.has("-diagnose") {
try diagnose(args: args)
} else if args.has("-help") {
printHelp()
} else {
Expand Down

0 comments on commit aa56781

Please sign in to comment.