Skip to content

Commit 3b8efbc

Browse files
committed
reporting callback added;
progress messages passed via compiler callback
1 parent 6fc069f commit 3b8efbc

File tree

9 files changed

+145
-47
lines changed

9 files changed

+145
-47
lines changed

src/dotty/tools/dotc/CompilerCallback.scala

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/dotty/tools/dotc/Driver.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dotty.tools.dotc
22

33
import config.CompilerCommand
44
import core.Contexts.{Context, ContextBase}
5+
import dotty.tools.dotc.callbacks.{ReportingCallback, CompilerCallback}
56
import util.DotClass
67
import reporting._
78
import scala.util.control.NonFatal
@@ -36,7 +37,7 @@ abstract class Driver extends DotClass {
3637
val summary = CompilerCommand.distill(args)(rootCtx)
3738
// FIXME: We should reuse rootCtx instead of creating newCtx, but this
3839
// makes some tests fail with "denotation module _root_ invalid in run 2."
39-
val newCtx = initCtx.setCompilerCallback(rootCtx.compilerCallback)
40+
val newCtx = initCtx.setCompilerCallback(rootCtx.compilerCallback).setReportingCallback(rootCtx.reportingCallback)
4041
implicit val ctx: Context = newCtx.fresh.setSettings(summary.sstate)
4142
val fileNames = CompilerCommand.checkUsage(summary, sourcesRequired)
4243
(fileNames, ctx)
@@ -47,8 +48,8 @@ abstract class Driver extends DotClass {
4748
doCompile(newCompiler(), fileNames)(ctx)
4849
}
4950

50-
def process(args: Array[String], callback: CompilerCallback): Reporter = {
51-
process(args, initCtx.setCompilerCallback(callback))
51+
def process(args: Array[String], callback: CompilerCallback, reportingCallback: ReportingCallback): Reporter = {
52+
process(args, initCtx.setCompilerCallback(callback).setReportingCallback(reportingCallback))
5253
}
5354

5455
// We overload `process` instead of using a default argument so that we
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package dotty.tools.dotc.callbacks;
2+
3+
import java.io.File;
4+
5+
/** This interface contains methods that can be implemented to execute code during the
6+
* compilation process.
7+
*
8+
* NOTE: This trait is experimental and may be subject to arbitrary changes.
9+
*
10+
* Example usage:
11+
* {{{
12+
* val args: Array[String] = ...
13+
* val callback = new CompilerCallback {
14+
* override def onClassGenerated(source: File, generatedClass: File, className: String) =
15+
* println(s"onClassGenerated($source, $generatedClass, $className)")
16+
* override def onSourceCompiled(source: File) =
17+
* println(s"onSourceCompiled($source)")
18+
* }
19+
* dotty.tools.dotc.process(args, callback)
20+
* // Or, if you have a custom root context `rootCtx`:
21+
* dotty.tools.dotc.process(args, rootCtx.setCompilerCallback(callback))
22+
* }}}
23+
*/
24+
public interface CompilerCallback {
25+
26+
/** Called when a class has been generated.
27+
*
28+
* @param source The source file corresponding to this class.
29+
* Example: ./src/library/scala/collection/Seq.scala
30+
* @param generatedClass The generated classfile for this class.
31+
* Example: ./scala/collection/Seq$.class
32+
* @param className The name of this class.
33+
* Example: scala.collection.Seq$
34+
*/
35+
void onClassGenerated(File source, File generatedClass, String className);
36+
37+
/** Called when every class for this file has been generated.
38+
*
39+
* @param source The source file.
40+
* Example: ./src/library/scala/collection/Seq.scala
41+
*/
42+
void onSourceCompiled(File source);
43+
44+
/** Called when compilation phase starts on a given source
45+
*
46+
* @param phase Name of the current phase
47+
* @param sourcePath Path to the processed file
48+
*/
49+
void startUnit(String phase, String sourcePath);
50+
51+
/** Called to show progress of the compilation process
52+
*/
53+
void advance(int currentProgress, int totalProgress);
54+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dotty.tools.dotc.callbacks;
2+
3+
import java.io.File;
4+
5+
public interface Position
6+
{
7+
File sourceFile();
8+
9+
String sourcePath();
10+
11+
int line();
12+
13+
String lineContent();
14+
15+
int offset();
16+
17+
int column();
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package dotty.tools.dotc.callbacks;
2+
3+
/**
4+
* @author Nikolay.Tropin
5+
*/
6+
public interface ReportingCallback {
7+
8+
void info(String msg, Position pos);
9+
10+
void warning(String msg, Position pos);
11+
12+
void error(String msg, Position pos);
13+
14+
void debug(String msg);
15+
16+
void trace(Throwable exception);
17+
}

src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import NameOps._
1313
import Uniques._
1414
import SymDenotations._
1515
import Flags.ParamAccessor
16+
import dotty.tools.dotc.callbacks.{CompilerCallback, ReportingCallback}
1617
import util.Positions._
1718
import ast.Trees._
1819
import ast.untpd
@@ -77,6 +78,11 @@ object Contexts {
7778
base.compilerCallback = callback; this
7879
}
7980

81+
/** Set the reporting callback, shared by all contexts with the same `base` */
82+
def setReportingCallback(callback: ReportingCallback): this.type = {
83+
base.reportingCallback = callback; this
84+
}
85+
8086
/** The outer context */
8187
private[this] var _outer: Context = _
8288
protected def outer_=(outer: Context) = _outer = outer
@@ -540,6 +546,8 @@ object Contexts {
540546
/** The compiler callback implementation, or null if unset */
541547
var compilerCallback: CompilerCallback = _
542548

549+
var reportingCallback: ReportingCallback = _
550+
543551
// Symbols state
544552

545553
/** A counter for unique ids */

src/dotty/tools/dotc/core/Phases.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ object Phases {
267267

268268
def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] =
269269
units.map { unit =>
270+
if (ctx.compilerCallback != null)
271+
ctx.compilerCallback.startUnit(phaseName, unit.source.file.path)
272+
270273
val unitCtx = ctx.fresh.setPhase(this.start).setCompilationUnit(unit)
271274
run(unitCtx)
272275
unitCtx.compilationUnit
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dotty.tools.dotc.reporting
2+
3+
import java.io.File
4+
5+
import dotty.tools.dotc.callbacks.Position
6+
import dotty.tools.dotc.util.SourcePosition
7+
8+
/**
9+
* @author Nikolay.Tropin
10+
*/
11+
class CallbackPositionAdapter(sourcePosition: SourcePosition) extends Position {
12+
13+
override def sourceFile(): File = sourcePosition.source.file.file
14+
15+
override def sourcePath(): String = sourcePosition.source.file.path
16+
17+
override def line(): Int = sourcePosition.line
18+
19+
override def lineContent(): String = sourcePosition.lineContents
20+
21+
override def column(): Int = sourcePosition.column
22+
23+
override def offset(): Int = sourcePosition.point
24+
}

src/dotty/tools/dotc/reporting/Reporter.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ object Reporter {
7171
}
7272
}
7373

74-
import Reporter._
74+
import dotty.tools.dotc.reporting.Reporter._
7575

7676
trait Reporting { this: Context =>
7777

@@ -234,14 +234,16 @@ abstract class Reporter {
234234
}
235235

236236
def report(d: Diagnostic)(implicit ctx: Context): Unit =
237-
if (!isHidden(d) && doReport(d)(ctx.addMode(Mode.Printing)))
237+
if (!isHidden(d) && doReport(d)(ctx.addMode(Mode.Printing))) {
238+
doCallbackReport(d)
238239
d match {
239240
case d: ConditionalWarning if !d.enablingOption.value => unreportedWarnings(d.enablingOption.name) += 1
240241
case d: Warning => warningCount += 1
241242
case d: Error => errorCount += 1
242243
case d: Info => // nothing to do here
243244
// match error if d is something else
244245
}
246+
}
245247

246248
def incomplete(d: Diagnostic)(implicit ctx: Context): Unit =
247249
incompleteHandler(d)(ctx)
@@ -265,6 +267,19 @@ abstract class Reporter {
265267
case _ => n + " " + elements + "s"
266268
}
267269

270+
private def doCallbackReport(d: Diagnostic)(implicit ctx: Context): Unit = {
271+
val reportingCallback = ctx.reportingCallback
272+
if (reportingCallback != null) {
273+
val position = if (d.pos.exists) new CallbackPositionAdapter(d.pos) else null
274+
d match {
275+
case d: ConditionalWarning if d.enablingOption.value => reportingCallback.warning(d.msg, position)
276+
case d: Warning => reportingCallback.warning(d.msg, position)
277+
case d: Error => reportingCallback.error(d.msg, position)
278+
case d: Info => reportingCallback.info(d.msg, position)
279+
}
280+
}
281+
}
282+
268283
/** Should this diagnostic not be reported at all? */
269284
def isHidden(d: Diagnostic)(implicit ctx: Context): Boolean = ctx.mode.is(Mode.Printing)
270285

0 commit comments

Comments
 (0)