From 3100c58b63a16c8fe0e51419093cc3544422e280 Mon Sep 17 00:00:00 2001 From: Aleksander Boruch-Gruszecki Date: Fri, 13 Nov 2020 14:29:15 +0100 Subject: [PATCH 1/3] Add a "terminal" callback to TastyInspector This interface allows performing arbitrary operations on all compilation units. --- .../tasty/inspector/TastyInspector.scala | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala b/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala index d5128331a2c3..33755b1b9cdc 100644 --- a/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala +++ b/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala @@ -20,6 +20,9 @@ trait TastyInspector: /** Process a TASTy file using TASTy reflect */ protected def processCompilationUnit(using QuoteContext)(root: qctx.reflect.Tree): Unit + /** Called after all compilation units are processed */ + protected def finishProcessingCompilationUnits(using QuoteContext): Unit = () + /** Load and process TASTy files using TASTy reflect * * @param tastyFiles List of paths of `.tasty` files @@ -70,7 +73,8 @@ trait TastyInspector: override protected def transformPhases: List[List[Phase]] = Nil override protected def backendPhases: List[List[Phase]] = - List(new TastyInspectorPhase) :: // Print all loaded classes + List(new TastyInspectorPhase) :: // Perform a callback for each compilation unit + List(new TastyInspectorFinishPhase) :: // Perform a final callback Nil override def newRun(implicit ctx: Context): Run = @@ -89,6 +93,19 @@ trait TastyInspector: end TastyInspectorPhase + class TastyInspectorFinishPhase extends Phase: + + override def phaseName: String = "tastyInspectorFinish" + + override def run(implicit ctx: Context): Unit = + if !alreadyRan then + val qctx = QuoteContextImpl() + self.finishProcessingCompilationUnits(using qctx) + alreadyRan = true + + var alreadyRan = false + end TastyInspectorFinishPhase + val currentClasspath = ClasspathFromClassloader(getClass.getClassLoader) val fullClasspath = (classpath :+ currentClasspath).mkString(pathSeparator) val args = "-from-tasty" :: "-Yretain-trees" :: "-classpath" :: fullClasspath :: classes From d5414d690a68cde22715eaa014301443af59438f Mon Sep 17 00:00:00 2001 From: Aleksander Boruch-Gruszecki Date: Mon, 16 Nov 2020 15:32:03 +0100 Subject: [PATCH 2/3] Run final Inspector callback only once when exns happen --- .../src/scala/tasty/inspector/TastyInspector.scala | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala b/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala index 33755b1b9cdc..35fbcca4b240 100644 --- a/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala +++ b/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala @@ -99,9 +99,11 @@ trait TastyInspector: override def run(implicit ctx: Context): Unit = if !alreadyRan then - val qctx = QuoteContextImpl() - self.finishProcessingCompilationUnits(using qctx) - alreadyRan = true + try + val qctx = QuoteContextImpl() + self.finishProcessingCompilationUnits(using qctx) + finally + alreadyRan = true var alreadyRan = false end TastyInspectorFinishPhase From b8c33c7eacb0952b58ca6c346063239e629f7c5f Mon Sep 17 00:00:00 2001 From: Aleksander Boruch-Gruszecki Date: Tue, 17 Nov 2020 14:58:57 +0100 Subject: [PATCH 3/3] Adjust after feedback --- .../tasty/inspector/TastyInspector.scala | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala b/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala index 35fbcca4b240..b7aa7726b89f 100644 --- a/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala +++ b/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala @@ -11,6 +11,8 @@ import dotty.tools.dotc.core.Mode import dotty.tools.dotc.core.Phases.Phase import dotty.tools.dotc.fromtasty._ import dotty.tools.dotc.util.ClasspathFromClassloader +import dotty.tools.dotc.CompilationUnit +import dotty.tools.unsupported import java.io.File.pathSeparator @@ -21,7 +23,7 @@ trait TastyInspector: protected def processCompilationUnit(using QuoteContext)(root: qctx.reflect.Tree): Unit /** Called after all compilation units are processed */ - protected def finishProcessingCompilationUnits(using QuoteContext): Unit = () + protected def postProcess(using QuoteContext): Unit = () /** Load and process TASTy files using TASTy reflect * @@ -97,15 +99,13 @@ trait TastyInspector: override def phaseName: String = "tastyInspectorFinish" - override def run(implicit ctx: Context): Unit = - if !alreadyRan then - try - val qctx = QuoteContextImpl() - self.finishProcessingCompilationUnits(using qctx) - finally - alreadyRan = true - - var alreadyRan = false + override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] = + val qctx = QuoteContextImpl() + self.postProcess(using qctx) + units + + override def run(implicit ctx: Context): Unit = unsupported("run") + end TastyInspectorFinishPhase val currentClasspath = ClasspathFromClassloader(getClass.getClassLoader)