@@ -5,8 +5,8 @@ import java.util.concurrent.atomic.AtomicInteger
55
66import scala .reflect .internal .ModifierFlags
77import scala .reflect .internal .util .SourceFile
8- import scala .tools .nsc ._
9- import scala .tools .nsc .plugins .{Plugin , PluginComponent }
8+ import scala .tools .nsc .Global
9+ import scala .tools .nsc .plugins .{PluginComponent , Plugin }
1010import scala .tools .nsc .transform .{Transform , TypingTransformers }
1111
1212/** @author Stephen Samuel */
@@ -142,6 +142,16 @@ class ScoverageInstrumentationComponent(val global: Global)
142142
143143 def transformStatements (trees : List [Tree ]): List [Tree ] = trees.map(process)
144144
145+ def transformForCases (cases : List [CaseDef ]): List [CaseDef ] = {
146+ // we don't instrument the synthetic case _ => false clause
147+ cases.dropRight(1 ).map(c => {
148+ treeCopy.CaseDef (
149+ // in a for-loop we don't care about instrumenting the guards, as they are synthetically generated
150+ c, c.pat, process(c.guard), process(c.body)
151+ )
152+ }) ++ cases.takeRight(1 )
153+ }
154+
145155 def transformCases (cases : List [CaseDef ]): List [CaseDef ] = {
146156 cases.map(c => {
147157 treeCopy.CaseDef (
@@ -167,7 +177,7 @@ class ScoverageInstrumentationComponent(val global: Global)
167177 safeStart(tree),
168178 safeEnd(tree),
169179 safeLine(tree),
170- original.toString() ,
180+ original.toString,
171181 Option (original.symbol).fold(" <nosymbol>" )(_.fullNameString),
172182 tree.getClass.getSimpleName,
173183 branch
@@ -225,6 +235,8 @@ class ScoverageInstrumentationComponent(val global: Global)
225235 def traverseApplication (t : Tree ): Tree = {
226236 t match {
227237 case a : ApplyToImplicitArgs => treeCopy.Apply (a, traverseApplication(a.fun), transformStatements(a.args))
238+ case Apply (Select (_, TermName (" withFilter" )), List (fun@ Function (params, body)))
239+ if fun.symbol.isSynthetic && fun.toString.contains(" check$ifrefutable$1" ) => t
228240 case a : Apply => treeCopy.Apply (a, traverseApplication(a.fun), transformStatements(a.args))
229241 case a : TypeApply => treeCopy.TypeApply (a, traverseApplication(a.fun), transformStatements(a.args))
230242 case s : Select => treeCopy.Select (s, traverseApplication(s.qualifier), s.name)
@@ -323,11 +335,6 @@ class ScoverageInstrumentationComponent(val global: Global)
323335 // ignore macro definitions in 2.11
324336 case DefDef (mods, _, _, _, _, _) if mods.isMacro => tree
325337
326- case d : DefDef =>
327- println(d)
328- val e = d
329- d
330-
331338 // this will catch methods defined as macros, eg def test = macro testImpl
332339 // it will not catch macro implemenations
333340 case d : DefDef if d.symbol != null
@@ -428,16 +435,17 @@ class ScoverageInstrumentationComponent(val global: Global)
428435 // pattern match clauses will be instrumented per case
429436 case m@ Match (selector : Tree , cases : List [CaseDef ]) =>
430437 // we can be fairly sure this was generated as part of a for loop
431- if (selector.toString() .contains(" check$" )
438+ if (selector.toString.contains(" check$" )
432439 && selector.tpe.annotations.mkString == " unchecked"
433440 && m.cases.last.toString == " case _ => false" ) {
434- // todo check these assumptions for 2.11
435- treeCopy
436- .Match (tree, instrument(selector, selector), transformCases(cases.dropRight(1 )) ++ cases.takeRight(1 ))
441+ treeCopy.Match (tree, process(selector), transformForCases(cases))
437442 } else {
438- if (selector.symbol.isSynthetic)
443+ // if the selector was added by compiler, we don't want to instrument it....
444+ // that usually means some construct is being transformed into a match
445+ if (Option (selector.symbol).exists(_.isSynthetic))
439446 treeCopy.Match (tree, selector, transformCases(cases))
440447 else
448+ // .. but we will if it was a user match
441449 treeCopy.Match (tree, process(selector), transformCases(cases))
442450 }
443451
0 commit comments