Skip to content

Commit 31ba352

Browse files
committed
#23 Fixes for macros (ignore macros)
1 parent 37a7a34 commit 31ba352

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/main/scala/scoverage/plugin.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,16 @@ class ScoverageInstrumentationComponent(val global: Global)
349349
c
350350
}
351351

352-
// skip macros
352+
// this will catch methods defined as macros, eg def test = macro testImpl
353+
// it will not catch macro implemenations
353354
case d: DefDef if d.symbol != null && d.symbol.annotations.size > 0
354355
&& d.symbol.annotations.head.atp.typeSymbol.nameString == "macroImpl" =>
355-
tree.duplicate
356+
tree
357+
358+
// will catch macro implemenations, as they must end with Expr, however will catch
359+
// any method that ends in Expr. // todo add way of allowing methods that return Expr
360+
case d: DefDef if d.symbol != null && d.tpt.symbol.fullNameString == "scala.reflect.api.Exprs.Expr" =>
361+
tree
356362

357363
// todo do we really want to ignore?
358364
case d: DefDef if d.symbol.isPrimaryConstructor => tree

src/test/scala/scoverage/PluginCoverageTest.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class PluginCoverageTest
5151
| def print1(list: List[String]) = for (string: String <- list) println(string)
5252
|} """.stripMargin)
5353
assert(!reporter.hasErrors)
54+
assert(!reporter.hasWarnings)
5455
// should have one statement for the withFilter invoke, one of the match selector,
5556
// one of the case block, one for the case string RHS value, one for the foreach block.
5657
assertNMeasuredStatements(5)
@@ -65,8 +66,26 @@ class PluginCoverageTest
6566
| }
6667
|} """.stripMargin)
6768
assert(!reporter.hasErrors)
69+
assert(!reporter.hasWarnings)
6870
// should have one statement for each literal, one for each case block, and one for the selector.
6971
assertNMeasuredStatements(7)
7072
}
7173

74+
test("scoverage component should not instrument any macro code") {
75+
compileCodeSnippet( """
76+
| object MyMacro {
77+
| import scala.language.experimental.macros
78+
| import scala.reflect.macros.Context
79+
| def test = macro testImpl
80+
| def testImpl(c: Context): c.Expr[Unit] = {
81+
| import c.universe._
82+
| reify {
83+
| println("macro test")
84+
| }
85+
| }
86+
|} """.stripMargin)
87+
assert(!reporter.hasErrors)
88+
assert(!reporter.hasWarnings)
89+
assertNMeasuredStatements(0)
90+
}
7291
}

src/test/scala/scoverage/PluginRunner.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ trait PluginSupport {
7575
assert(compiler.testStore.sources.mkString(" ").contains(s"scoverage.Invoker.invoked($k,"),
7676
s"Should be $n invoked statements but missing $k")
7777
}
78+
assert(!compiler.testStore.sources.mkString(" ").contains(s"scoverage.Invoker.invoked(${n + 1},"),
79+
s"Found statement ${n + 1} but only expected $n")
7880
}
7981
}
8082

0 commit comments

Comments
 (0)