Skip to content
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

Decouple -print-tasty from compiler #13276

Merged
merged 2 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 40 additions & 2 deletions compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,52 @@ import TastyUnpickler._
import util.Spans.offsetToInt
import printing.Highlighting._
import dotty.tools.tasty.TastyFormat.{ASTsSection, PositionsSection, CommentsSection}
import java.nio.file.{Files, Paths}
import dotty.tools.io.{JarArchive, Path}

object TastyPrinter:
def show(bytes: Array[Byte])(using Context): String =

def showContents(bytes: Array[Byte], noColor: Boolean): String =
val printer =
if ctx.settings.color.value == "never" then new TastyPrinter(bytes)
if noColor then new TastyPrinter(bytes)
else new TastyAnsiiPrinter(bytes)
printer.showContents()

def main(args: Array[String]): Unit = {
// TODO: Decouple CliCommand from Context and use CliCommand.distill?
val lineWidth = 80
val line = "-" * lineWidth
val noColor = args.contains("-color:never")
var printLastLine = false
def printTasty(fileName: String, bytes: Array[Byte]): Unit =
println(line)
println(fileName)
println(line)
println(showContents(bytes, noColor))
println()
printLastLine = true
for arg <- args if arg != "-print-tasty" do
if arg == "-print-tasty" || arg == "-color:never" then () // skip
else if arg.startsWith("-") then println(s"bad option '$arg' was ignored")
else if arg.endsWith(".tasty") then {
val path = Paths.get(arg)
if Files.exists(path) then printTasty(arg, Files.readAllBytes(path))
else println("File not found: " + arg)
}
else if arg.endsWith(".jar") then {
val jar = JarArchive.open(Path(arg), create = false)
try
for file <- jar.iterator() if file.name.endsWith(".tasty") do
printTasty(s"$arg ${file.path}", file.toByteArray)
finally jar.close()

}
else println(s"Not a '.tasty' or '.jar' file: $arg")

if printLastLine then
println(line)
}

class TastyPrinter(bytes: Array[Byte]) {

private val sb: StringBuilder = new StringBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ class DecompilationPrinter extends Phase {

private def printToOutput(out: PrintStream)(using Context): Unit = {
val unit = ctx.compilationUnit
if (ctx.settings.printTasty.value)
dwijnand marked this conversation as resolved.
Show resolved Hide resolved
println(TastyPrinter.show(unit.pickled.head._2()))
else {
val unitFile = unit.source.toString.replace("\\", "/").replace(".class", ".tasty")
out.println(s"/** Decompiled from $unitFile */")
out.println(QuotesImpl.showDecompiledTree(unit.tpdTree))
}
val unitFile = unit.source.toString.replace("\\", "/").replace(".class", ".tasty")
out.println(s"/** Decompiled from $unitFile */")
out.println(QuotesImpl.showDecompiledTree(unit.tpdTree))
}
}
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ object PickledQuotes {
positionWarnings.foreach(report.warning(_))

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

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

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

val mode = if (isType) UnpickleMode.TypeTree else UnpickleMode.Term
val unpickler = new DottyUnpickler(bytes, mode)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/Pickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Pickler extends Phase {
if pickling ne noPrinter then
pickling.synchronized {
println(i"**** pickled info of $cls")
println(TastyPrinter.show(pickled))
println(TastyPrinter.showContents(pickled, ctx.settings.color.value == "never"))
}
pickled
}(using ExecutionContext.global)
Expand Down
40 changes: 18 additions & 22 deletions compiler/test/dotty/tools/dotc/core/tasty/PathPicklingTest.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dotty.tools.dotc.core.tasty

import java.io.{File => JFile, ByteArrayOutputStream, IOException}
import java.nio.file.{Files, NoSuchFileException, Path, Paths}
import java.nio.file.{Files, NoSuchFileException, Paths}

import scala.sys.process._

Expand All @@ -19,7 +19,7 @@ import dotty.tools.dotc.core.Mode
import dotty.tools.dotc.core.Names.Name
import dotty.tools.dotc.interfaces.Diagnostic.ERROR
import dotty.tools.dotc.reporting.TestReporter
import dotty.tools.io.{Directory, File, Path}
import dotty.tools.io.{Directory, File, Path, JarArchive}

import dotty.tools.vulpix.TestConfiguration

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

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

assertTrue(decompiled.contains(": i10430/lib.scala"))
assertTrue(decompiled.contains(": i10430/app.scala"))
assertTrue(decompiled.contains("[i10430/lib.scala]"))
assertTrue(decompiled.contains("[i10430/app.scala]"))
assertTrue(printedTasty.contains(": i10430/lib.scala"))
assertTrue(printedTasty.contains("[i10430/lib.scala]"))
assertFalse(printedTasty.contains(": i10430\\lib.scala"))
assertFalse(printedTasty.contains("[i10430\\lib.scala]"))

assertFalse(decompiled.contains(": i10430\\lib.scala"))
assertFalse(decompiled.contains(": i10430\\app.scala"))
assertFalse(decompiled.contains("[i10430\\lib.scala]"))
assertFalse(decompiled.contains("[i10430\\app.scala]"))
assertTrue(printedTasty.contains(": i10430/app.scala"))
assertTrue(printedTasty.contains("[i10430/app.scala]"))
assertFalse(printedTasty.contains(": i10430\\app.scala"))
assertFalse(printedTasty.contains("[i10430\\app.scala]"))
}

private def delete(file: JFile): Unit = {
Expand Down
1 change: 1 addition & 0 deletions dist/bin/common
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ default_java_opts="-Xmx768m -Xms768m"

CompilerMain=dotty.tools.dotc.Main
DecompilerMain=dotty.tools.dotc.decompiler.Main
TastyPrinterMain=dotty.tools.dotc.core.tasty.TastyPrinter
ReplMain=dotty.tools.repl.Main
ScriptingMain=dotty.tools.scripting.Main

Expand Down
2 changes: 1 addition & 1 deletion dist/bin/scalac
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ case "$1" in
while [[ $# -gt 0 ]]; do addScript "$1" && shift ; done ;;
-compile) PROG_NAME="$CompilerMain" && shift ;;
-decompile) PROG_NAME="$DecompilerMain" && shift ;;
-print-tasty) PROG_NAME="$DecompilerMain" && addScala "-print-tasty" && shift ;;
-print-tasty) PROG_NAME="$TastyPrinterMain" && shift ;;
-run) PROG_NAME="$ReplMain" && shift ;;
-colors) colors=true && shift ;;
-no-colors) unset colors && shift ;;
Expand Down
7 changes: 4 additions & 3 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -643,13 +643,14 @@ object Build {
arg != "-with-compiler" && arg != "-Ythrough-tasty")

val main =
if (decompile || printTasty) "dotty.tools.dotc.decompiler.Main"
if (decompile) "dotty.tools.dotc.decompiler.Main"
else if (printTasty) "dotty.tools.dotc.core.tasty.TastyPrinter"
else if (debugFromTasty) "dotty.tools.dotc.fromtasty.Debug"
else "dotty.tools.dotc.Main"

var extraClasspath = Seq(scalaLib, dottyLib)

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

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

val fullArgs = main :: insertClasspathInArgs(args, extraClasspath.mkString(File.pathSeparator))
val fullArgs = main :: (if (printTasty) args else insertClasspathInArgs(args, extraClasspath.mkString(File.pathSeparator)))

(Compile / runMain).toTask(fullArgs.mkString(" ", " ", ""))
}.evaluated,
Expand Down
10 changes: 9 additions & 1 deletion project/scripts/cmdTests
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ clear_out "$OUT"
"$SBT" ";scalac $SOURCE ; scala $MAIN" > "$tmp"
grep -qe "$EXPECTED_OUTPUT" "$tmp"

echo "testing sbt scalac -print-tasty"
clear_out "$OUT"
"$SBT" ";scalac $SOURCE -d $OUT ;scalac -print-tasty -color:never $TASTY" > "$tmp"
grep -qe "0: ASTs" "$tmp"
grep -qe "0: tests/pos/HelloWorld.scala" "$tmp"

echo "testing that paths SourceFile annotations are relativized"
clear_out "$OUT"
"$SBT" "scalac -d $OUT/out.jar -sourceroot tests/pos $(pwd)/tests/pos/i10430/lib.scala $(pwd)/tests/pos/i10430/app.scala"
"$SBT" "scalac -decompile -print-tasty -color:never $OUT/out.jar" > "$tmp"
"$SBT" "scalac -print-tasty -color:never $OUT/out.jar" > "$tmp"
cat "$tmp" # for debugging
grep -q ": i10430/lib.scala" "$tmp"
grep -q ": i10430/app.scala" "$tmp"
Expand Down Expand Up @@ -82,3 +88,5 @@ clear_out "$OUT"
# fi
# fi
# done 3<"$tmp1" 4<"./tests/vulpix-tests/meta/sbt-output.check"

echo "cmdTests successful"