From f7b2aa54e7c77aa2c095ef364dddcc43cbaffd6a Mon Sep 17 00:00:00 2001 From: Jan Chyb Date: Fri, 10 Jan 2025 11:32:46 +0100 Subject: [PATCH] Initialise Coverage object only when the option is used --- compiler/src/dotty/tools/dotc/core/Contexts.scala | 7 +++---- .../tools/dotc/transform/InstrumentCoverage.scala | 12 ++++++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Contexts.scala b/compiler/src/dotty/tools/dotc/core/Contexts.scala index d6d726c8e92d..7c54d1392720 100644 --- a/compiler/src/dotty/tools/dotc/core/Contexts.scala +++ b/compiler/src/dotty/tools/dotc/core/Contexts.scala @@ -983,11 +983,10 @@ object Contexts { /** Was best effort file used during compilation? */ private[core] var usedBestEffortTasty = false - /** Stores all instrumented statements (for InstrumentCoverage). - * We need this information to be persisted across different runs, - * so it's stored here. + /** If coverage option is used, it stores all instrumented statements (for InstrumentCoverage). + * We need this information to be persisted across different runs, so it's stored here. */ - private[dotc] var coverage: Coverage = Coverage() + private[dotc] var coverage: Coverage | Null = null // Types state /** A table for hash consing unique types */ diff --git a/compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala b/compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala index 8a8cbf037dc0..0229284a1b5f 100644 --- a/compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala +++ b/compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala @@ -55,13 +55,17 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer: .foreach(_.nn.delete()) end if + // Initialise a coverage object if it does not exist yet + if ctx.base.coverage == null then + ctx.base.coverage = Coverage() + coverageExcludeClasslikePatterns = ctx.settings.coverageExcludeClasslikes.value.map(_.r.pattern) coverageExcludeFilePatterns = ctx.settings.coverageExcludeFiles.value.map(_.r.pattern) - ctx.base.coverage.removeStatementsFromFile(ctx.compilationUnit.source.file.absolute.jpath) + ctx.base.coverage.nn.removeStatementsFromFile(ctx.compilationUnit.source.file.absolute.jpath) super.run - Serializer.serialize(ctx.base.coverage, outputPath, ctx.settings.sourceroot.value) + Serializer.serialize(ctx.base.coverage.nn, outputPath, ctx.settings.sourceroot.value) private def isClassIncluded(sym: Symbol)(using Context): Boolean = val fqn = sym.fullName.toText(ctx.printerFn(ctx)).show @@ -105,7 +109,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer: * @return the statement's id */ private def recordStatement(tree: Tree, pos: SourcePosition, branch: Boolean)(using ctx: Context): Int = - val id = ctx.base.coverage.nextStatementId() + val id = ctx.base.coverage.nn.nextStatementId() val sourceFile = pos.source val statement = Statement( @@ -121,7 +125,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer: treeName = tree.getClass.getSimpleName.nn, branch ) - ctx.base.coverage.addStatement(statement) + ctx.base.coverage.nn.addStatement(statement) id /**