@@ -36,6 +36,10 @@ func printHelp() {
36
36
-roundtrip
37
37
Parse the given source file (-source-file) and print it out using
38
38
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.
39
43
-print-tree
40
44
Parse the given source file (-source-file) and output its syntax
41
45
tree.
@@ -343,6 +347,8 @@ func performParseIncremental(args: CommandLineArguments) throws {
343
347
enum TestingError : Error , CustomStringConvertible {
344
348
case reparsedRegionsVerificationFailed( ByteSourceRange )
345
349
case classificationVerificationFailed( String , String )
350
+ case readingSourceFileFailed( URL )
351
+ case roundTripFailed
346
352
347
353
public var description : String {
348
354
switch self {
@@ -357,6 +363,10 @@ enum TestingError: Error, CustomStringConvertible {
357
363
--- CONSTRUCTED:
358
364
\( constructed)
359
365
"""
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 "
360
370
}
361
371
}
362
372
}
@@ -416,6 +426,20 @@ func performRoundtrip(args: CommandLineArguments) throws {
416
426
}
417
427
}
418
428
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
+
419
443
class NodePrinter : SyntaxAnyVisitor {
420
444
init ( ) {
421
445
super. init ( viewMode: . sourceAccurate)
534
558
try performParseIncremental ( args: args)
535
559
} else if args. has ( " -roundtrip " ) {
536
560
try performRoundtrip ( args: args)
561
+ } else if args. has ( " -verify-roundtrip " ) {
562
+ try performVerifyRoundtrip ( args: args)
537
563
} else if args. has ( " -print-tree " ) {
538
564
try printSyntaxTree ( args: args)
539
565
} else if args. has ( " -dump-diags " ) {
550
576
exit ( 1 )
551
577
}
552
578
exit ( 0 )
579
+ } catch let error as TestingError {
580
+ printerr ( " \( error) " )
581
+ exit ( 1 )
553
582
} catch {
554
583
printerr ( " \( error) " )
555
584
printerr ( " Run lit-test-helper -help for more help. " )
0 commit comments