Skip to content

Escape chars when printing strings in decompiler #4644

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

Merged
merged 3 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/StdNames.scala
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ object StdNames {
class ScalaTermNames extends ScalaNames[TermName] {
protected implicit def fromString(s: String): TermName = termName(s)

@switch def syntheticParamName(i: Int): TermName = i match {
def syntheticParamName(i: Int): TermName = (i: @switch) match {
case 0 => x_0
case 1 => x_1
case 2 => x_2
Expand All @@ -702,7 +702,7 @@ object StdNames {
case _ => termName("x$" + i)
}

@switch def productAccessorName(j: Int): TermName = j match {
def productAccessorName(j: Int): TermName = (j: @switch) match {
case 1 => nme._1
case 2 => nme._2
case 3 => nme._3
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ class PlainPrinter(_ctx: Context) extends Printer {

def toText(denot: Denotation): Text = toText(denot.symbol) ~ "/D"

@switch private def escapedChar(ch: Char): String = ch match {
private def escapedChar(ch: Char): String = (ch: @switch) match {
case '\b' => "\\b"
case '\t' => "\\t"
case '\n' => "\\n"
Expand Down
20 changes: 1 addition & 19 deletions compiler/src/dotty/tools/dotc/quoted/Toolbox.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package dotty.tools.dotc.quoted

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.Constants._
import dotty.tools.dotc.core.quoted.PickledQuotes
import dotty.tools.dotc.printing.RefinedPrinter

import scala.quoted.Expr
import scala.runtime.BoxedUnit
import scala.quoted.Exprs.{LiftedExpr, TastyTreeExpr}
import scala.runtime.quoted._

Expand All @@ -32,20 +27,7 @@ object Toolbox {
new QuoteDriver().run(expr, runSettings)
}

def show(expr: Expr[T]): String = expr match {
case expr: LiftedExpr[T] =>
expr.value match {
case value: Class[_] => s"classOf[${value.getCanonicalName}]"
case value if value == BoxedUnit.UNIT => "()"
case value =>
implicit val ctx = new QuoteDriver().initCtx
if (showSettings.compilerArgs.contains("-color:never"))
ctx.settings.color.update("never")
val printer = new RefinedPrinter(ctx)
printer.toText(Literal(Constant(value))).mkString(Int.MaxValue, false)
}
case _ => new QuoteDriver().show(expr, showSettings)
}
def show(expr: Expr[T]): String = new QuoteDriver().show(expr, showSettings)

}

Expand Down
19 changes: 17 additions & 2 deletions library/src/scala/tasty/util/ShowSourceCode.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package scala.tasty
package util

import scala.annotation.switch

class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty0) {
import tasty._

Expand Down Expand Up @@ -591,8 +593,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
case Constant.Long(v) => this += v += "L"
case Constant.Float(v) => this += v
case Constant.Double(v) => this += v
case Constant.Char(v) => this += '\'' += v.toString += '\'' // TODO escape char
case Constant.String(v) => this += '"' += v.toString += '"' // TODO escape string
case Constant.Char(v) => this += '\'' += escapedChar(v) += '\''
case Constant.String(v) => this += '"' += escapedString(v) += '"'
}

def printTypeOrBoundsTree(tpt: TypeOrBoundsTree): Buffer = tpt match {
Expand Down Expand Up @@ -770,6 +772,19 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
def +=(x: Char): this.type = { sb.append(x); this }
def +=(x: String): this.type = { sb.append(x); this }

private def escapedChar(ch: Char): String = (ch: @switch) match {
case '\b' => "\\b"
case '\t' => "\\t"
case '\n' => "\\n"
case '\f' => "\\f"
case '\r' => "\\r"
case '"' => "\\\""
case '\'' => "\\\'"
case '\\' => "\\\\"
case _ => if (ch.isControl) "\\0" + Integer.toOctalString(ch) else String.valueOf(ch)
}

private def escapedString(str: String): String = str flatMap escapedChar
}


Expand Down