Skip to content

Commit

Permalink
Initialise Coverage object only when the option is used
Browse files Browse the repository at this point in the history
  • Loading branch information
jchyb committed Jan 10, 2025
1 parent d07040d commit f7b2aa5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 3 additions & 4 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
12 changes: 8 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/InstrumentCoverage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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

/**
Expand Down

0 comments on commit f7b2aa5

Please sign in to comment.