@@ -25,6 +25,7 @@ import scala.jdk.CollectionConverters._
25
25
import scala .PartialFunction .condOpt
26
26
import typer .ImportInfo .withRootImports
27
27
28
+ import dotty .tools .dotc .reporting .Diagnostic .Warning
28
29
import dotty .tools .dotc .{semanticdb => s }
29
30
import dotty .tools .io .{AbstractFile , JarArchive }
30
31
import dotty .tools .dotc .semanticdb .DiagnosticOps .*
@@ -61,56 +62,64 @@ class ExtractSemanticDB private (phaseMode: ExtractSemanticDB.PhaseMode) extends
61
62
// Check not needed since it does not transform trees
62
63
override def isCheckable : Boolean = false
63
64
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
+
64
103
override def runOn (units : List [CompilationUnit ])(using ctx : Context ): List [CompilationUnit ] = {
65
104
val sourceRoot = ctx.settings.sourceroot.value
66
105
val appendDiagnostics = phaseMode == ExtractSemanticDB .PhaseMode .AppendDiagnostics
106
+ val unitContexts = units.map(ctx.fresh.setCompilationUnit(_).withRootImports)
67
107
if (appendDiagnostics)
68
108
val warnings = ctx.reporter.allWarnings.groupBy(w => w.pos.source)
69
109
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
84
113
cancellable {
85
114
buf.toList.asJava.parallelStream().forEach { case (out, warnings) =>
86
115
ExtractSemanticDB .appendDiagnostics(warnings, out)
87
116
}
88
117
}
118
+ units0
89
119
else
90
120
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
114
123
}
115
124
116
125
def run (using Context ): Unit = unsupported(" run" )
0 commit comments