Skip to content

Commit 68c697d

Browse files
authored
Merge pull request #565 from ahoppen/pr/verify-roundtrip-action
Add an action to verify that round-tripping a source file works
2 parents 342a9d2 + 49ae27b commit 68c697d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Sources/lit-test-helper/main.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func printHelp() {
3636
-roundtrip
3737
Parse the given source file (-source-file) and print it out using
3838
its syntax tree.
39+
-verify-roundtrip
40+
Parse the given source file (-source-file) and exit with a non-zero
41+
exit code if printing the tree does not result in the original
42+
source file.
3943
-print-tree
4044
Parse the given source file (-source-file) and output its syntax
4145
tree.
@@ -343,6 +347,8 @@ func performParseIncremental(args: CommandLineArguments) throws {
343347
enum TestingError: Error, CustomStringConvertible {
344348
case reparsedRegionsVerificationFailed(ByteSourceRange)
345349
case classificationVerificationFailed(String, String)
350+
case readingSourceFileFailed(URL)
351+
case roundTripFailed
346352

347353
public var description: String {
348354
switch self {
@@ -357,6 +363,10 @@ enum TestingError: Error, CustomStringConvertible {
357363
--- CONSTRUCTED:
358364
\(constructed)
359365
"""
366+
case .readingSourceFileFailed(let url):
367+
return "Reading the source file at \(url) failed"
368+
case .roundTripFailed:
369+
return "Round-tripping the source file failed"
360370
}
361371
}
362372
}
@@ -416,6 +426,20 @@ func performRoundtrip(args: CommandLineArguments) throws {
416426
}
417427
}
418428

429+
func performVerifyRoundtrip(args: CommandLineArguments) throws {
430+
let sourceURL = URL(fileURLWithPath: try args.getRequired("-source-file"))
431+
guard let source = try String(data: Data(contentsOf: sourceURL), encoding: .utf8) else {
432+
throw TestingError.readingSourceFileFailed(sourceURL)
433+
}
434+
let versionInfo = getSwiftLanguageVersionInfo(args: args)
435+
let useNewParser = args.has("-use-new-parser")
436+
437+
let tree = try SyntaxParser.parse(source: source, languageVersion: versionInfo.languageVersion, enableBareSlashRegexLiteral: versionInfo.enableBareSlashRegexLiteral)
438+
if tree.description != source {
439+
throw TestingError.roundTripFailed
440+
}
441+
}
442+
419443
class NodePrinter: SyntaxAnyVisitor {
420444
init() {
421445
super.init(viewMode: .sourceAccurate)
@@ -534,6 +558,8 @@ do {
534558
try performParseIncremental(args: args)
535559
} else if args.has("-roundtrip") {
536560
try performRoundtrip(args: args)
561+
} else if args.has("-verify-roundtrip") {
562+
try performVerifyRoundtrip(args: args)
537563
} else if args.has("-print-tree") {
538564
try printSyntaxTree(args: args)
539565
} else if args.has("-dump-diags") {
@@ -550,6 +576,9 @@ do {
550576
exit(1)
551577
}
552578
exit(0)
579+
} catch let error as TestingError {
580+
printerr("\(error)")
581+
exit(1)
553582
} catch {
554583
printerr("\(error)")
555584
printerr("Run lit-test-helper -help for more help.")

0 commit comments

Comments
 (0)