Skip to content

Commit b072662

Browse files
committed
refactor semanticdb
1 parent 550c316 commit b072662

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import scala.jdk.CollectionConverters._
2525
import scala.PartialFunction.condOpt
2626
import typer.ImportInfo.withRootImports
2727

28+
import dotty.tools.dotc.reporting.Diagnostic.Warning
2829
import dotty.tools.dotc.{semanticdb => s}
2930
import dotty.tools.io.{AbstractFile, JarArchive}
3031
import dotty.tools.dotc.semanticdb.DiagnosticOps.*
@@ -61,56 +62,64 @@ class ExtractSemanticDB private (phaseMode: ExtractSemanticDB.PhaseMode) extends
6162
// Check not needed since it does not transform trees
6263
override def isCheckable: Boolean = false
6364

65+
private def computeDiagnostics(
66+
sourceRoot: String,
67+
warnings: Map[SourceFile, List[Warning]],
68+
append: ((Path, List[Diagnostic])) => Unit)(using Context): Boolean = monitor(phaseName) {
69+
val unit = ctx.compilationUnit
70+
warnings.get(unit.source).foreach { ws =>
71+
val outputDir =
72+
ExtractSemanticDB.semanticdbPath(
73+
unit.source,
74+
ExtractSemanticDB.semanticdbOutDir,
75+
sourceRoot
76+
)
77+
append((outputDir, ws.map(_.toSemanticDiagnostic)))
78+
}
79+
}
80+
81+
private def extractSemanticDB(sourceRoot: String, writeSemanticdbText: Boolean)(using Context): Boolean =
82+
monitor(phaseName) {
83+
val unit = ctx.compilationUnit
84+
val outputDir =
85+
ExtractSemanticDB.semanticdbPath(
86+
unit.source,
87+
ExtractSemanticDB.semanticdbOutDir,
88+
sourceRoot
89+
)
90+
val extractor = ExtractSemanticDB.Extractor()
91+
extractor.extract(unit.tpdTree)
92+
ExtractSemanticDB.write(
93+
unit.source,
94+
extractor.occurrences.toList,
95+
extractor.symbolInfos.toList,
96+
extractor.synthetics.toList,
97+
outputDir,
98+
sourceRoot,
99+
writeSemanticdbText
100+
)
101+
}
102+
64103
override def runOn(units: List[CompilationUnit])(using ctx: Context): List[CompilationUnit] = {
65104
val sourceRoot = ctx.settings.sourceroot.value
66105
val appendDiagnostics = phaseMode == ExtractSemanticDB.PhaseMode.AppendDiagnostics
106+
val unitContexts = units.map(ctx.fresh.setCompilationUnit(_).withRootImports)
67107
if (appendDiagnostics)
68108
val warnings = ctx.reporter.allWarnings.groupBy(w => w.pos.source)
69109
val buf = mutable.ListBuffer.empty[(Path, Seq[Diagnostic])]
70-
units.foreach { unit =>
71-
val unitCtx = ctx.fresh.setCompilationUnit(unit).withRootImports
72-
monitor(phaseName) {
73-
warnings.get(unit.source).foreach { ws =>
74-
val outputDir =
75-
ExtractSemanticDB.semanticdbPath(
76-
unit.source,
77-
ExtractSemanticDB.semanticdbOutDir(using unitCtx),
78-
sourceRoot
79-
)
80-
buf += ((outputDir, ws.map(_.toSemanticDiagnostic)))
81-
}
82-
}(using unitCtx)
83-
}
110+
val units0 =
111+
for unitCtx <- unitContexts if computeDiagnostics(sourceRoot, warnings, buf += _)(using unitCtx)
112+
yield unitCtx.compilationUnit
84113
cancellable {
85114
buf.toList.asJava.parallelStream().forEach { case (out, warnings) =>
86115
ExtractSemanticDB.appendDiagnostics(warnings, out)
87116
}
88117
}
118+
units0
89119
else
90120
val writeSemanticdbText = ctx.settings.semanticdbText.value
91-
units.foreach { unit =>
92-
val unitCtx = ctx.fresh.setCompilationUnit(unit).withRootImports
93-
monitor(phaseName) {
94-
val outputDir =
95-
ExtractSemanticDB.semanticdbPath(
96-
unit.source,
97-
ExtractSemanticDB.semanticdbOutDir(using unitCtx),
98-
sourceRoot
99-
)
100-
val extractor = ExtractSemanticDB.Extractor()
101-
extractor.extract(unit.tpdTree)(using unitCtx)
102-
ExtractSemanticDB.write(
103-
unit.source,
104-
extractor.occurrences.toList,
105-
extractor.symbolInfos.toList,
106-
extractor.synthetics.toList,
107-
outputDir,
108-
sourceRoot,
109-
writeSemanticdbText
110-
)
111-
}(using unitCtx)
112-
}
113-
units
121+
for unitCtx <- unitContexts if extractSemanticDB(sourceRoot, writeSemanticdbText)(using unitCtx)
122+
yield unitCtx.compilationUnit
114123
}
115124

116125
def run(using Context): Unit = unsupported("run")

0 commit comments

Comments
 (0)