diff options
| author | Jim Lawson | 2018-02-16 17:10:30 -0800 |
|---|---|---|
| committer | GitHub | 2018-02-16 17:10:30 -0800 |
| commit | edcb81a34dbf8a04d0b011aa1ca07c6e19598f23 (patch) | |
| tree | aba2e3b8b921f9fdc861ed51687735f6d18d7bff /src/main/scala/firrtl/Driver.scala | |
| parent | 74a3b302df4422bec47e754cad1703b36ff75cd2 (diff) | |
Replacematcherror - catch exceptions and convert to internal error. (#424)
* Catch exceptions and convert to internal error.
We need to update the displayed message to incorporate a line number and text to be used for the issue.
* Cleanup exception handling/throwing.
Re-throw expected (or uncorrectable exceptions).
Provide Utils.getThrowable() to get the first (eldest) or last throwable in the chain.
Update tests to conform to FreeSpec protocol.
* Minor cleanup
Admit we've updated some deprecated ScalaTest methods.
Diffstat (limited to 'src/main/scala/firrtl/Driver.scala')
| -rw-r--r-- | src/main/scala/firrtl/Driver.scala | 65 |
1 files changed, 47 insertions, 18 deletions
diff --git a/src/main/scala/firrtl/Driver.scala b/src/main/scala/firrtl/Driver.scala index bc5102d2..99ae6d54 100644 --- a/src/main/scala/firrtl/Driver.scala +++ b/src/main/scala/firrtl/Driver.scala @@ -5,6 +5,7 @@ package firrtl import scala.collection._ import scala.io.Source import scala.sys.process.{BasicIO,stringSeqToProcess} +import scala.util.control.ControlThrowable import java.io.{File, FileNotFoundException} import net.jcazevedo.moultingyaml._ @@ -12,8 +13,9 @@ import logger.Logger import Parser.{IgnoreInfo, InfoMode} import annotations._ import firrtl.annotations.AnnotationYamlProtocol._ +import firrtl.passes.PassException import firrtl.transforms._ -import Utils.throwInternalError +import firrtl.Utils.throwInternalError /** @@ -52,12 +54,23 @@ object Driver { customTransforms: Seq[Transform] = Seq.empty, annotations: AnnotationMap = AnnotationMap(Seq.empty) ): String = { - val parsedInput = Parser.parse(Source.fromFile(input).getLines(), infoMode) val outputBuffer = new java.io.CharArrayWriter - compiler.compile( - CircuitState(parsedInput, ChirrtlForm, Some(annotations)), - outputBuffer, - customTransforms) + try { + val parsedInput = Parser.parse(Source.fromFile(input).getLines(), infoMode) + compiler.compile( + CircuitState(parsedInput, ChirrtlForm, Some(annotations)), + outputBuffer, + customTransforms) + } + + catch { + // Rethrow the exceptions which are expected or due to the runtime environment (out of memory, stack overflow) + case p: ControlThrowable => throw p + case p: PassException => throw p + case p: FIRRTLException => throw p + // Treat remaining exceptions as internal errors. + case e: Exception => throwInternalError(exception = Some(e)) + } val outputFile = new java.io.PrintWriter(output) val outputString = outputBuffer.toString @@ -191,19 +204,35 @@ object Driver { } } - val annos = loadAnnotations(optionsManager) + var maybeFinalState: Option[CircuitState] = None + + // Wrap compilation in a try/catch to present Scala MatchErrors in a more user-friendly format. + try { + val annos = loadAnnotations(optionsManager) - val parsedInput = Parser.parse(firrtlSource, firrtlConfig.infoMode) + val parsedInput = Parser.parse(firrtlSource, firrtlConfig.infoMode) - // Does this need to be before calling compiler? - optionsManager.makeTargetDir() + // Does this need to be before calling compiler? + optionsManager.makeTargetDir() + + maybeFinalState = Some(firrtlConfig.compiler.compile( + CircuitState(parsedInput, + ChirrtlForm, + Some(AnnotationMap(annos))), + firrtlConfig.customTransforms + )) + } + + catch { + // Rethrow the exceptions which are expected or due to the runtime environment (out of memory, stack overflow) + case p: ControlThrowable => throw p + case p: PassException => throw p + case p: FIRRTLException => throw p + // Treat remaining exceptions as internal errors. + case e: Exception => throwInternalError(exception = Some(e)) + } - val finalState = firrtlConfig.compiler.compile( - CircuitState(parsedInput, - ChirrtlForm, - Some(AnnotationMap(annos))), - firrtlConfig.customTransforms - ) + val finalState = maybeFinalState.get // Do emission // Note: Single emission target assumption is baked in here @@ -217,7 +246,7 @@ object Driver { emitted.value case OneFilePerModule(dirName) => val emittedModules = finalState.emittedComponents collect { case x: EmittedModule => x } - if (emittedModules.isEmpty) throwInternalError // There should be something + if (emittedModules.isEmpty) throwInternalError() // There should be something emittedModules.foreach { module => val filename = optionsManager.getBuildFileName(firrtlConfig.outputSuffix, s"$dirName/${module.name}") val outputFile = new java.io.PrintWriter(filename) @@ -261,7 +290,7 @@ object Driver { optionsManager.showUsageAsError() failure case result => - throw new Exception(s"Error: Unknown Firrtl Execution result $result") + throwInternalError(Some(s"Error: Unknown Firrtl Execution result $result")) } } else { |
