Skip to content

Add an action to verify that round-tripping a source file works #565

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
Aug 9, 2022
Merged
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
29 changes: 29 additions & 0 deletions Sources/lit-test-helper/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func printHelp() {
-roundtrip
Parse the given source file (-source-file) and print it out using
its syntax tree.
-verify-roundtrip
Parse the given source file (-source-file) and exit with a non-zero
exit code if printing the tree does not result in the original
source file.
-print-tree
Parse the given source file (-source-file) and output its syntax
tree.
Expand Down Expand Up @@ -343,6 +347,8 @@ func performParseIncremental(args: CommandLineArguments) throws {
enum TestingError: Error, CustomStringConvertible {
case reparsedRegionsVerificationFailed(ByteSourceRange)
case classificationVerificationFailed(String, String)
case readingSourceFileFailed(URL)
case roundTripFailed

public var description: String {
switch self {
Expand All @@ -357,6 +363,10 @@ enum TestingError: Error, CustomStringConvertible {
--- CONSTRUCTED:
\(constructed)
"""
case .readingSourceFileFailed(let url):
return "Reading the source file at \(url) failed"
case .roundTripFailed:
return "Round-tripping the source file failed"
}
}
}
Expand Down Expand Up @@ -416,6 +426,20 @@ func performRoundtrip(args: CommandLineArguments) throws {
}
}

func performVerifyRoundtrip(args: CommandLineArguments) throws {
let sourceURL = URL(fileURLWithPath: try args.getRequired("-source-file"))
guard let source = try String(data: Data(contentsOf: sourceURL), encoding: .utf8) else {
throw TestingError.readingSourceFileFailed(sourceURL)
}
let versionInfo = getSwiftLanguageVersionInfo(args: args)
let useNewParser = args.has("-use-new-parser")

let tree = try SyntaxParser.parse(source: source, languageVersion: versionInfo.languageVersion, enableBareSlashRegexLiteral: versionInfo.enableBareSlashRegexLiteral)
if tree.description != source {
throw TestingError.roundTripFailed
}
}

class NodePrinter: SyntaxAnyVisitor {
init() {
super.init(viewMode: .sourceAccurate)
Expand Down Expand Up @@ -534,6 +558,8 @@ do {
try performParseIncremental(args: args)
} else if args.has("-roundtrip") {
try performRoundtrip(args: args)
} else if args.has("-verify-roundtrip") {
try performVerifyRoundtrip(args: args)
} else if args.has("-print-tree") {
try printSyntaxTree(args: args)
} else if args.has("-dump-diags") {
Expand All @@ -550,6 +576,9 @@ do {
exit(1)
}
exit(0)
} catch let error as TestingError {
printerr("\(error)")
exit(1)
} catch {
printerr("\(error)")
printerr("Run lit-test-helper -help for more help.")
Expand Down