From e1ef4728be7c54d7875499d809c7544e93ef0037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Batko?= Date: Mon, 9 May 2016 16:55:08 +0200 Subject: [PATCH] Add extra phase order options --- .../src/main/scala/scoverage/plugin.scala | 36 ++++++++++++++++--- .../scala/scoverage/ScoverageCompiler.scala | 2 +- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/scalac-scoverage-plugin/src/main/scala/scoverage/plugin.scala b/scalac-scoverage-plugin/src/main/scala/scoverage/plugin.scala index 34641003..5bab490c 100644 --- a/scalac-scoverage-plugin/src/main/scala/scoverage/plugin.scala +++ b/scalac-scoverage-plugin/src/main/scala/scoverage/plugin.scala @@ -14,18 +14,21 @@ class ScoveragePlugin(val global: Global) extends Plugin { override val name: String = "scoverage" override val description: String = "scoverage code coverage compiler plugin" - val instrumentationComponent = new ScoverageInstrumentationComponent(global) + private val (extraAfterPhase, extraBeforePhase) = processPhaseOptions(pluginOptions) + val instrumentationComponent = new ScoverageInstrumentationComponent(global, extraAfterPhase, extraBeforePhase) override val components: List[PluginComponent] = List(instrumentationComponent) override def processOptions(opts: List[String], error: String => Unit) { val options = new ScoverageOptions - for ( opt <- opts ) { + for (opt <- opts) { if (opt.startsWith("excludedPackages:")) { options.excludedPackages = opt.substring("excludedPackages:".length).split(";").map(_.trim).filterNot(_.isEmpty) } else if (opt.startsWith("excludedFiles:")) { options.excludedFiles = opt.substring("excludedFiles:".length).split(";").map(_.trim).filterNot(_.isEmpty) } else if (opt.startsWith("dataDir:")) { options.dataDir = opt.substring("dataDir:".length) + } else if (opt.startsWith("extraAfterPhase:") || opt.startsWith("extraBeforePhase:")){ + // skip here, these flags are processed elsewhere } else { error("Unknown option: " + opt) } @@ -39,9 +42,32 @@ class ScoveragePlugin(val global: Global) extends Plugin { "-P:scoverage:dataDir: where the coverage files should be written\n", "-P:scoverage:excludedPackages:; semicolon separated list of regexs for packages to exclude", "-P:scoverage:excludedFiles:; semicolon separated list of regexs for paths to exclude", + "-P:scoverage:extraAfterPhase: phase after which scoverage phase runs (must be after typer phase)", + "-P:scoverage:extraBeforePhase: phase before which scoverage phase runs (must be before patmat phase)", " Any classes whose fully qualified name matches the regex will", " be excluded from coverage." ).mkString("\n")) + + // copied from scala 2.11 + private def pluginOptions: List[String] = { + // Process plugin options of form plugin:option + def namec = name + ":" + global.settings.pluginOptions.value filter (_ startsWith namec) map (_ stripPrefix namec) + } + + private def processPhaseOptions(opts: List[String]): (Option[String], Option[String]) = { + var afterPhase: Option[String] = None + var beforePhase: Option[String] = None + for (opt <- opts) { + if (opt.startsWith("extraAfterPhase:")) { + afterPhase = Some(opt.substring("extraAfterPhase:".length)) + } + if (opt.startsWith("extraBeforePhase:")) { + beforePhase = Some(opt.substring("extraBeforePhase:".length)) + } + } + (afterPhase, beforePhase) + } } class ScoverageOptions { @@ -50,7 +76,7 @@ class ScoverageOptions { var dataDir: String = IOUtils.getTempPath } -class ScoverageInstrumentationComponent(val global: Global) +class ScoverageInstrumentationComponent(val global: Global, extraAfterPhase: Option[String], extraBeforePhase: Option[String]) extends PluginComponent with TypingTransformers with Transform { @@ -61,8 +87,8 @@ class ScoverageInstrumentationComponent(val global: Global) val coverage = new Coverage override val phaseName: String = "scoverage-instrumentation" - override val runsAfter: List[String] = List("typer") - override val runsBefore = List[String]("patmat") + override val runsAfter: List[String] = List("typer") ::: extraAfterPhase.toList + override val runsBefore: List[String] = List("patmat") ::: extraBeforePhase.toList /** * Our options are not provided at construction time, but shortly after, diff --git a/scalac-scoverage-plugin/src/test/scala/scoverage/ScoverageCompiler.scala b/scalac-scoverage-plugin/src/test/scala/scoverage/ScoverageCompiler.scala index f9f2247c..75b44f0a 100644 --- a/scalac-scoverage-plugin/src/test/scala/scoverage/ScoverageCompiler.scala +++ b/scalac-scoverage-plugin/src/test/scala/scoverage/ScoverageCompiler.scala @@ -75,7 +75,7 @@ class ScoverageCompiler(settings: scala.tools.nsc.Settings, reporter: scala.tool .getAbsolutePath } - val instrumentationComponent = new ScoverageInstrumentationComponent(this) + val instrumentationComponent = new ScoverageInstrumentationComponent(this, None, None) instrumentationComponent.setOptions(new ScoverageOptions()) val testStore = new ScoverageTestStoreComponent(this) val validator = new PositionValidator(this)