Skip to content

Make coverage instrumentation more robust #16235

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
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
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/coverage/Coverage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Coverage:

/** A statement that can be invoked, and thus counted as "covered" by code coverage tools. */
case class Statement(
source: String,
location: Location,
id: Int,
start: Int,
Expand Down
17 changes: 10 additions & 7 deletions compiler/src/dotty/tools/dotc/coverage/Location.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,35 @@ import ast.tpd._
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.Flags.*
import java.nio.file.Path
import dotty.tools.dotc.util.SourceFile

/** Information about the location of a coverable piece of code.
*
* @param packageName name of the enclosing package
* @param className name of the closest enclosing class
* @param fullClassName fully qualified name of the closest enclosing class
* @param classType "type" of the closest enclosing class: Class, Trait or Object
* @param method name of the closest enclosing method
* @param method name of the closest enclosing method
* @param sourcePath absolute path of the source file
*/
final case class Location(
packageName: String,
className: String,
fullClassName: String,
classType: String,
method: String,
methodName: String,
sourcePath: Path
)

object Location:
/** Extracts the location info of a Tree. */
def apply(tree: Tree)(using ctx: Context): Location =
def apply(tree: Tree, source: SourceFile)(using ctx: Context): Location =

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

val classType: String =
if enclosingClass.is(Trait) then "Trait"
Expand All @@ -42,6 +45,6 @@ object Location:
className,
s"$packageName.$className",
classType,
ctx.owner.denot.enclosingMethod.name.toSimpleName.toString(),
ctx.source.file.absolute.jpath
methodName,
source.file.absolute.jpath
)
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.method}
|${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()
Loading