Skip to content

Commit 4ed6a3f

Browse files
committed
Instrument method invocations under -Yinstrument-defs
1 parent f63ff90 commit 4ed6a3f

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class ScalaSettings extends Settings.SettingGroup {
181181
val YnoDecodeStacktraces: Setting[Boolean] = BooleanSetting("-Yno-decode-stacktraces", "Show raw StackOverflow stacktraces, instead of decoding them into triggering operations.")
182182

183183
val Yinstrument: Setting[Boolean] = BooleanSetting("-Yinstrument", "Add instrumentation code that counts allocations and closure creations.")
184+
val YinstrumentDefs: Setting[Boolean] = BooleanSetting("-Yinstrument-defs", "Add instrumentation code that counts method calls; needs -Yinstrument to be set, too.")
184185

185186
/** Dottydoc specific settings */
186187
val siteRoot: Setting[String] = StringSetting(

compiler/src/dotty/tools/dotc/transform/Instrumentation.scala

+15
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ class Instrumentation extends MiniPhase { thisPhase =>
7474
private def ok(using Context) =
7575
!ctx.owner.ownersIterator.exists(_.name.toString.startsWith("Stats"))
7676

77+
override def transformDefDef(tree: DefDef)(using Context): Tree =
78+
val sym = tree.symbol
79+
if ctx.settings.YinstrumentDefs.value
80+
&& ok
81+
&& sym.exists
82+
&& !sym.isOneOf(Synthetic | Artifact)
83+
then
84+
def icall = record(i"method/${sym.fullName}", tree)
85+
def rhs1 = tree.rhs match
86+
case rhs @ Block(stats, expr) => cpy.Block(rhs)(icall :: stats, expr)
87+
case _: Match | _: If | _: Try | _: Labeled => cpy.Block(tree.rhs)(icall :: Nil, tree.rhs)
88+
case rhs => rhs
89+
cpy.DefDef(tree)(rhs = rhs1)
90+
else tree
91+
7792
override def transformApply(tree: Apply)(using Context): Tree = tree.fun match {
7893
case Select(nu: New, _) =>
7994
cpy.Block(tree)(record(i"alloc/${nu.tpe}", tree) :: Nil, tree)

0 commit comments

Comments
 (0)