Skip to content

Commit f81f730

Browse files
committed
Better compiler entry points
- Document the entry points - It is now possible to set a custom reporter without using a custom context - Use `null` for optional arguments to make it easier to run the compiler using reflection or from Java. - DPDirectCompiler does not use a custom context anymore
1 parent 4a06182 commit f81f730

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

src/dotty/tools/dotc/Driver.scala

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,57 @@ abstract class Driver extends DotClass {
4040
(fileNames, ctx)
4141
}
4242

43-
def process(args: Array[String], rootCtx: Context): Reporter = {
44-
val (fileNames, ctx) = setup(args, rootCtx)
45-
doCompile(newCompiler(), fileNames)(ctx)
46-
}
4743

48-
def process(args: Array[String], callback: CompilerCallback): Reporter = {
49-
process(args, initCtx.setCompilerCallback(callback))
44+
/** Principal entry point to the compiler.
45+
* Creates a new compiler instance and run it with arguments `args`.
46+
*
47+
* The optional arguments of this method all have `null` as their default
48+
* value, this makes it easier to call this method by reflection or from Java.
49+
*
50+
* @param args Arguments to pass to the compiler.
51+
* @param reporter Used to log errors, warnings, and info messages.
52+
* The default reporter is used if this is `null`.
53+
* @param callback Used to execute custom code during the compilation
54+
* process. No callbacks will be executed if this is `null`.
55+
* @return The `Reporter` used. Use `Reporter#hasErrors` to check
56+
* if compilation succeeded.
57+
*/
58+
final def process(args: Array[String], reporter: Reporter = null,
59+
callback: CompilerCallback = null): Reporter = {
60+
val ctx = initCtx.fresh
61+
if (reporter != null)
62+
ctx.setReporter(reporter)
63+
if (callback != null)
64+
ctx.setCompilerCallback(callback)
65+
process(args, ctx)
5066
}
5167

52-
// We overload `process` instead of using a default argument so that we
53-
// can easily call this method using reflection from `RawCompiler` in sbt.
54-
def process(args: Array[String]): Reporter = {
55-
process(args, initCtx)
68+
/** Entry point to the compiler with no optional arguments.
69+
*
70+
* This overload is provided for compatibility reasons: the
71+
* `RawCompiler` of sbt expects this method to exist and calls
72+
* it using reflection. Keeping it means that we can change
73+
* the other overloads without worrying about breaking compatibility
74+
* with sbt.
75+
*/
76+
final def process(args: Array[String]): Reporter =
77+
process(args, null, null)
78+
79+
/** Entry point to the compiler using a custom `Context`.
80+
*
81+
* In most cases, you do not need a custom `Context` and should
82+
* instead use one of the other overloads of `process`. However,
83+
* the other overloads cannot be overriden, instead you
84+
* should override this one which they call internally.
85+
*
86+
* @param args Arguments to pass to the compiler.
87+
* @param rootCtx The root Context to use.
88+
* @return The `Reporter` used. Use `Reporter#hasErrors` to check
89+
* if compilation succeeded.
90+
*/
91+
def process(args: Array[String], rootCtx: Context): Reporter = {
92+
val (fileNames, ctx) = setup(args, rootCtx)
93+
doCompile(newCompiler(), fileNames)(ctx)
5694
}
5795

5896
def main(args: Array[String]): Unit = {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ object Contexts {
427427
def setPeriod(period: Period): this.type = { this.period = period; this }
428428
def setMode(mode: Mode): this.type = { this.mode = mode; this }
429429
def setTyperState(typerState: TyperState): this.type = { this.typerState = typerState; this }
430+
def setReporter(reporter: Reporter): this.type = setTyperState(typerState.withReporter(reporter))
430431
def setNewTyperState: this.type = setTyperState(typerState.fresh(isCommittable = true))
431432
def setExploreTyperState: this.type = setTyperState(typerState.fresh(isCommittable = false))
432433
def setPrinterFn(printer: Context => Printer): this.type = { this.printerFn = printer; this }

test/dotty/partest/DPDirectCompiler.scala

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,14 @@ class DPDirectCompiler(runner: DPTestRunner) extends nest.DirectCompiler(runner)
1313
val clogWriter = new PrintWriter(clogFWriter, true)
1414
clogWriter.println("\ncompiling " + sources.mkString(" ") + "\noptions: " + opts0.mkString(" "))
1515

16-
implicit val ctx: dotty.tools.dotc.core.Contexts.Context = {
17-
val base = new dotty.tools.dotc.core.Contexts.ContextBase
18-
base.initialCtx.fresh
19-
}
20-
2116
try {
2217
val processor =
2318
if (opts0.exists(_.startsWith("#"))) dotty.tools.dotc.Bench else dotty.tools.dotc.Main
2419
val clogger = new ConsoleReporter(writer = clogWriter)
25-
val logCtx = ctx.fresh.setTyperState(ctx.typerState.withReporter(clogger))
26-
val reporter = processor.process((sources.map(_.toString) ::: opts0).toArray, logCtx)
20+
val reporter = processor.process((sources.map(_.toString) ::: opts0).toArray, clogger)
2721
if (!reporter.hasErrors) runner.genPass()
2822
else {
29-
reporter.printSummary(ctx)
23+
clogWriter.println(reporter.summary)
3024
runner.genFail(s"compilation failed with ${reporter.errorCount} errors")
3125
}
3226
} catch {

0 commit comments

Comments
 (0)