Skip to content

lit-tester: add an action to diagnose regular parser errors and unknown syntax nodes #131

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
Jun 19, 2019
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
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