Revision: 8784
Author: [email protected]
Date: Wed Sep 15 05:54:44 2010
Log: Adds a -strict option to the GWT compiler. If this option is specified,
then the compile will fail if any of the input files are bad.

Review at http://gwt-code-reviews.appspot.com/853801

Review by: [email protected]
http://code.google.com/p/google-web-toolkit/source/detail?r=8784

Added:
 /trunk/dev/core/src/com/google/gwt/dev/util/arg/ArgHandlerStrict.java
 /trunk/dev/core/src/com/google/gwt/dev/util/arg/OptionStrict.java
 /trunk/user/test/com/google/gwt/dev/StrictModeTest.java
 /trunk/user/test/com/google/gwt/dev/strict
 /trunk/user/test/com/google/gwt/dev/strict/bad
 /trunk/user/test/com/google/gwt/dev/strict/bad/Bad.gwt.xml
 /trunk/user/test/com/google/gwt/dev/strict/bad/client
 /trunk/user/test/com/google/gwt/dev/strict/bad/client/BadSource.java
 /trunk/user/test/com/google/gwt/dev/strict/bad/client/Entry.java
 /trunk/user/test/com/google/gwt/dev/strict/good
 /trunk/user/test/com/google/gwt/dev/strict/good/Good.gwt.xml
 /trunk/user/test/com/google/gwt/dev/strict/good/client
 /trunk/user/test/com/google/gwt/dev/strict/good/client/Entry.java
Modified:
 /trunk/dev/core/src/com/google/gwt/dev/Precompile.java
 /trunk/dev/core/src/com/google/gwt/dev/javac/CompilationState.java
 /trunk/dev/core/src/com/google/gwt/dev/jjs/JJSOptions.java
 /trunk/dev/core/src/com/google/gwt/dev/jjs/JJSOptionsImpl.java
 /trunk/user/test/com/google/gwt/core/CoreSuite.java

=======================================
--- /dev/null
+++ /trunk/dev/core/src/com/google/gwt/dev/util/arg/ArgHandlerStrict.java Wed Sep 15 05:54:44 2010
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.gwt.dev.util.arg;
+
+import com.google.gwt.util.tools.ArgHandlerFlag;
+
+/**
+ * Argument handler for {...@link OptionStrict}.
+ */
+public class ArgHandlerStrict extends ArgHandlerFlag {
+  private final OptionStrict options;
+
+  public ArgHandlerStrict(OptionStrict options) {
+    this.options = options;
+  }
+
+  @Override
+  public String getPurpose() {
+    return "Only succeed if no input files have errors";
+  }
+
+  @Override
+  public String getTag() {
+    return "-strict";
+  }
+
+  @Override
+  public boolean setFlag() {
+    options.setStrict(true);
+    return true;
+  }
+}
=======================================
--- /dev/null
+++ /trunk/dev/core/src/com/google/gwt/dev/util/arg/OptionStrict.java Wed Sep 15 05:54:44 2010
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.gwt.dev.util.arg;
+
+/**
+ * Option for enforcing strict compiles. If this option is on, then fail the
+ * compile if any of the input files have errors.
+ */
+public interface OptionStrict {
+  boolean isStrict();
+
+  void setStrict(boolean strict);
+}
=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/dev/StrictModeTest.java Wed Sep 15 05:54:44 2010
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.gwt.dev;
+
+import com.google.gwt.core.ext.TreeLogger;
+import com.google.gwt.core.ext.UnableToCompleteException;
+import com.google.gwt.dev.cfg.ModuleDef;
+import com.google.gwt.dev.cfg.ModuleDefLoader;
+import com.google.gwt.dev.jjs.JJSOptions;
+
+import junit.framework.TestCase;
+
+/**
+ * Test the -strict option to the GWT compiler.
+ */
+public class StrictModeTest extends TestCase {
+  /**
+   * Module name for a module with a bad source file.
+   */
+  private static final String BAD = "com.google.gwt.dev.strict.bad.Bad";
+
+  /**
+   * Module name for a module with no bad source files.
+   */
+  private static final String GOOD = "com.google.gwt.dev.strict.good.Good";
+
+  private TreeLogger logger = TreeLogger.NULL;
+
+  private JJSOptions options = new Compiler.CompilerOptionsImpl();
+
+  /**
+   * A normal compile with a bad file should still succeed.
+   */
+  public void testBadCompile() throws UnableToCompleteException {
+    precompile(BAD);
+  }
+
+  /**
+   * A bad compile in strict mode.
+   */
+  public void testBadCompileStrict() {
+    options.setStrict(true);
+    try {
+      precompile(BAD);
+      fail("Should have failed");
+    } catch (UnableToCompleteException expected) {
+    }
+  }
+
+  /**
+   * A normal compile with a bad file should still succeed.
+   */
+  public void testBadValidate() {
+    assertTrue(validate(BAD));
+  }
+
+  /**
+   * A bad compile in strict mode.
+   */
+  public void testBadValidateStrict() {
+    options.setStrict(true);
+    assertFalse(validate(BAD));
+  }
+
+  /**
+   * Test a plain old successful compile.
+   */
+  public void testGoodCompile() throws UnableToCompleteException {
+    precompile(GOOD);
+  }
+
+  /**
+   * A good compile in strict mode.
+   */
+  public void testGoodCompileStrict() throws UnableToCompleteException {
+    options.setStrict(true);
+    precompile(GOOD);
+  }
+
+  /**
+   * Test a plain old successful validate.
+   */
+  public void testGoodValidate() {
+    assertTrue(validate(GOOD));
+  }
+
+  /**
+   * A good compile in strict mode.
+   */
+  public void testGoodValidateStrict() {
+    options.setStrict(true);
+    assertTrue(validate(GOOD));
+  }
+
+ private void precompile(String moduleName) throws UnableToCompleteException { + ModuleDef module = ModuleDefLoader.loadFromClassPath(logger, moduleName);
+    if (Precompile.precompile(logger, options, module, null) == null) {
+      throw new UnableToCompleteException();
+    }
+  }
+
+  private boolean validate(String moduleName) {
+    ModuleDef module;
+    try {
+      module = ModuleDefLoader.loadFromClassPath(logger, moduleName);
+    } catch (UnableToCompleteException e) {
+      fail("Failed to load the module definition");
+      return false;
+    }
+    return Precompile.validate(logger, options, module, null);
+  }
+}
=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/dev/strict/bad/Bad.gwt.xml Wed Sep 15 05:54:44 2010
@@ -0,0 +1,4 @@
+<module>
+  <inherits name='com.google.gwt.junit.JUnit'/>
+  <entry-point class='com.google.gwt.dev.strict.bad.client.Entry'/>
+</module>
=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/dev/strict/bad/client/BadSource.java Wed Sep 15 05:54:44 2010
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.dev.strict.bad.client;
+
+/**
+ * Used by {...@link com.google.gwt.dev.StrictModeTest}.
+ */
+public class BadSource {
+  public void useThread() {
+    new Thread();
+  }
+}
=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/dev/strict/bad/client/Entry.java Wed Sep 15 05:54:44 2010
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.gwt.dev.strict.bad.client;
+
+import com.google.gwt.core.client.EntryPoint;
+
+/**
+ * Used by {...@link com.google.gwt.dev.StrictModeTest}.
+ */
+public class Entry implements EntryPoint {
+  public void onModuleLoad() {
+  }
+}
=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/dev/strict/good/Good.gwt.xml Wed Sep 15 05:54:44 2010
@@ -0,0 +1,4 @@
+<module>
+  <inherits name='com.google.gwt.junit.JUnit'/>
+  <entry-point class='com.google.gwt.dev.strict.good.client.Entry'/>
+</module>
=======================================
--- /dev/null
+++ /trunk/user/test/com/google/gwt/dev/strict/good/client/Entry.java Wed Sep 15 05:54:44 2010
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.gwt.dev.strict.good.client;
+
+import com.google.gwt.core.client.EntryPoint;
+
+/**
+ * Used by {...@link com.google.gwt.dev.StrictModeTest}.
+ */
+public class Entry implements EntryPoint {
+  public void onModuleLoad() {
+  }
+}
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/Precompile.java Tue Sep 7 04:40:43 2010 +++ /trunk/dev/core/src/com/google/gwt/dev/Precompile.java Wed Sep 15 05:54:44 2010
@@ -60,6 +60,7 @@
 import com.google.gwt.dev.util.arg.ArgHandlerScriptStyle;
 import com.google.gwt.dev.util.arg.ArgHandlerSoyc;
 import com.google.gwt.dev.util.arg.ArgHandlerSoycDetailed;
+import com.google.gwt.dev.util.arg.ArgHandlerStrict;
 import com.google.gwt.dev.util.arg.ArgHandlerValidateOnlyFlag;
 import com.google.gwt.dev.util.arg.OptionDisableUpdateCheck;
 import com.google.gwt.dev.util.arg.OptionEnableGeneratingOnShards;
@@ -121,6 +122,7 @@
       registerHandler(new ArgHandlerCompileReport(options));
       registerHandler(new ArgHandlerSoyc(options));
       registerHandler(new ArgHandlerSoycDetailed(options));
+      registerHandler(new ArgHandlerStrict(options));
     }

     @Override
@@ -208,6 +210,11 @@
     public boolean isSoycExtra() {
       return jjsOptions.isSoycExtra();
     }
+
+    @Override
+    public boolean isStrict() {
+      return jjsOptions.isStrict();
+    }

     public boolean isUpdateCheckDisabled() {
       return disableUpdateCheck;
@@ -272,6 +279,11 @@
     public void setSoycExtra(boolean soycExtra) {
       jjsOptions.setSoycExtra(soycExtra);
     }
+
+    @Override
+    public void setStrict(boolean strict) {
+      jjsOptions.setStrict(strict);
+    }

     public void setValidateOnly(boolean validateOnly) {
       this.validateOnly = validateOnly;
@@ -463,6 +475,9 @@
Event validateEvent = SpeedTracerLogger.start(CompilerEventType.VALIDATE);
     try {
CompilationState compilationState = module.getCompilationState(logger);
+      if (jjsOptions.isStrict() && compilationState.hasErrors()) {
+        abortDueToStrictMode(logger);
+      }
       String[] declEntryPts = module.getEntryPointTypeNames();
       String[] additionalRootTypes = null;
       if (declEntryPts.length == 0) {
@@ -507,6 +522,10 @@

     try {
CompilationState compilationState = module.getCompilationState(logger);
+      if (jjsOptions.isStrict() && compilationState.hasErrors()) {
+        abortDueToStrictMode(logger);
+      }
+
       String[] declEntryPts = module.getEntryPointTypeNames();
       if (declEntryPts.length == 0) {
logger.log(TreeLogger.ERROR, "Module has no entry points defined", null);
@@ -552,6 +571,13 @@
       precompileEvent.end();
     }
   }
+
+  private static void abortDueToStrictMode(TreeLogger logger)
+      throws UnableToCompleteException {
+    logger.log(TreeLogger.ERROR,
+        "Aborting compile due to errors in some input files");
+    throw new UnableToCompleteException();
+  }

   private static AbstractCompiler getCompiler(ModuleDef module) {
ConfigurationProperty compilerClassProp = module.getProperties().createConfiguration(
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/javac/CompilationState.java Thu Aug 19 06:52:40 2010 +++ /trunk/dev/core/src/com/google/gwt/dev/javac/CompilationState.java Wed Sep 15 05:54:44 2010
@@ -131,6 +131,18 @@
   public TypeOracle getTypeOracle() {
     return mediator.getTypeOracle();
   }
+
+  /**
+ * Whether any errors were encountered while building this compilation state.
+   */
+  public boolean hasErrors() {
+    for (CompilationUnit unit : unitMap.values()) {
+      if (unit.isError()) {
+        return true;
+      }
+    }
+    return false;
+  }

   /**
    * For testing.
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/jjs/JJSOptions.java Wed Nov 11 16:03:42 2009 +++ /trunk/dev/core/src/com/google/gwt/dev/jjs/JJSOptions.java Wed Sep 15 05:54:44 2010
@@ -25,6 +25,7 @@
 import com.google.gwt.dev.util.arg.OptionScriptStyle;
 import com.google.gwt.dev.util.arg.OptionSoycDetailed;
 import com.google.gwt.dev.util.arg.OptionSoycEnabled;
+import com.google.gwt.dev.util.arg.OptionStrict;

 /**
  * Controls options for the {...@link JavaToJavaScriptCompiler}.
@@ -32,5 +33,6 @@
 public interface JJSOptions extends OptionAggressivelyOptimize,
OptionDisableClassMetadata, OptionDisableCastChecking, OptionDraftCompile,
     OptionEnableAssertions, OptionRunAsyncEnabled, OptionScriptStyle,
-    OptionSoycEnabled, OptionSoycDetailed, OptionOptimizePrecompile {
-}
+    OptionSoycEnabled, OptionSoycDetailed, OptionOptimizePrecompile,
+    OptionStrict {
+}
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/jjs/JJSOptionsImpl.java Wed Nov 11 16:03:42 2009 +++ /trunk/dev/core/src/com/google/gwt/dev/jjs/JJSOptionsImpl.java Wed Sep 15 05:54:44 2010
@@ -32,6 +32,7 @@
   private boolean runAsyncEnabled = true;
   private boolean soycEnabled = false;
   private boolean soycExtra = false;
+  private boolean strict = false;

   public JJSOptionsImpl() {
   }
@@ -50,6 +51,7 @@
     setRunAsyncEnabled(other.isRunAsyncEnabled());
     setSoycEnabled(other.isSoycEnabled());
     setSoycExtra(other.isSoycExtra());
+    setStrict(other.isStrict());
   }

   public JsOutputOption getOutput() {
@@ -91,6 +93,11 @@
   public boolean isSoycExtra() {
     return soycExtra;
   }
+
+  @Override
+  public boolean isStrict() {
+    return strict;
+  }

   public void setAggressivelyOptimize(boolean aggressivelyOptimize) {
     this.aggressivelyOptimize = aggressivelyOptimize;
@@ -131,4 +138,9 @@
   public void setSoycExtra(boolean enabled) {
     soycExtra = enabled;
   }
-}
+
+  @Override
+  public void setStrict(boolean strict) {
+    this.strict = strict;
+  }
+}
=======================================
--- /trunk/user/test/com/google/gwt/core/CoreSuite.java Mon Jun 7 12:20:31 2010 +++ /trunk/user/test/com/google/gwt/core/CoreSuite.java Wed Sep 15 05:54:44 2010
@@ -25,6 +25,7 @@
 import com.google.gwt.core.client.impl.SchedulerImplTest;
 import com.google.gwt.core.client.impl.StackTraceCreatorTest;
 import com.google.gwt.core.client.impl.XhrLoadingStrategyTest;
+import com.google.gwt.dev.StrictModeTest;
 import com.google.gwt.junit.tools.GWTTestSuite;

 import junit.framework.Test;
@@ -46,6 +47,7 @@
     suite.addTestSuite(SchedulerImplTest.class);
     suite.addTestSuite(SchedulerTest.class);
     suite.addTestSuite(StackTraceCreatorTest.class);
+    suite.addTestSuite(StrictModeTest.class);
     suite.addTestSuite(XhrLoadingStrategyTest.class);
     // $JUnit-END$

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to