Skip to content

Commit

Permalink
Coverage: escape strings before serializing statements, fix scala#15384
Browse files Browse the repository at this point in the history
  • Loading branch information
TheElectronWill committed Oct 23, 2022
1 parent 5414292 commit 3bd7293
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 38 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/coverage/Location.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object Location:

val ownerDenot = ctx.owner.denot
val enclosingClass = ownerDenot.enclosingClass
val packageName = ownerDenot.enclosingPackageClass.name.toSimpleName.toString
val packageName = ownerDenot.enclosingPackageClass.fullName.toSimpleName.toString
val className = enclosingClass.name.toSimpleName.toString
val methodName = ownerDenot.enclosingMethod.name.toSimpleName.toString

Expand Down
39 changes: 32 additions & 7 deletions compiler/src/dotty/tools/dotc/coverage/Serializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package coverage
import java.nio.file.{Path, Paths, Files}
import java.io.Writer
import scala.language.unsafeNulls
import scala.collection.mutable.StringBuilder

/**
* Serializes scoverage data.
Expand Down Expand Up @@ -62,25 +63,49 @@ object Serializer:
def writeStatement(stmt: Statement, writer: Writer): Unit =
// Note: we write 0 for the count because we have not measured the actual coverage at this point
writer.write(s"""${stmt.id}
|${getRelativePath(stmt.location.sourcePath)}
|${stmt.location.packageName}
|${stmt.location.className}
|${getRelativePath(stmt.location.sourcePath).escaped}
|${stmt.location.packageName.escaped}
|${stmt.location.className.escaped}
|${stmt.location.classType}
|${stmt.location.fullClassName}
|${stmt.location.methodName}
|${stmt.location.fullClassName.escaped}
|${stmt.location.methodName.escaped}
|${stmt.start}
|${stmt.end}
|${stmt.line}
|${stmt.symbolName}
|${stmt.symbolName.escaped}
|${stmt.treeName}
|${stmt.branch}
|0
|${stmt.ignored}
|${stmt.desc}
|${stmt.desc.escaped}
|\f
|""".stripMargin)

writeHeader(writer)
coverage.statements.toSeq
.sortBy(_.id)
.foreach(stmt => writeStatement(stmt, writer))

/** Makes a String suitable for output in the coverage statement data as a single line.
* Escaped characters: '\\' (backslash), '\n', '\r', '\f'
*/
extension (str: String) def escaped: String =
val builder = StringBuilder(str.length)
var i = 0
while
i < str.length
do
str.charAt(i) match
case '\\' =>
builder ++= "\\\\"
case '\n' =>
builder ++= "\\n"
case '\r' =>
builder ++= "\\r"
case '\f' =>
builder ++= "\\f"
case c =>
builder += c
i += 1
end while
builder.result()
11 changes: 5 additions & 6 deletions tests/coverage/pos/Enum.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Apply
false
0
false
println("Example 1: \n"+emptyList)
println("Example 1: \\n"+emptyList)

4
Enum.scala
Expand All @@ -101,7 +101,7 @@ Apply
false
0
false
"Example 1: \n"+emptyList
"Example 1: \\n"+emptyList

5
Enum.scala
Expand All @@ -118,7 +118,7 @@ Apply
false
0
false
println(s"${list}\n")
println(s"${list}\\n")

6
Enum.scala
Expand All @@ -135,7 +135,7 @@ Apply
false
0
false
s"${list}\n"
s"${list}\\n"

7
Enum.scala
Expand Down Expand Up @@ -169,8 +169,7 @@ Apply
false
0
false
for p <- Planet.values do
println(s"Your weight on $p is ${p.surfaceWeight(mass)}")
for p <- Planet.values do\n println(s"Your weight on $p is ${p.surfaceWeight(mass)}")

9
Enum.scala
Expand Down
4 changes: 4 additions & 0 deletions tests/coverage/pos/Escaping.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package covtest.`\n`

class `\r\n\f`:
def `\r\n\f`(`\\`: String) = `\\`.length
54 changes: 54 additions & 0 deletions tests/coverage/pos/Escaping.scoverage.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Coverage data, format version: 3.0
# Statement data:
# - id
# - source path
# - package name
# - class name
# - class type (Class, Object or Trait)
# - full class name
# - method name
# - start offset
# - end offset
# - line number
# - symbol name
# - tree name
# - is branch
# - invocations count
# - is ignored
# - description (can be multi-line)
# ' ' sign
# ------------------------------------------
0
Escaping.scala
covtest.\n
\r\n\f
Class
covtest.\n.\r\n\f
\r\n\f
69
80
3
length
Apply
false
0
false
`\\\\`.length

1
Escaping.scala
covtest.\n
\r\n\f
Class
covtest.\n.\r\n\f
\r\n\f
40
48
3
\r\n\f
DefDef
false
0
false
def `\\r\\

6 changes: 2 additions & 4 deletions tests/coverage/pos/For.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ Apply
false
0
false
for i <- 1 to 10 do
println(i)
for i <- 1 to 10 do\n println(i)

1
For.scala
Expand Down Expand Up @@ -136,8 +135,7 @@ Apply
false
0
false
for j <- 1 to 10 if f(j) do
println(j)
for j <- 1 to 10 if f(j) do\n println(j)

7
For.scala
Expand Down
3 changes: 1 addition & 2 deletions tests/coverage/run/erased/test.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,5 @@ DefDef
false
0
false
@main
def Test
@main\ndef Test

3 changes: 1 addition & 2 deletions tests/coverage/run/inheritance/test.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,5 @@ DefDef
false
0
false
@main
def Test
@main\ndef Test

3 changes: 1 addition & 2 deletions tests/coverage/run/inline-def/test.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,5 @@ DefDef
false
0
false
@main
def Test
@main\ndef Test

4 changes: 2 additions & 2 deletions tests/coverage/run/interpolation/test.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ Apply
false
0
false
println(raw"a\nb")
println(raw"a\\nb")

16
interpolation/test.scala
Expand All @@ -305,7 +305,7 @@ Apply
false
0
false
raw"a\nb"
raw"a\\nb"

17
interpolation/test.scala
Expand Down
3 changes: 1 addition & 2 deletions tests/coverage/run/java-methods/test.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,5 @@ DefDef
false
0
false
@main
def Test
@main\ndef Test

3 changes: 1 addition & 2 deletions tests/coverage/run/lifting-bool/test.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,5 @@ DefDef
false
0
false
@main
def Test
@main\ndef Test

3 changes: 1 addition & 2 deletions tests/coverage/run/lifting/test.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,5 @@ DefDef
false
0
false
@main
def Test
@main\ndef Test

3 changes: 1 addition & 2 deletions tests/coverage/run/parameterless/test.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,5 @@ DefDef
false
0
false
@main
def Test
@main\ndef Test

3 changes: 1 addition & 2 deletions tests/coverage/run/trait/test.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,5 @@ DefDef
false
0
false
@main
def Test
@main\ndef Test

3 changes: 1 addition & 2 deletions tests/coverage/run/varargs/test_1.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,5 @@ DefDef
false
0
false
@main
def Test
@main\ndef Test

0 comments on commit 3bd7293

Please sign in to comment.