|  | 
|  | 1 | +package dotty.tools.dotc.profile | 
|  | 2 | + | 
|  | 3 | +import org.junit.Assert.* | 
|  | 4 | +import org.junit.* | 
|  | 5 | + | 
|  | 6 | +import scala.annotation.tailrec | 
|  | 7 | +import dotty.tools.DottyTest | 
|  | 8 | +import dotty.tools.dotc.util.SourceFile | 
|  | 9 | +import dotty.tools.dotc.core.Contexts.FreshContext | 
|  | 10 | +import java.nio.file.Files | 
|  | 11 | +import java.util.Locale | 
|  | 12 | + | 
|  | 13 | +class TraceNameManglingTest extends DottyTest { | 
|  | 14 | + | 
|  | 15 | +  override protected def initializeCtx(fc: FreshContext): Unit = { | 
|  | 16 | +    super.initializeCtx(fc) | 
|  | 17 | +    val tmpDir = Files.createTempDirectory("trace_name_mangling_test").nn | 
|  | 18 | +    fc.setSetting(fc.settings.YprofileEnabled, true) | 
|  | 19 | +    fc.setSetting( | 
|  | 20 | +      fc.settings.YprofileTrace, | 
|  | 21 | +      tmpDir.resolve("trace.json").nn.toAbsolutePath().toString() | 
|  | 22 | +    ) | 
|  | 23 | +    fc.setSetting( | 
|  | 24 | +      fc.settings.YprofileDestination, | 
|  | 25 | +      tmpDir.resolve("profiler.out").nn.toAbsolutePath().toString() | 
|  | 26 | +    ) | 
|  | 27 | +  } | 
|  | 28 | + | 
|  | 29 | +  @Test def escapeBackslashes(): Unit = { | 
|  | 30 | +    val isWindows = sys.props("os.name").toLowerCase(Locale.ROOT) == "windows" | 
|  | 31 | +    val filename = if isWindows then "/.scala" else "\\.scala" | 
|  | 32 | +    checkTraceEvents( | 
|  | 33 | +      """ | 
|  | 34 | +      |class /\ : | 
|  | 35 | +      |  var /\ = ??? | 
|  | 36 | +      |object /\{ | 
|  | 37 | +      |  def /\ = ??? | 
|  | 38 | +      |}""".stripMargin, | 
|  | 39 | +      filename = filename | 
|  | 40 | +    )( | 
|  | 41 | +      Set( | 
|  | 42 | +        raw"class /\\", | 
|  | 43 | +        raw"object /\\", | 
|  | 44 | +        raw"method /\\", | 
|  | 45 | +        raw"variable /\\", | 
|  | 46 | +        raw"setter /\\_=" | 
|  | 47 | +      ).map(TraceEvent("typecheck", _)) | 
|  | 48 | +        ++ Set( | 
|  | 49 | +          TraceEvent("file", if isWindows then "/.scala" else "\\\\.scala") | 
|  | 50 | +        ) | 
|  | 51 | +    ) | 
|  | 52 | +  } | 
|  | 53 | + | 
|  | 54 | +  @Test def escapeDoubleQuotes(): Unit = { | 
|  | 55 | +    val filename = "\"quoted\".scala" | 
|  | 56 | +    checkTraceEvents( | 
|  | 57 | +      """ | 
|  | 58 | +      |class `"QuotedClass"`: | 
|  | 59 | +      |  var `"quotedVar"` = ??? | 
|  | 60 | +      |object `"QuotedObject"` { | 
|  | 61 | +      |  def `"quotedMethod"` = ??? | 
|  | 62 | +      |}""".stripMargin, | 
|  | 63 | +      filename = filename | 
|  | 64 | +    ): | 
|  | 65 | +      Set( | 
|  | 66 | +        raw"class \"QuotedClass\"", | 
|  | 67 | +        raw"object \"QuotedObject\"", | 
|  | 68 | +        raw"method \"quotedMethod\"", | 
|  | 69 | +        raw"variable \"quotedVar\"" | 
|  | 70 | +      ).map(TraceEvent("typecheck", _)) | 
|  | 71 | +        ++ Set(TraceEvent("file", "\\\"quoted\\\".scala")) | 
|  | 72 | +  } | 
|  | 73 | +  @Test def escapeNonAscii(): Unit = { | 
|  | 74 | +    val filename = "unic😀de.scala" | 
|  | 75 | +    checkTraceEvents( | 
|  | 76 | +      """ | 
|  | 77 | +      |class ΩUnicodeClass: | 
|  | 78 | +      |  var `中文Var` = ??? | 
|  | 79 | +      |object ΩUnicodeObject { | 
|  | 80 | +      |  def 中文Method = ??? | 
|  | 81 | +      |}""".stripMargin, | 
|  | 82 | +      filename = filename | 
|  | 83 | +    ): | 
|  | 84 | +      Set( | 
|  | 85 | +        "class \\u03A9UnicodeClass", | 
|  | 86 | +        "object \\u03A9UnicodeObject", | 
|  | 87 | +        "method \\u4E2D\\u6587Method", | 
|  | 88 | +        "variable \\u4E2D\\u6587Var" | 
|  | 89 | +      ).map(TraceEvent("typecheck", _)) | 
|  | 90 | +        ++ Set(TraceEvent("file", "unic\\uD83D\\uDE00de.scala")) | 
|  | 91 | +  } | 
|  | 92 | + | 
|  | 93 | +  case class TraceEvent(category: String, name: String) | 
|  | 94 | +  private def compileWithTracer( | 
|  | 95 | +      code: String, | 
|  | 96 | +      filename: String, | 
|  | 97 | +      afterPhase: String = "typer" | 
|  | 98 | +  )(checkEvents: Seq[TraceEvent] => Unit) = { | 
|  | 99 | +    val runCtx = locally: | 
|  | 100 | +      val source = SourceFile.virtual(filename, code) | 
|  | 101 | +      val c = compilerWithChecker(afterPhase) { (_, _) => () } | 
|  | 102 | +      val run = c.newRun | 
|  | 103 | +      run.compileSources(List(source)) | 
|  | 104 | +      run.runContext | 
|  | 105 | +    assert(!runCtx.reporter.hasErrors, "compilation failed") | 
|  | 106 | +    val outfile = ctx.settings.YprofileTrace.value | 
|  | 107 | +    checkEvents: | 
|  | 108 | +      scala.io.Source | 
|  | 109 | +        .fromFile(outfile) | 
|  | 110 | +        .getLines() | 
|  | 111 | +        .collect: | 
|  | 112 | +          case s"""${_}"cat":"${category}","name":${name},"ph":${_}""" => | 
|  | 113 | +            TraceEvent(category, name.stripPrefix("\"").stripSuffix("\"")) | 
|  | 114 | +        .distinct.toSeq | 
|  | 115 | +  } | 
|  | 116 | + | 
|  | 117 | +  private def checkTraceEvents(code: String, filename: String = "test")(expected: Set[TraceEvent]): Unit = { | 
|  | 118 | +    compileWithTracer(code, filename = filename, afterPhase = "typer"){ events => | 
|  | 119 | +      val missing = expected.diff(events.toSet) | 
|  | 120 | +      def showFound = events | 
|  | 121 | +        .groupBy(_.category) | 
|  | 122 | +        .collect: | 
|  | 123 | +          case (category, events) | 
|  | 124 | +              if expected.exists(_.category == category) => | 
|  | 125 | +            s"- $category: [${events.map(_.name).mkString(", ")}]" | 
|  | 126 | +        .mkString("\n") | 
|  | 127 | +      assert( | 
|  | 128 | +        missing.isEmpty, | 
|  | 129 | +        s"""Missing ${missing.size} names [${missing.mkString(", ")}] in events, got:\n${showFound}""" | 
|  | 130 | +      ) | 
|  | 131 | +    } | 
|  | 132 | +  } | 
|  | 133 | +} | 
0 commit comments