@@ -2,23 +2,21 @@ package dotty.tools.dotc
2
2
package transform
3
3
4
4
import java .io .File
5
- import java .util .concurrent .atomic .AtomicInteger
6
5
7
6
import ast .tpd .*
8
7
import collection .mutable
9
8
import core .Flags .*
10
9
import core .Contexts .{Context , ctx , inContext }
11
10
import core .DenotTransformers .IdentityDenotTransformer
12
11
import core .Symbols .{defn , Symbol }
13
- import core .Decorators .{toTermName , i }
14
12
import core .Constants .Constant
15
13
import core .NameOps .isContextFunction
16
14
import core .Types .*
15
+ import coverage .*
17
16
import typer .LiftCoverage
18
- import util .{ SourcePosition , Property }
17
+ import util .SourcePosition
19
18
import util .Spans .Span
20
- import coverage .*
21
- import localopt .StringInterpolatorOpt .isCompilerIntrinsic
19
+ import localopt .StringInterpolatorOpt
22
20
23
21
/** Implements code coverage by inserting calls to scala.runtime.coverage.Invoker
24
22
* ("instruments" the source code).
@@ -44,7 +42,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
44
42
val outputPath = ctx.settings.coverageOutputDir.value
45
43
46
44
// Ensure the dir exists
47
- val dataDir = new File (outputPath)
45
+ val dataDir = File (outputPath)
48
46
val newlyCreated = dataDir.mkdirs()
49
47
50
48
if ! newlyCreated then
@@ -66,7 +64,16 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
66
64
tree match
67
65
// simple cases
68
66
case tree : (Import | Export | Literal | This | Super | New ) => tree
69
- case tree if tree.isEmpty || tree.isType => tree // empty Thicket, Ident, TypTree, ...
67
+ case tree if tree.isEmpty || tree.isType => tree // empty Thicket, Ident (referring to a type), TypeTree, ...
68
+
69
+ // identifier
70
+ case tree : Ident =>
71
+ val sym = tree.symbol
72
+ if canInstrumentParameterless(sym) then
73
+ // call to a local parameterless method f
74
+ instrument(tree)
75
+ else
76
+ tree
70
77
71
78
// branches
72
79
case tree : If =>
@@ -82,20 +89,6 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
82
89
finalizer = instrument(transform(tree.finalizer), branch = true )
83
90
)
84
91
85
- // a.f(args)
86
- case tree @ Apply (fun : Select , args) =>
87
- // don't transform the first Select, but do transform `a.b` in `a.b.f(args)`
88
- val transformedFun = cpy.Select (fun)(transform(fun.qualifier), fun.name)
89
- if canInstrumentApply(tree) then
90
- if needsLift(tree) then
91
- val transformed = cpy.Apply (tree)(transformedFun, args) // args will be transformed in instrumentLifted
92
- instrumentLifted(transformed)
93
- else
94
- val transformed = transformApply(tree, transformedFun)
95
- instrument(transformed)
96
- else
97
- transformApply(tree, transformedFun)
98
-
99
92
// f(args)
100
93
case tree : Apply =>
101
94
if canInstrumentApply(tree) then
@@ -106,24 +99,19 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
106
99
else
107
100
transformApply(tree)
108
101
109
- // (f(x) )[args]
110
- case TypeApply (fun : Apply , args) =>
102
+ // (fun )[args]
103
+ case TypeApply (fun, args) =>
111
104
cpy.TypeApply (tree)(transform(fun), args)
112
105
113
106
// a.b
114
107
case Select (qual, name) =>
115
- if qual.symbol.exists && qual.symbol.is( JavaDefined ) then
116
- // Java class can't be used as a value, we can't instrument the
117
- // qualifier ({<Probe>;System}.xyz() is not possible !) instrument it
118
- // as it is
119
- instrument(tree )
108
+ val transformed = cpy. Select (tree)(transform(qual), name)
109
+ val sym = tree.symbol
110
+ if canInstrumentParameterless(sym) then
111
+ // call to a parameterless method
112
+ instrument(transformed )
120
113
else
121
- val transformed = cpy.Select (tree)(transform(qual), name)
122
- if transformed.qualifier.isDef then
123
- // instrument calls to methods without parameter list
124
- instrument(transformed)
125
- else
126
- transformed
114
+ transformed
127
115
128
116
case tree : CaseDef => instrumentCaseDef(tree)
129
117
case tree : ValDef =>
@@ -142,7 +130,9 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
142
130
val rhs = transform(tree.rhs)
143
131
val finalRhs =
144
132
if canInstrumentDefDef(tree) then
145
- // Ensure that the rhs is always instrumented, if possible
133
+ // Ensure that the rhs is always instrumented, if possible.
134
+ // This is useful because methods can be stored and called later, or called by reflection,
135
+ // and if the rhs is too simple to be instrumented (like `def f = this`), the method won't show up as covered.
146
136
instrumentBody(tree, rhs)
147
137
else
148
138
rhs
@@ -162,7 +152,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
162
152
}
163
153
164
154
/** Lifts and instruments an application.
165
- * Note that if only one arg needs to be lifted, we just lift everything.
155
+ * Note that if only one arg needs to be lifted, we just lift everything (see LiftCoverage) .
166
156
*/
167
157
private def instrumentLifted (tree : Apply )(using Context ) =
168
158
// lifting
@@ -178,10 +168,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
178
168
)
179
169
180
170
private inline def transformApply (tree : Apply )(using Context ): Apply =
181
- transformApply(tree, transform(tree.fun))
182
-
183
- private inline def transformApply (tree : Apply , transformedFun : Tree )(using Context ): Apply =
184
- cpy.Apply (tree)(transformedFun, transform(tree.args))
171
+ cpy.Apply (tree)(transform(tree.fun), transform(tree.args))
185
172
186
173
private inline def instrumentCases (cases : List [CaseDef ])(using Context ): List [CaseDef ] =
187
174
cases.map(instrumentCaseDef)
@@ -201,7 +188,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
201
188
private def recordStatement (tree : Tree , pos : SourcePosition , branch : Boolean )(using ctx : Context ): Int =
202
189
val id = statementId
203
190
statementId += 1
204
- val statement = new Statement (
191
+ val statement = Statement (
205
192
source = ctx.source.file.name,
206
193
location = Location (tree),
207
194
id = id,
@@ -292,7 +279,7 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
292
279
* they shouldn't be lifted.
293
280
*/
294
281
val sym = fun.symbol
295
- sym.exists && (isShortCircuitedOp(sym) || isCompilerIntrinsic(sym))
282
+ sym.exists && (isShortCircuitedOp(sym) || StringInterpolatorOpt . isCompilerIntrinsic(sym))
296
283
end
297
284
298
285
val fun = tree.fun
@@ -312,7 +299,9 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
312
299
313
300
/** Check if an Apply can be instrumented. Prevents this phase from generating incorrect code. */
314
301
private def canInstrumentApply (tree : Apply )(using Context ): Boolean =
315
- ! tree.symbol.isOneOf(Synthetic | Artifact ) && // no need to instrument synthetic apply
302
+ val sym = tree.symbol
303
+ ! sym.isOneOf(Synthetic | Artifact ) && // no need to instrument synthetic apply
304
+ ! isCompilerIntrinsicMethod(sym) &&
316
305
(tree.typeOpt match
317
306
case AppliedType (tycon : NamedType , _) =>
318
307
/* If the last expression in a block is a context function, we'll try to
@@ -339,6 +328,28 @@ class InstrumentCoverage extends MacroTransform with IdentityDenotTransformer:
339
328
true
340
329
)
341
330
331
+ /** Is this the symbol of a parameterless method that we can instrument?
332
+ * Note: it is crucial that `asInstanceOf` and `isInstanceOf`, among others,
333
+ * do NOT get instrumented, because that would generate invalid code and crash
334
+ * in post-erasure checking.
335
+ */
336
+ private def canInstrumentParameterless (sym : Symbol )(using Context ): Boolean =
337
+ sym.is(Method , butNot = Synthetic | Artifact ) &&
338
+ sym.info.isParameterless &&
339
+ ! isCompilerIntrinsicMethod(sym)
340
+
341
+ /** Does sym refer to a "compiler intrinsic" method, which only exist during compilation,
342
+ * like Any.isInstanceOf?
343
+ * If this returns true, the call souldn't be instrumented.
344
+ */
345
+ private def isCompilerIntrinsicMethod (sym : Symbol )(using Context ): Boolean =
346
+ val owner = sym.maybeOwner
347
+ owner.exists && (
348
+ owner.eq(defn.AnyClass ) ||
349
+ owner.isPrimitiveValueClass ||
350
+ owner.maybeOwner == defn.CompiletimePackageClass
351
+ )
352
+
342
353
object InstrumentCoverage :
343
354
val name : String = " instrumentCoverage"
344
355
val description : String = " instrument code for coverage checking"
0 commit comments