Skip to content

Commit

Permalink
Pre compute coverage filter patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Feb 25, 2024
1 parent 807d995 commit 3990fae
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import util.Spans.Span
import localopt.StringInterpolatorOpt
import inlines.Inlines
import scala.util.matching.Regex
import java.util.regex.Pattern

/** Implements code coverage by inserting calls to scala.runtime.coverage.Invoker
* ("instruments" the source code).
Expand All @@ -43,6 +44,9 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
// stores all instrumented statements
private val coverage = Coverage()

private var coverageExcludeClasslikePatterns: List[Pattern] = Nil
private var coverageExcludeFilePatterns: List[Pattern] = Nil

override def run(using ctx: Context): Unit =
val outputPath = ctx.settings.coverageOutputDir.value

Expand All @@ -56,19 +60,21 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
.filter(_.nn.getName.nn.startsWith("scoverage"))
.foreach(_.nn.delete())
end if

coverageExcludeClasslikePatterns = ctx.settings.coverageExcludeClasslikes.value.map(_.r.pattern)
coverageExcludeFilePatterns = ctx.settings.coverageExcludeFiles.value.map(_.r.pattern)

super.run

Serializer.serialize(coverage, outputPath, ctx.settings.sourceroot.value)

private def isClassIncluded(sym: Symbol)(using Context): Boolean =
val excludedClassNamePatterns = ctx.settings.coverageExcludeClasslikes.value.map(_.r.pattern)
excludedClassNamePatterns.isEmpty || !excludedClassNamePatterns.exists(
coverageExcludeClasslikePatterns.isEmpty || !coverageExcludeClasslikePatterns.exists(
_.matcher(sym.fullName.toText(ctx.printerFn(ctx)).show).nn.matches
)

private def isFileIncluded(file: SourceFile)(using Context): Boolean =
val excludedFilePatterns = ctx.settings.coverageExcludeFiles.value.map(_.r.pattern)
excludedFilePatterns.isEmpty || !excludedFilePatterns.exists(
coverageExcludeFilePatterns.isEmpty || !coverageExcludeFilePatterns.exists(
_.matcher(file.path.replace(".scala", "")).nn.matches
)

Expand Down Expand Up @@ -283,14 +289,14 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
transformDefDef(tree)

case tree: PackageDef =>
if (isFileIncluded(tree.srcPos.sourcePos.source) && isClassIncluded(tree.symbol))
if isFileIncluded(tree.srcPos.sourcePos.source) && isClassIncluded(tree.symbol) then
// only transform the statements of the package
cpy.PackageDef(tree)(tree.pid, transform(tree.stats))
else
tree

case tree: TypeDef =>
if (isFileIncluded(tree.srcPos.sourcePos.source) && isClassIncluded(tree.symbol))
if isFileIncluded(tree.srcPos.sourcePos.source) && isClassIncluded(tree.symbol) then
super.transform(tree)
else
tree
Expand Down

0 comments on commit 3990fae

Please sign in to comment.