From 5223adb474d755e1db50a79322e169388e44222c Mon Sep 17 00:00:00 2001 From: Kacper Korban Date: Sun, 25 Feb 2024 22:15:48 +0100 Subject: [PATCH] Pre compute coverage filter patterns --- .../dotc/transform/InstrumentCoverage.scala | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala b/compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala index aa3b48e53f35..a39a1c6f591e 100644 --- a/compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala +++ b/compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala @@ -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). @@ -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 @@ -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 ) @@ -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