From 389dec8deeb4f0c765e31c1ea54a4f7dff30f842 Mon Sep 17 00:00:00 2001 From: bradfordcsmith Date: Tue, 9 May 2017 16:59:09 -0700 Subject: [PATCH] Split methods for stage 1 and stage 2 compilation. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=155572139 --- .../jscomp/AbstractCommandLineRunner.java | 10 +++- .../google/javascript/jscomp/Compiler.java | 48 +++++++++++++++---- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/com/google/javascript/jscomp/AbstractCommandLineRunner.java b/src/com/google/javascript/jscomp/AbstractCommandLineRunner.java index eb237ccd2a6..c069ffa1105 100644 --- a/src/com/google/javascript/jscomp/AbstractCommandLineRunner.java +++ b/src/com/google/javascript/jscomp/AbstractCommandLineRunner.java @@ -1138,7 +1138,10 @@ private Result performFullCompilationWithModules( if (options.getInstrumentForCoverageOnly()) { compiler.instrumentForCoverage(); } else { - compiler.stage1AndStage2Passes(); + compiler.stage1Passes(); + if (!compiler.hasErrors()) { + compiler.stage2Passes(); + } } compiler.completeCompilation(); } @@ -1161,7 +1164,10 @@ private Result performFullCompilation( if (options.getInstrumentForCoverageOnly()) { compiler.instrumentForCoverage(); } else { - compiler.stage1AndStage2Passes(); + compiler.stage1Passes(); + if (!compiler.hasErrors()) { + compiler.stage2Passes(); + } } compiler.completeCompilation(); } diff --git a/src/com/google/javascript/jscomp/Compiler.java b/src/com/google/javascript/jscomp/Compiler.java index 8475820c9ae..303e7979a85 100644 --- a/src/com/google/javascript/jscomp/Compiler.java +++ b/src/com/google/javascript/jscomp/Compiler.java @@ -760,7 +760,10 @@ public Result compile( // runner, not the compiler. instrumentForCoverage(); } else { - stage1AndStage2Passes(); + stage1Passes(); + if (!hasErrors()) { + stage2Passes(); + } } completeCompilation(); } @@ -809,7 +812,10 @@ public Result compileModules( if (options.getInstrumentForCoverageOnly()) { instrumentForCoverage(); } else { - stage1AndStage2Passes(); + stage1Passes(); + if (!hasErrors()) { + stage2Passes(); + } } completeCompilation(); } @@ -820,16 +826,16 @@ public Result compileModules( } /** - * Perform checks transpilation and optimization. + * Perform compiler passes for stage 1 of compilation. + * + *

Stage 1 consists primarily of error and type checking passes. * *

{@code parseForCompilation()} must be called before this method is called. * *

The caller is responsible for also calling {@code generateReport()} to generate a report of * warnings and errors to stderr. See the invocation in {@link #compile} for a good example. - * - *

TODO(bradfordcsmith): Break this up into stage1Passes() and stage2Passes(). */ - public void stage1AndStage2Passes() { + public void stage1Passes() { checkState( inputs != null && !inputs.isEmpty(), "No inputs. Did you call init() or initModules()?"); checkState(!hasErrors()); @@ -839,7 +845,31 @@ public void stage1AndStage2Passes() { @Override public Void call() throws Exception { performChecksAndTranspilation(); - if (!hasErrors() && options.shouldOptimize()) { + return null; + } + }); + } + + /** + * Perform compiler passes for stage 2 of compilation. + * + *

Stage 2 consists primarily of optimization passes. + * + *

{@code stage1Passes()} must be called before this method is called. + * + *

The caller is responsible for also calling {@code generateReport()} to generate a report of + * warnings and errors to stderr. See the invocation in {@link #compile} for a good example. + */ + public void stage2Passes() { + checkState( + inputs != null && !inputs.isEmpty(), "No inputs. Did you call init() or initModules()?"); + checkState(!hasErrors()); + checkState(!options.getInstrumentForCoverageOnly()); + runInCompilerThread( + new Callable() { + @Override + public Void call() throws Exception { + if (options.shouldOptimize()) { performOptimizations(); } return null; @@ -929,8 +959,8 @@ private void completeCompilationInternal() { *

The caller is responsible for also calling {@code generateReport()} to generate a report of * warnings and errors to stderr. See the invocation in {@link #compile} for a good example. * - *

Do not call both this and stage1AndStage2Passes(). They should be considered mutually - * exclusive. + *

This method is mutually exclusive with stage1Passes() and stage2Passes(). + * Either call those two methods or this one, but not both. */ public void instrumentForCoverage() { checkState(