Skip to content

Commit 1911d4e

Browse files
committed
Added a --bare option to ShowPickled so I can d...
Added a --bare option to ShowPickled so I can diff signatures without all the explicit indices blowing any points of similarity. No review.
1 parent 52f8509 commit 1911d4e

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

src/compiler/scala/tools/nsc/util/CommandLineParser.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ trait ParserUtil extends Parsers {
2525

2626
class CommandLine(val args: List[String], val unaryArguments: List[String]) {
2727
def this(args: List[String]) = this(args, Nil)
28+
def this(args: Array[String]) = this(args.toList)
29+
def this(args: Array[String], unaryArguments: List[String]) = this(args.toList, unaryArguments)
2830
def this(line: String) = this(CommandLineParser tokenize line)
2931

3032
def withUnaryArguments(xs: List[String]) = new CommandLine(args, xs)

src/compiler/scala/tools/nsc/util/ShowPickled.scala

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,31 @@ object ShowPickled extends Names {
7070
case _ => "***BAD TAG***(" + tag + ")"
7171
}
7272

73-
def printFile(buf: PickleBuffer, out: PrintStream) {
73+
def printFile(buf: PickleBuffer, out: PrintStream): Unit = printFile(buf, out, false)
74+
def printFile(buf: PickleBuffer, out: PrintStream, bare: Boolean) {
7475
out.println("Version " + buf.readNat() + "." + buf.readNat())
7576
val index = buf.createIndex
7677

78+
/** A print wrapper which discards everything if bare is true.
79+
*/
80+
def p(s: String) = if (!bare) out print s
81+
7782
def printNameRef() {
7883
val x = buf.readNat()
7984
val savedIndex = buf.readIndex
8085
buf.readIndex = index(x)
8186
val tag = buf.readByte()
8287
val len = buf.readNat()
83-
out.print(" " + x + "(" + newTermName(buf.bytes, buf.readIndex, len) + ")")
88+
val name = newTermName(buf.bytes, buf.readIndex, len)
89+
90+
val toPrint = if (bare) " " + name else " %s(%s)".format(x, name)
91+
out print toPrint
92+
8493
buf.readIndex = savedIndex
8594
}
8695

87-
def printNat() = out.print(" " + buf.readNat())
96+
def printNat() = p(" " + buf.readNat())
97+
8898
def printSymbolRef() = printNat()
8999
def printTypeRef() = printNat()
90100
def printConstantRef() = printNat()
@@ -107,12 +117,12 @@ object ShowPickled extends Names {
107117
*/
108118
def printEntry(i: Int) {
109119
buf.readIndex = index(i)
110-
out.print(i + "," + buf.readIndex + ": ")
120+
p(i + "," + buf.readIndex + ": ")
111121
val tag = buf.readByte()
112122
out.print(tag2string(tag))
113123
val len = buf.readNat()
114124
val end = len + buf.readIndex
115-
out.print(" " + len + ":")
125+
p(" " + len + ":")
116126
tag match {
117127
case TERMname =>
118128
out.print(" ")
@@ -196,26 +206,28 @@ object ShowPickled extends Names {
196206
for (i <- 0 until index.length) printEntry(i)
197207
}
198208

199-
def fromBytes(what: String, data: => Array[Byte]): Boolean = {
200-
try {
201-
val pickle = new PickleBuffer(data, 0, data.length)
202-
Console.println(what + ": ")
203-
printFile(pickle, Console.out)
204-
true
205-
}
206-
catch {
207-
case _: Exception => false
208-
}
209-
}
209+
def fromFile(path: String) = fromBytes(io.File(path).toByteArray)
210+
def fromName(name: String) = fromBytes(scalaSigBytesForPath(name) getOrElse Array())
211+
def fromBytes(data: => Array[Byte]): Option[PickleBuffer] =
212+
try Some(new PickleBuffer(data, 0, data.length))
213+
catch { case _: Exception => None }
210214

211-
def fromFile(path: String) = fromBytes(path, io.File(path).toByteArray)
212-
def fromName(name: String) = fromBytes(name, scalaSigBytesForPath(name) getOrElse Array())
215+
def show(what: String, pickle: PickleBuffer, bare: Boolean) = {
216+
Console.println(what + ": ")
217+
printFile(pickle, Console.out, bare)
218+
}
213219

220+
/** Option --bare suppresses numbers so the output can be diffed.
221+
*/
214222
def main(args: Array[String]) {
215-
args foreach { arg =>
216-
val res = fromFile(arg) || fromName(arg)
217-
if (!res)
218-
Console.println("Cannot read " + arg)
223+
val parsed = new CommandLine(args, List("--bare"))
224+
def isBare = parsed isSet "--bare"
225+
226+
parsed.residualArgs foreach { arg =>
227+
(fromFile(arg) orElse fromName(arg)) match {
228+
case Some(pb) => show(arg, pb, isBare)
229+
case _ => Console.println("Cannot read " + arg)
230+
}
219231
}
220232
}
221233
}

tools/showPickled

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Shows the pickled scala data in a classfile.
44

55
if [ $# == 0 ] ; then
6-
echo "Usage: $0 [-cp classpath] class class [...]"
6+
echo "Usage: $0 [--bare] [-cp classpath] <class*>"
77
exit 1
88
fi
99

0 commit comments

Comments
 (0)