Skip to content

Commit cd46102

Browse files
authored
Merge pull request #13276 from dotty-staging/standalone-tasty-printer
2 parents 3dd15f7 + 80ee3e1 commit cd46102

File tree

9 files changed

+80
-40
lines changed

9 files changed

+80
-40
lines changed

compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,52 @@ import TastyUnpickler._
1111
import util.Spans.offsetToInt
1212
import printing.Highlighting._
1313
import dotty.tools.tasty.TastyFormat.{ASTsSection, PositionsSection, CommentsSection}
14+
import java.nio.file.{Files, Paths}
15+
import dotty.tools.io.{JarArchive, Path}
1416

1517
object TastyPrinter:
16-
def show(bytes: Array[Byte])(using Context): String =
18+
19+
def showContents(bytes: Array[Byte], noColor: Boolean): String =
1720
val printer =
18-
if ctx.settings.color.value == "never" then new TastyPrinter(bytes)
21+
if noColor then new TastyPrinter(bytes)
1922
else new TastyAnsiiPrinter(bytes)
2023
printer.showContents()
2124

25+
def main(args: Array[String]): Unit = {
26+
// TODO: Decouple CliCommand from Context and use CliCommand.distill?
27+
val lineWidth = 80
28+
val line = "-" * lineWidth
29+
val noColor = args.contains("-color:never")
30+
var printLastLine = false
31+
def printTasty(fileName: String, bytes: Array[Byte]): Unit =
32+
println(line)
33+
println(fileName)
34+
println(line)
35+
println(showContents(bytes, noColor))
36+
println()
37+
printLastLine = true
38+
for arg <- args do
39+
if arg == "-color:never" then () // skip
40+
else if arg.startsWith("-") then println(s"bad option '$arg' was ignored")
41+
else if arg.endsWith(".tasty") then {
42+
val path = Paths.get(arg)
43+
if Files.exists(path) then printTasty(arg, Files.readAllBytes(path))
44+
else println("File not found: " + arg)
45+
}
46+
else if arg.endsWith(".jar") then {
47+
val jar = JarArchive.open(Path(arg), create = false)
48+
try
49+
for file <- jar.iterator() if file.name.endsWith(".tasty") do
50+
printTasty(s"$arg ${file.path}", file.toByteArray)
51+
finally jar.close()
52+
53+
}
54+
else println(s"Not a '.tasty' or '.jar' file: $arg")
55+
56+
if printLastLine then
57+
println(line)
58+
}
59+
2260
class TastyPrinter(bytes: Array[Byte]) {
2361

2462
private val sb: StringBuilder = new StringBuilder

compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,8 @@ class DecompilationPrinter extends Phase {
4040

4141
private def printToOutput(out: PrintStream)(using Context): Unit = {
4242
val unit = ctx.compilationUnit
43-
if (ctx.settings.printTasty.value)
44-
println(TastyPrinter.show(unit.pickled.head._2()))
45-
else {
46-
val unitFile = unit.source.toString.replace("\\", "/").replace(".class", ".tasty")
47-
out.println(s"/** Decompiled from $unitFile */")
48-
out.println(QuotesImpl.showDecompiledTree(unit.tpdTree))
49-
}
43+
val unitFile = unit.source.toString.replace("\\", "/").replace(".class", ".tasty")
44+
out.println(s"/** Decompiled from $unitFile */")
45+
out.println(QuotesImpl.showDecompiledTree(unit.tpdTree))
5046
}
5147
}

compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ object PickledQuotes {
174174
positionWarnings.foreach(report.warning(_))
175175

176176
val pickled = pickler.assembleParts()
177-
quotePickling.println(s"**** pickled quote\n${TastyPrinter.show(pickled)}")
177+
quotePickling.println(s"**** pickled quote\n${TastyPrinter.showContents(pickled, ctx.settings.color.value == "never")}")
178178
pickled
179179
}
180180

@@ -195,7 +195,7 @@ object PickledQuotes {
195195
case pickled: String => TastyString.unpickle(pickled)
196196
case pickled: List[String] => TastyString.unpickle(pickled)
197197

198-
quotePickling.println(s"**** unpickling quote from TASTY\n${TastyPrinter.show(bytes)}")
198+
quotePickling.println(s"**** unpickling quote from TASTY\n${TastyPrinter.showContents(bytes, ctx.settings.color.value == "never")}")
199199

200200
val mode = if (isType) UnpickleMode.TypeTree else UnpickleMode.Term
201201
val unpickler = new DottyUnpickler(bytes, mode)

compiler/src/dotty/tools/dotc/transform/Pickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Pickler extends Phase {
9191
if pickling ne noPrinter then
9292
pickling.synchronized {
9393
println(i"**** pickled info of $cls")
94-
println(TastyPrinter.show(pickled))
94+
println(TastyPrinter.showContents(pickled, ctx.settings.color.value == "never"))
9595
}
9696
pickled
9797
}(using ExecutionContext.global)

compiler/test/dotty/tools/dotc/core/tasty/PathPicklingTest.scala

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dotty.tools.dotc.core.tasty
22

33
import java.io.{File => JFile, ByteArrayOutputStream, IOException}
4-
import java.nio.file.{Files, NoSuchFileException, Path, Paths}
4+
import java.nio.file.{Files, NoSuchFileException, Paths}
55

66
import scala.sys.process._
77

@@ -19,7 +19,7 @@ import dotty.tools.dotc.core.Mode
1919
import dotty.tools.dotc.core.Names.Name
2020
import dotty.tools.dotc.interfaces.Diagnostic.ERROR
2121
import dotty.tools.dotc.reporting.TestReporter
22-
import dotty.tools.io.{Directory, File, Path}
22+
import dotty.tools.io.{Directory, File, Path, JarArchive}
2323

2424
import dotty.tools.vulpix.TestConfiguration
2525

@@ -42,28 +42,24 @@ class PathPicklingTest {
4242
assertFalse("Compilation failed.", rep.hasErrors)
4343
}
4444

45-
val decompiled =
46-
val outstream = new ByteArrayOutputStream()
47-
val options = TestConfiguration.defaultOptions
48-
.and("-print-tasty")
49-
.and("-color:never")
50-
.and(s"$out/out.jar")
51-
val reporter = TestReporter.reporter(System.out, logLevel = ERROR)
52-
val rep = Console.withOut(outstream) {
53-
decompiler.Main.process(options.all, reporter)
54-
}
55-
assertFalse("Decompilation failed.", rep.hasErrors)
56-
new String(outstream.toByteArray(), "UTF-8")
45+
val printedTasty =
46+
val sb = new StringBuffer
47+
val jar = JarArchive.open(Path(s"$out/out.jar"), create = false)
48+
try
49+
for file <- jar.iterator() if file.name.endsWith(".tasty") do
50+
sb.append(TastyPrinter.showContents(file.toByteArray, noColor = true))
51+
finally jar.close()
52+
sb.toString()
5753

58-
assertTrue(decompiled.contains(": i10430/lib.scala"))
59-
assertTrue(decompiled.contains(": i10430/app.scala"))
60-
assertTrue(decompiled.contains("[i10430/lib.scala]"))
61-
assertTrue(decompiled.contains("[i10430/app.scala]"))
54+
assertTrue(printedTasty.contains(": i10430/lib.scala"))
55+
assertTrue(printedTasty.contains("[i10430/lib.scala]"))
56+
assertFalse(printedTasty.contains(": i10430\\lib.scala"))
57+
assertFalse(printedTasty.contains("[i10430\\lib.scala]"))
6258

63-
assertFalse(decompiled.contains(": i10430\\lib.scala"))
64-
assertFalse(decompiled.contains(": i10430\\app.scala"))
65-
assertFalse(decompiled.contains("[i10430\\lib.scala]"))
66-
assertFalse(decompiled.contains("[i10430\\app.scala]"))
59+
assertTrue(printedTasty.contains(": i10430/app.scala"))
60+
assertTrue(printedTasty.contains("[i10430/app.scala]"))
61+
assertFalse(printedTasty.contains(": i10430\\app.scala"))
62+
assertFalse(printedTasty.contains("[i10430\\app.scala]"))
6763
}
6864

6965
private def delete(file: JFile): Unit = {

dist/bin/common

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ default_java_opts="-Xmx768m -Xms768m"
200200

201201
CompilerMain=dotty.tools.dotc.Main
202202
DecompilerMain=dotty.tools.dotc.decompiler.Main
203+
TastyPrinterMain=dotty.tools.dotc.core.tasty.TastyPrinter
203204
ReplMain=dotty.tools.repl.Main
204205
ScriptingMain=dotty.tools.scripting.Main
205206

dist/bin/scalac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ case "$1" in
4545
while [[ $# -gt 0 ]]; do addScript "$1" && shift ; done ;;
4646
-compile) PROG_NAME="$CompilerMain" && shift ;;
4747
-decompile) PROG_NAME="$DecompilerMain" && shift ;;
48-
-print-tasty) PROG_NAME="$DecompilerMain" && addScala "-print-tasty" && shift ;;
48+
-print-tasty) PROG_NAME="$TastyPrinterMain" && shift ;;
4949
-run) PROG_NAME="$ReplMain" && shift ;;
5050
-colors) colors=true && shift ;;
5151
-no-colors) unset colors && shift ;;

project/Build.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,16 +640,17 @@ object Build {
640640
val printTasty = args0.contains("-print-tasty")
641641
val debugFromTasty = args0.contains("-Ythrough-tasty")
642642
val args = args0.filter(arg => arg != "-repl" && arg != "-decompile" &&
643-
arg != "-with-compiler" && arg != "-Ythrough-tasty")
643+
arg != "-with-compiler" && arg != "-Ythrough-tasty" && arg != "-print-tasty")
644644

645645
val main =
646-
if (decompile || printTasty) "dotty.tools.dotc.decompiler.Main"
646+
if (decompile) "dotty.tools.dotc.decompiler.Main"
647+
else if (printTasty) "dotty.tools.dotc.core.tasty.TastyPrinter"
647648
else if (debugFromTasty) "dotty.tools.dotc.fromtasty.Debug"
648649
else "dotty.tools.dotc.Main"
649650

650651
var extraClasspath = Seq(scalaLib, dottyLib)
651652

652-
if ((decompile || printTasty) && !args.contains("-classpath"))
653+
if (decompile && !args.contains("-classpath"))
653654
extraClasspath ++= Seq(".")
654655

655656
if (args0.contains("-with-compiler")) {
@@ -664,7 +665,7 @@ object Build {
664665
extraClasspath ++= Seq(dottyCompiler, dottyInterfaces, asm, dottyStaging, dottyTastyInspector, tastyCore)
665666
}
666667

667-
val fullArgs = main :: insertClasspathInArgs(args, extraClasspath.mkString(File.pathSeparator))
668+
val fullArgs = main :: (if (printTasty) args else insertClasspathInArgs(args, extraClasspath.mkString(File.pathSeparator)))
668669

669670
(Compile / runMain).toTask(fullArgs.mkString(" ", " ", ""))
670671
}.evaluated,

project/scripts/cmdTests

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ clear_out "$OUT"
2323
"$SBT" ";scalac $SOURCE ; scala $MAIN" > "$tmp"
2424
grep -qe "$EXPECTED_OUTPUT" "$tmp"
2525

26+
echo "testing sbt scalac -print-tasty"
27+
clear_out "$OUT"
28+
"$SBT" ";scalac $SOURCE -d $OUT ;scalac -print-tasty -color:never $TASTY" > "$tmp"
29+
grep -qe "0: ASTs" "$tmp"
30+
grep -qe "0: tests/pos/HelloWorld.scala" "$tmp"
31+
2632
echo "testing that paths SourceFile annotations are relativized"
2733
clear_out "$OUT"
2834
"$SBT" "scalac -d $OUT/out.jar -sourceroot tests/pos $(pwd)/tests/pos/i10430/lib.scala $(pwd)/tests/pos/i10430/app.scala"
29-
"$SBT" "scalac -decompile -print-tasty -color:never $OUT/out.jar" > "$tmp"
35+
"$SBT" "scalac -print-tasty -color:never $OUT/out.jar" > "$tmp"
3036
cat "$tmp" # for debugging
3137
grep -q ": i10430/lib.scala" "$tmp"
3238
grep -q ": i10430/app.scala" "$tmp"
@@ -82,3 +88,5 @@ clear_out "$OUT"
8288
# fi
8389
# fi
8490
# done 3<"$tmp1" 4<"./tests/vulpix-tests/meta/sbt-output.check"
91+
92+
echo "cmdTests successful"

0 commit comments

Comments
 (0)