Skip to content

Commit e226aa8

Browse files
committed
Tasty inspector is able to run using provided context
1 parent af92734 commit e226aa8

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import dotty.tools.dotc.fromtasty._
1313
import dotty.tools.dotc.util.ClasspathFromClassloader
1414
import dotty.tools.dotc.CompilationUnit
1515
import dotty.tools.unsupported
16+
import dotty.tools.dotc.report
1617

1718
import java.io.File.pathSeparator
1819

@@ -57,13 +58,39 @@ trait TastyInspector:
5758
val files = tastyFiles ::: jars
5859
files.nonEmpty && inspectFiles(dependenciesClasspath, files)
5960

60-
private def inspectFiles(classpath: List[String], classes: List[String]): Boolean =
61-
if (classes.isEmpty)
62-
throw new IllegalArgumentException("Parameter classes should no be empty")
61+
/** Load and process TASTy files using TASTy reflect and provided context
62+
*
63+
* Used in doctool to reuse reporter and setup provided by sbt
64+
*
65+
* @param classes List of paths of `.tasty` and `.jar` files (no validation is performed)
66+
* @param classpath Classpath with extra dependencies needed to load class in the `.tasty` files
67+
*/
68+
protected def inspectFilesInContext(classpath: List[String], classes: List[String])(using Context): Unit =
69+
if (classes.isEmpty) report.error("Parameter classes should no be empty")
70+
inspectorDriver().process(inspectorArgs(classpath, classes), summon[Context])
71+
6372

73+
private def inspectorDriver() =
6474
class InspectorDriver extends Driver:
6575
override protected def newCompiler(implicit ctx: Context): Compiler = new TastyFromClass
6676

77+
class TastyInspectorPhase extends Phase:
78+
override def phaseName: String = "tastyInspector"
79+
80+
override def run(implicit ctx: Context): Unit =
81+
val qctx = QuotesImpl()
82+
self.processCompilationUnit(using qctx)(ctx.compilationUnit.tpdTree.asInstanceOf[qctx.reflect.Tree])
83+
84+
class TastyInspectorFinishPhase extends Phase:
85+
override def phaseName: String = "tastyInspectorFinish"
86+
87+
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
88+
val qctx = QuotesImpl()
89+
self.postProcess(using qctx)
90+
units
91+
92+
override def run(implicit ctx: Context): Unit = unsupported("run")
93+
6794
class TastyFromClass extends TASTYCompiler:
6895

6996
override protected def frontendPhases: List[List[Phase]] =
@@ -83,35 +110,19 @@ trait TastyInspector:
83110
reset()
84111
new TASTYRun(this, ctx.fresh.addMode(Mode.ReadPositions).addMode(Mode.ReadComments))
85112

86-
end TastyFromClass
113+
new InspectorDriver
87114

88-
class TastyInspectorPhase extends Phase:
89-
90-
override def phaseName: String = "tastyInspector"
91-
92-
override def run(implicit ctx: Context): Unit =
93-
val qctx = QuotesImpl()
94-
self.processCompilationUnit(using qctx)(ctx.compilationUnit.tpdTree.asInstanceOf[qctx.reflect.Tree])
95-
96-
end TastyInspectorPhase
97-
98-
class TastyInspectorFinishPhase extends Phase:
99-
100-
override def phaseName: String = "tastyInspectorFinish"
101-
102-
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
103-
val qctx = QuotesImpl()
104-
self.postProcess(using qctx)
105-
units
115+
private def inspectorArgs(classpath: List[String], classes: List[String]): Array[String] =
116+
val currentClasspath = ClasspathFromClassloader(getClass.getClassLoader)
117+
val fullClasspath = (classpath :+ currentClasspath).mkString(pathSeparator)
118+
("-from-tasty" :: "-Yretain-trees" :: "-classpath" :: fullClasspath :: classes).toArray
106119

107-
override def run(implicit ctx: Context): Unit = unsupported("run")
108120

109-
end TastyInspectorFinishPhase
121+
private def inspectFiles(classpath: List[String], classes: List[String]): Boolean =
122+
if (classes.isEmpty)
123+
throw new IllegalArgumentException("Parameter classes should no be empty")
110124

111-
val currentClasspath = ClasspathFromClassloader(getClass.getClassLoader)
112-
val fullClasspath = (classpath :+ currentClasspath).mkString(pathSeparator)
113-
val args = "-from-tasty" :: "-Yretain-trees" :: "-classpath" :: fullClasspath :: classes
114-
val reporter = (new InspectorDriver).process(args.toArray)
125+
val reporter = inspectorDriver().process(inspectorArgs(classpath, classes))
115126
reporter.hasErrors
116127

117128
end inspectFiles

0 commit comments

Comments
 (0)