From 5add9b78927ff0715ae5214859adb111044491e6 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 30 Nov 2023 12:00:46 +0100 Subject: [PATCH 1/3] Homogenize TASTy printer formatting --- compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala index 8950f4509d3e..e2e18dc57d5b 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala @@ -256,12 +256,11 @@ class TastyPrinter(bytes: Array[Byte]) { def unpickle(reader: TastyReader, tastyName: NameTable): String = { import reader.* - sb.append(s" ${reader.endAddr.index - reader.currentAddr.index}") val attributes = new AttributeUnpickler(reader).attributes - sb.append(s" attributes bytes:\n") + sb.append(s"Attributes (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n") for tag <- attributes.booleanTags do - sb.append(" ").append(attributeTagToString(tag)).append("\n") + sb.append(" ").append(attributeTagToString(tag)).append("\n") sb.result } From d74496e84cd9f579eb087c4959885348ed4fa69f Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 1 Dec 2023 14:21:52 +0100 Subject: [PATCH 2/3] Reuse the `StringBuilder` in `TastyPrinter` --- .../tools/dotc/core/tasty/TastyPrinter.scala | 73 ++++++------------- 1 file changed, 21 insertions(+), 52 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala index e2e18dc57d5b..e132d8749a71 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala @@ -64,8 +64,6 @@ object TastyPrinter: class TastyPrinter(bytes: Array[Byte]) { - private val sb: StringBuilder = new StringBuilder - class TastyPrinterUnpickler extends TastyUnpickler(bytes) { var namesStart: Addr = uninitialized var namesEnd: Addr = uninitialized @@ -83,7 +81,7 @@ class TastyPrinter(bytes: Array[Byte]) { private def nameRefToString(ref: NameRef): String = nameToString(nameAtRef(ref)) - private def printHeader(): Unit = + private def printHeader(sb: StringBuilder): Unit = val header = unpickler.header sb.append("Header:\n") sb.append(s" version: ${header.majorVersion}.${header.minorVersion}.${header.experimentalVersion}\n") @@ -91,7 +89,7 @@ class TastyPrinter(bytes: Array[Byte]) { sb.append(" UUID: ").append(header.uuid).append("\n") sb.append("\n") - private def printNames(): Unit = + private def printNames(sb: StringBuilder): Unit = sb.append(s"Names (${unpickler.namesEnd.index - unpickler.namesStart.index} bytes, starting from ${unpickler.namesStart.index}):\n") for ((name, idx) <- nameAtRef.contents.zipWithIndex) { val index = nameStr("%6d".format(idx)) @@ -99,33 +97,19 @@ class TastyPrinter(bytes: Array[Byte]) { } def showContents(): String = { - printHeader() - printNames() - unpickle(new TreeSectionUnpickler) match { - case Some(s) => sb.append("\n\n").append(s) - case _ => - } - unpickle(new PositionSectionUnpickler) match { - case Some(s) => sb.append("\n\n").append(s) - case _ => - } - unpickle(new CommentSectionUnpickler) match { - case Some(s) => sb.append("\n\n").append(s) - case _ => - } - unpickle(new AttributesSectionUnpickler) match { - case Some(s) => sb.append("\n\n").append(s) - case _ => - } + val sb: StringBuilder = new StringBuilder + printHeader(sb) + printNames(sb) + unpickle(new TreeSectionUnpickler(sb)) + unpickle(new PositionSectionUnpickler(sb)) + unpickle(new CommentSectionUnpickler(sb)) + unpickle(new AttributesSectionUnpickler(sb)) sb.result } - class TreeSectionUnpickler extends SectionUnpickler[String](ASTsSection) { + class TreeSectionUnpickler(sb: StringBuilder) extends SectionUnpickler[Unit](ASTsSection) { import dotty.tools.tasty.TastyFormat.* - - private val sb: StringBuilder = new StringBuilder - - def unpickle(reader: TastyReader, tastyName: NameTable): String = { + def unpickle(reader: TastyReader, tastyName: NameTable): Unit = { import reader.* var indent = 0 def newLine() = { @@ -186,23 +170,19 @@ class TastyPrinter(bytes: Array[Byte]) { } indent -= 2 } - sb.append(s"Trees (${endAddr.index - startAddr.index} bytes, starting from $base):") + sb.append(s"\n\nTrees (${endAddr.index - startAddr.index} bytes, starting from $base):") while (!isAtEnd) { printTree() newLine() } - sb.result } } - class PositionSectionUnpickler extends SectionUnpickler[String](PositionsSection) { - - private val sb: StringBuilder = new StringBuilder - - def unpickle(reader: TastyReader, tastyName: NameTable): String = { + class PositionSectionUnpickler(sb: StringBuilder) extends SectionUnpickler[Unit](PositionsSection) { + def unpickle(reader: TastyReader, tastyName: NameTable): Unit = { import reader.* val posUnpickler = new PositionUnpickler(reader, tastyName) - sb.append(s"Positions (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n") + sb.append(s"\n\nPositions (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n") val lineSizes = posUnpickler.lineSizes sb.append(s" lines: ${lineSizes.length}\n") sb.append(s" line sizes:\n") @@ -226,43 +206,32 @@ class TastyPrinter(bytes: Array[Byte]) { sb.append(nameStr(s"${nameRef.index} [${tastyName(nameRef)}]")) sb.append("\n") } - - sb.result } } - class CommentSectionUnpickler extends SectionUnpickler[String](CommentsSection) { - - private val sb: StringBuilder = new StringBuilder - - def unpickle(reader: TastyReader, tastyName: NameTable): String = { + class CommentSectionUnpickler(sb: StringBuilder) extends SectionUnpickler[Unit](CommentsSection) { + def unpickle(reader: TastyReader, tastyName: NameTable): Unit = { import reader.* val comments = new CommentUnpickler(reader).comments if !comments.isEmpty then - sb.append(s"Comments (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n") + sb.append(s"\n\nComments (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n") val sorted = comments.toSeq.sortBy(_._1.index) for ((addr, cmt) <- sorted) { sb.append(treeStr("%6d".format(addr.index))) sb.append(s": ${cmt.raw} (expanded = ${cmt.isExpanded})\n") } - sb.result } } - class AttributesSectionUnpickler extends SectionUnpickler[String](AttributesSection) { + class AttributesSectionUnpickler(sb: StringBuilder) extends SectionUnpickler[Unit](AttributesSection) { import dotty.tools.tasty.TastyFormat.* - - private val sb: StringBuilder = new StringBuilder - - def unpickle(reader: TastyReader, tastyName: NameTable): String = { + def unpickle(reader: TastyReader, tastyName: NameTable): Unit = { import reader.* val attributes = new AttributeUnpickler(reader).attributes - sb.append(s"Attributes (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n") + sb.append(s"\n\nAttributes (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n") for tag <- attributes.booleanTags do sb.append(" ").append(attributeTagToString(tag)).append("\n") - - sb.result } } From 9b1a32e60e0db559748a97a7057fc7155d4a6965 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 1 Dec 2023 14:25:45 +0100 Subject: [PATCH 3/3] Fix source path index color --- compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala index e132d8749a71..b2094bd8a94a 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala @@ -202,7 +202,8 @@ class TastyPrinter(bytes: Array[Byte]) { sb.append(s"\n source paths:\n") val sortedPath = sources.toSeq.sortBy(_._1.index) for ((addr, nameRef) <- sortedPath) { - sb.append(treeStr("%6d: ".format(addr.index))) + sb.append(treeStr("%6d".format(addr.index))) + sb.append(": ") sb.append(nameStr(s"${nameRef.index} [${tastyName(nameRef)}]")) sb.append("\n") }