summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/Driver.scala
diff options
context:
space:
mode:
authorSom Snytt <som.snytt@gmail.com>2015-06-08 11:09:42 -0700
committerSom Snytt <som.snytt@gmail.com>2015-06-29 10:26:44 -0700
commit7a076931491ee2467e76f480386ea05e319ec3a2 (patch)
treed5335f61005653b5a02b86133161de208773d6b9 /src/compiler/scala/tools/nsc/Driver.scala
parentffcf07347925cabdfc205aec1e4ff42de840ba5e (diff)
downloadscala-7a076931491ee2467e76f480386ea05e319ec3a2.tar.gz
scala-7a076931491ee2467e76f480386ea05e319ec3a2.tar.bz2
scala-7a076931491ee2467e76f480386ea05e319ec3a2.zip
SI-9350 Command option -Xreporter
Add a setting to take a custom Reporter. Example of reporter that discounts deprecations for purposes of (not) failing the build: ``` import scala.tools.nsc.Settings import scala.tools.nsc.reporters.ConsoleReporter import scala.reflect.internal.util._ class MyReporter(ss: Settings) extends ConsoleReporter(ss) { var deprecationCount = 0 override def warning(pos: Position, msg: String): Unit = { if (msg contains "is deprecated") deprecationCount += 1 super.warning(pos, msg) } override def hasWarnings: Boolean = count(WARNING) - deprecationCount > 0 override def reset() = { deprecationCount = 0 ; super.reset() } } ``` Invoked as: ``` $ scalac -toolcp . -Xreporter myrep.MyReporter -Xfatal-warnings -deprecation test.scala test.scala:8: warning: class C in package tester is deprecated: Don't use me Console println s"${new C}" ^ one warning found ``` where the reporter class is in the current directory, placed on the tool class path. Also flush on early-reported errors.
Diffstat (limited to 'src/compiler/scala/tools/nsc/Driver.scala')
-rw-r--r--src/compiler/scala/tools/nsc/Driver.scala26
1 files changed, 14 insertions, 12 deletions
diff --git a/src/compiler/scala/tools/nsc/Driver.scala b/src/compiler/scala/tools/nsc/Driver.scala
index 6befa76b3f..b30744c4df 100644
--- a/src/compiler/scala/tools/nsc/Driver.scala
+++ b/src/compiler/scala/tools/nsc/Driver.scala
@@ -1,7 +1,7 @@
package scala
package tools.nsc
-import scala.tools.nsc.reporters.ConsoleReporter
+import scala.tools.nsc.reporters.{ ConsoleReporter, Reporter }
import Properties.{ versionMsg, residentPromptString }
import scala.reflect.internal.util.FakePos
@@ -9,39 +9,43 @@ abstract class Driver {
val prompt = residentPromptString
- var reporter: ConsoleReporter = _
+ var reporter: Reporter = _
protected var command: CompilerCommand = _
protected var settings: Settings = _
+ /** Forward errors to the (current) reporter. */
protected def scalacError(msg: String): Unit = {
reporter.error(FakePos("scalac"), msg + "\n scalac -help gives more information")
}
+ /** True to continue compilation. */
protected def processSettingsHook(): Boolean = {
- if (settings.version) { reporter echo versionMsg ; false } else true
+ if (settings.version) { reporter echo versionMsg ; false }
+ else !reporter.hasErrors
}
protected def newCompiler(): Global
- protected def doCompile(compiler: Global) {
+ protected def doCompile(compiler: Global): Unit = {
if (command.files.isEmpty) {
reporter.echo(command.usageMsg)
reporter.echo(compiler.pluginOptionsHelp)
} else {
val run = new compiler.Run()
run compile command.files
- reporter.printSummary()
+ reporter.finish()
}
}
- def process(args: Array[String]) {
+ def process(args: Array[String]): Boolean = {
val ss = new Settings(scalacError)
- reporter = new ConsoleReporter(ss)
+ reporter = new ConsoleReporter(ss) // for reporting early config errors, before compiler is constructed
command = new CompilerCommand(args.toList, ss)
settings = command.settings
if (processSettingsHook()) {
val compiler = newCompiler()
+ reporter = compiler.reporter // adopt the configured reporter
try {
if (reporter.hasErrors)
reporter.flush()
@@ -57,11 +61,9 @@ abstract class Driver {
case _ => throw ex // unexpected error, tell the outside world.
}
}
- }
+ } else if (reporter.hasErrors) reporter.flush()
+ !reporter.hasErrors
}
- def main(args: Array[String]) {
- process(args)
- sys.exit(if (reporter.hasErrors) 1 else 0)
- }
+ def main(args: Array[String]): Unit = sys.exit(if (process(args)) 0 else 1)
}