Skip to content

Commit f32d200

Browse files
committed
Add decompiler tests
1 parent d8b72b2 commit f32d200

File tree

10 files changed

+249
-81
lines changed

10 files changed

+249
-81
lines changed
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package dotty.tools.dotc
22
package decompiler
33

4+
import java.io.{OutputStream, PrintStream}
5+
46
import dotty.tools.dotc.core.Contexts._
57
import dotty.tools.dotc.core.Phases.Phase
68
import dotty.tools.dotc.core.tasty.TastyPrinter
9+
import dotty.tools.io.{File, Path}
710

811
/** Phase that prints the trees in all loaded compilation units.
912
*
@@ -14,23 +17,39 @@ class DecompilationPrinter extends Phase {
1417
override def phaseName: String = "decompilationPrinter"
1518

1619
override def run(implicit ctx: Context): Unit = {
17-
val unit = ctx.compilationUnit
20+
val outputDir = ctx.settings.outputDir.value
21+
if (outputDir == ".") printToOutput(System.out)
22+
else {
23+
var os: OutputStream = null
24+
var ps: PrintStream = null
25+
try {
26+
os = File(outputDir + ".decompiled").outputStream()
27+
ps = new PrintStream(os)
28+
printToOutput(ps)
29+
} finally {
30+
if (os ne null) os.close()
31+
if (ps ne null) ps.close()
32+
}
33+
}
34+
}
1835

36+
private def printToOutput(out: PrintStream)(implicit ctx: Context): Unit = {
37+
val unit = ctx.compilationUnit
1938
val pageWidth = ctx.settings.pageWidth.value
2039

2140
val doubleLine = "=" * pageWidth
2241
val line = "-" * pageWidth
2342

24-
println(doubleLine)
25-
println(unit.source)
26-
println(line)
43+
out.println(doubleLine)
44+
out.println(unit.source)
45+
out.println(line)
2746

28-
println(unit.tpdTree.show)
29-
println(line)
47+
out.println(unit.tpdTree.show)
48+
out.println(line)
3049

3150
if (ctx.settings.printTasty.value) {
3251
new TastyPrinter(unit.pickled.head._2).printContents()
33-
println(line)
52+
out.println(line)
3453
}
3554
}
3655
}

compiler/test/dotty/tools/dotc/FromTastyTests.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class FromTastyTests extends ParallelTesting {
2626
// > dotc -Ythrough-tasty -Ycheck:all <source>
2727

2828
implicit val testGroup: TestGroup = TestGroup("posTestFromTasty")
29-
val (step1, step2) = compileTastyInDir("../tests/pos", defaultOptions,
29+
val (step1, step2, step3) = compileTastyInDir("../tests/pos", defaultOptions,
3030
blacklist = Set(
3131
"NoCyclicReference.scala",
3232
"depfuntype.scala",
@@ -53,7 +53,8 @@ class FromTastyTests extends ParallelTesting {
5353
)
5454
step1.checkCompile() // Compile all files to generate the class files with tasty
5555
step2.checkCompile() // Compile from tasty
56-
(step1 + step2).delete()
56+
step3.checkCompile() // Decompile from tasty
57+
(step1 + step2 + step3).delete()
5758
}
5859

5960
@Test def runTestFromTasty: Unit = {
@@ -63,7 +64,7 @@ class FromTastyTests extends ParallelTesting {
6364
// > dotr Test
6465

6566
implicit val testGroup: TestGroup = TestGroup("runTestFromTasty")
66-
val (step1, step2) = compileTastyInDir("../tests/run", defaultOptions,
67+
val (step1, step2, step3) = compileTastyInDir("../tests/run", defaultOptions,
6768
blacklist = Set(
6869
"Course-2002-13.scala",
6970
"bridges.scala",
@@ -93,7 +94,8 @@ class FromTastyTests extends ParallelTesting {
9394
)
9495
step1.checkCompile() // Compile all files to generate the class files with tasty
9596
step2.checkRuns() // Compile from tasty and run the result
96-
(step1 + step2).delete()
97+
step3.checkCompile() // Decompile from tasty
98+
(step1 + step2 + step3).delete()
9799
}
98100

99101
private implicit class tastyCompilationTuples(tup: (CompilationTest, CompilationTest)) {

0 commit comments

Comments
 (0)