Skip to content

Commit

Permalink
Rename SanityCheck to ValidityCheck
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171729614
  • Loading branch information
tbreisacher committed Oct 11, 2017
1 parent e4118f3 commit dee15e7
Show file tree
Hide file tree
Showing 23 changed files with 83 additions and 95 deletions.
3 changes: 0 additions & 3 deletions src/com/google/debugging/sourcemap/SourceMapConsumerV3.java
Expand Up @@ -291,9 +291,6 @@ private void completeLine(ArrayList<Entry> entries) {
previousCol = 0; previousCol = 0;
} }


/**
* Sanity check the entry.
*/
private void validateEntry(Entry entry) { private void validateEntry(Entry entry) {
Preconditions.checkState((lineCount < 0) || (line < lineCount), Preconditions.checkState((lineCount < 0) || (line < lineCount),
"line=%s, lineCount=%s", line, lineCount); "line=%s, lineCount=%s", line, lineCount);
Expand Down
Expand Up @@ -2095,7 +2095,7 @@ public CommandLineConfig setPrintPassGraph(boolean printPassGraph) {


private CompilerOptions.DevMode jscompDevMode = CompilerOptions.DevMode.OFF; private CompilerOptions.DevMode jscompDevMode = CompilerOptions.DevMode.OFF;


/** Turns on extra sanity checks */ /** Turns on extra validity checks */
public CommandLineConfig setJscompDevMode(CompilerOptions.DevMode jscompDevMode) { public CommandLineConfig setJscompDevMode(CompilerOptions.DevMode jscompDevMode) {
this.jscompDevMode = jscompDevMode; this.jscompDevMode = jscompDevMode;
return this; return this;
Expand Down
5 changes: 2 additions & 3 deletions src/com/google/javascript/jscomp/CodeGenerator.java
Expand Up @@ -334,9 +334,8 @@ protected void add(Node n, Context context) {
{ {
checkState(childCount == 1); checkState(childCount == 1);


// It's important to our sanity checker that the code // It's important to our validity checker that the code we print produces the same AST as
// we print produces the same AST as the code we parse back. // the code we parse back. NEG is a weird case because Rhino parses "- -2" as "2".
// NEG is a weird case because Rhino parses "- -2" as "2".
if (n.getFirstChild().isNumber()) { if (n.getFirstChild().isNumber()) {
cc.addNumber(-n.getFirstChild().getDouble(), n.getFirstChild()); cc.addNumber(-n.getFirstChild().getDouble(), n.getFirstChild());
} else { } else {
Expand Down
3 changes: 1 addition & 2 deletions src/com/google/javascript/jscomp/CommandLineRunner.java
Expand Up @@ -177,8 +177,7 @@ private static class Flags {
usage = "Assume input sources are to run in strict mode.") usage = "Assume input sources are to run in strict mode.")
private boolean strictModeInput = true; private boolean strictModeInput = true;


// Turn on (very slow) extra sanity checks for use when modifying the // Turn on (very slow) extra validity checks for use when modifying the compiler.
// compiler.
@Option( @Option(
name = "--jscomp_dev_mode", name = "--jscomp_dev_mode",
hidden = true, hidden = true,
Expand Down
21 changes: 10 additions & 11 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -902,7 +902,7 @@ private void performPostCompilationTasksInternal() {
} }


if (options.devMode == DevMode.START_AND_END) { if (options.devMode == DevMode.START_AND_END) {
runSanityCheck(); runValidityCheck();
} }
setProgress(1.0, "recordFunctionInformation"); setProgress(1.0, "recordFunctionInformation");


Expand Down Expand Up @@ -1055,7 +1055,7 @@ public void transpileAndDontCheck() {
private PhaseOptimizer createPhaseOptimizer() { private PhaseOptimizer createPhaseOptimizer() {
PhaseOptimizer phaseOptimizer = new PhaseOptimizer(this, tracker); PhaseOptimizer phaseOptimizer = new PhaseOptimizer(this, tracker);
if (options.devMode == DevMode.EVERY_PASS) { if (options.devMode == DevMode.EVERY_PASS) {
phaseOptimizer.setSanityCheck(sanityCheck); phaseOptimizer.setValidityCheck(validityCheck);
} }
if (options.getCheckDeterminism()) { if (options.getCheckDeterminism()) {
phaseOptimizer.setPrintAstHashcodes(true); phaseOptimizer.setPrintAstHashcodes(true);
Expand Down Expand Up @@ -1090,22 +1090,21 @@ void process(CompilerPass p) {
p.process(externsRoot, jsRoot); p.process(externsRoot, jsRoot);
} }


private final PassFactory sanityCheck = private final PassFactory validityCheck = new PassFactory("validityCheck", false) {
new PassFactory("sanityCheck", false) {
@Override @Override
protected CompilerPass create(AbstractCompiler compiler) { protected CompilerPass create(AbstractCompiler compiler) {
return new SanityCheck(compiler); return new ValidityCheck(compiler);
} }
}; };


private void maybeSanityCheck() { private void maybeRunValidityCheck() {
if (options.devMode == DevMode.EVERY_PASS) { if (options.devMode == DevMode.EVERY_PASS) {
runSanityCheck(); runValidityCheck();
} }
} }


private void runSanityCheck() { private void runValidityCheck() {
sanityCheck.create(this).process(externsRoot, jsRoot); validityCheck.create(this).process(externsRoot, jsRoot);
} }


/** /**
Expand Down Expand Up @@ -1147,7 +1146,7 @@ void endPass(String passName) {
currentPassName = null; currentPassName = null;
currentTracer = null; currentTracer = null;


maybeSanityCheck(); maybeRunValidityCheck();
} }


@Override @Override
Expand Down Expand Up @@ -1811,7 +1810,7 @@ Node parseInputs() {
} }


if (devMode) { if (devMode) {
runSanityCheck(); runValidityCheck();
if (hasErrors()) { if (hasErrors()) {
return null; return null;
} }
Expand Down
6 changes: 3 additions & 3 deletions src/com/google/javascript/jscomp/CompilerOptions.java
Expand Up @@ -252,7 +252,7 @@ boolean shouldPrintExterns() {
boolean skipNonTranspilationPasses; boolean skipNonTranspilationPasses;


/** /**
* Configures the compiler to run expensive sanity checks after * Configures the compiler to run expensive validity checks after
* every pass. Only intended for internal development. * every pass. Only intended for internal development.
*/ */
DevMode devMode; DevMode devMode;
Expand Down Expand Up @@ -3145,10 +3145,10 @@ FeatureSet toFeatureSet() {
} }
} }


/** When to do the extra sanity checks */ /** When to do the extra validity checks */
public static enum DevMode { public static enum DevMode {
/** /**
* Don't do any extra sanity checks. * Don't do any extra checks.
*/ */
OFF, OFF,


Expand Down
13 changes: 6 additions & 7 deletions src/com/google/javascript/jscomp/DefaultPassConfig.java
Expand Up @@ -940,8 +940,8 @@ protected List<PassFactory> getOptimizations() {
} }


// Safety checks // Safety checks
passes.add(sanityCheckAst); passes.add(checkAstValidity);
passes.add(sanityCheckVars); passes.add(varCheckValidity);


// Raise to ES6, if allowed // Raise to ES6, if allowed
if (options.getLanguageOut().toFeatureSet().contains(FeatureSet.ES6)) { if (options.getLanguageOut().toFeatureSet().contains(FeatureSet.ES6)) {
Expand Down Expand Up @@ -3140,9 +3140,8 @@ protected FeatureSet featureSet() {
} }
}; };


/** Checks that all variables are defined. */ private final PassFactory checkAstValidity =
private final PassFactory sanityCheckAst = new PassFactory("checkAstValidity", true) {
new PassFactory("sanityCheckAst", true) {
@Override @Override
protected CompilerPass create(AbstractCompiler compiler) { protected CompilerPass create(AbstractCompiler compiler) {
return new AstValidator(compiler); return new AstValidator(compiler);
Expand All @@ -3155,8 +3154,8 @@ protected FeatureSet featureSet() {
}; };


/** Checks that all variables are defined. */ /** Checks that all variables are defined. */
private final PassFactory sanityCheckVars = private final PassFactory varCheckValidity =
new PassFactory("sanityCheckVars", true) { new PassFactory("varCheckValidity", true) {
@Override @Override
protected CompilerPass create(AbstractCompiler compiler) { protected CompilerPass create(AbstractCompiler compiler) {
return new VarCheck(compiler, true); return new VarCheck(compiler, true);
Expand Down
5 changes: 3 additions & 2 deletions src/com/google/javascript/jscomp/Es6RewriteGenerators.java
Expand Up @@ -905,7 +905,8 @@ private void visitLoop(String label) {
guard = currentStatement.removeFirstChild(); guard = currentStatement.removeFirstChild();
} }


Node condition, prestatement; Node condition;
Node prestatement;


if (guard.isNormalBlock()) { if (guard.isNormalBlock()) {
prestatement = guard.removeFirstChild(); prestatement = guard.removeFirstChild();
Expand Down Expand Up @@ -1375,7 +1376,7 @@ static Node preloadGeneratorSkeletonAndReportChange(AbstractCompiler compiler) {
* If the skeleton is already preloaded, does not do anything, just returns the node. * If the skeleton is already preloaded, does not do anything, just returns the node.
* reportChange tells the function whether to report a code change in the enclosing scope. * reportChange tells the function whether to report a code change in the enclosing scope.
* *
* Because sanity checks happen between passes, we need to report the change if the generator * Because validity checks happen between passes, we need to report the change if the generator
* was preloaded in the {@link EarlyEs6ToEs3Converter} class. * was preloaded in the {@link EarlyEs6ToEs3Converter} class.
* However, if the generator was preloaded in this {@link Es6RewriteGenerators} class, we do not * However, if the generator was preloaded in this {@link Es6RewriteGenerators} class, we do not
* want to report the change since it will be removed by {@link #cleanUpGeneratorSkeleton} * want to report the change since it will be removed by {@link #cleanUpGeneratorSkeleton}
Expand Down
2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/InlineFunctions.java
Expand Up @@ -853,7 +853,7 @@ void removeInlinedFunctions() {
} }
} }


/** Sanity check to verify, that expression rewriting didn't make a call inaccessible. */ /** Check to verify that expression rewriting didn't make a call inaccessible. */
void verifyAllReferencesInlined(FunctionState functionState) { void verifyAllReferencesInlined(FunctionState functionState) {
for (Reference ref : functionState.getReferences()) { for (Reference ref : functionState.getReferences()) {
if (!ref.inlined) { if (!ref.inlined) {
Expand Down
Expand Up @@ -138,7 +138,6 @@ private void hoistConstantLikeField(Node clinitAssignment, Node declarationAssig
} }
declarationInClass.putBooleanProp(Node.IS_CONSTANT_VAR, true); declarationInClass.putBooleanProp(Node.IS_CONSTANT_VAR, true);


// Sanity check
checkState(NodeUtil.isLiteralValue(declarationAssignedValue, false /* includeFunctions */)); checkState(NodeUtil.isLiteralValue(declarationAssignedValue, false /* includeFunctions */));
} }


Expand Down
28 changes: 14 additions & 14 deletions src/com/google/javascript/jscomp/PhaseOptimizer.java
Expand Up @@ -43,7 +43,7 @@ class PhaseOptimizer implements CompilerPass {
private final PerformanceTracker tracker; private final PerformanceTracker tracker;
private final List<CompilerPass> passes; private final List<CompilerPass> passes;
private boolean inLoop; private boolean inLoop;
private PassFactory sanityCheck; private PassFactory validityCheck;
private boolean printAstHashcodes = false; private boolean printAstHashcodes = false;


private double progress = 0.0; private double progress = 0.0;
Expand All @@ -64,7 +64,7 @@ class PhaseOptimizer implements CompilerPass {


private final boolean useSizeHeuristicToStopOptimizationLoop; private final boolean useSizeHeuristicToStopOptimizationLoop;


// Used for sanity checking passes // Checks that passes have reported code changes correctly.
private ChangeVerifier changeVerifier; private ChangeVerifier changeVerifier;


/** /**
Expand Down Expand Up @@ -191,10 +191,10 @@ Loop addFixedPointLoop() {
} }


/** /**
* Adds a sanity checker to be run after every pass. Intended for development. * Adds a checker to be run after every pass. Intended for development.
*/ */
void setSanityCheck(PassFactory sanityCheck) { void setValidityCheck(PassFactory validityCheck) {
this.sanityCheck = sanityCheck; this.validityCheck = validityCheck;
this.changeVerifier = new ChangeVerifier(compiler).snapshot(jsRoot); this.changeVerifier = new ChangeVerifier(compiler).snapshot(jsRoot);
} }


Expand Down Expand Up @@ -244,17 +244,17 @@ private void maybePrintAstHashcodes(String passName, Node root) {
} }


/** /**
* Runs the sanity check if it is available. * Runs the validity check if it is available.
*/ */
private void maybeSanityCheck(String passName, Node externs, Node root) { private void maybeRunValidityCheck(String passName, Node externs, Node root) {
if (sanityCheck == null) { if (validityCheck == null) {
return; return;
} }
try { try {
sanityCheck.create(compiler).process(externs, root); validityCheck.create(compiler).process(externs, root);
changeVerifier.checkRecordedChanges(passName, jsRoot); changeVerifier.checkRecordedChanges(passName, jsRoot);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalStateException("Sanity check failed for pass: " + passName, e); throw new IllegalStateException("Validity checks failed for pass: " + passName, e);
} }
} }


Expand Down Expand Up @@ -286,8 +286,8 @@ public void process(Node externs, Node root) {
} }


logger.fine("Running pass " + name); logger.fine("Running pass " + name);
if (sanityCheck != null) { if (validityCheck != null) {
// Before running the pass, clone the AST so you can sanity-check the // Before running the pass, clone the AST so you can check the
// changed AST against the clone after the pass finishes. // changed AST against the clone after the pass finishes.
changeVerifier = new ChangeVerifier(compiler).snapshot(jsRoot); changeVerifier = new ChangeVerifier(compiler).snapshot(jsRoot);
} }
Expand Down Expand Up @@ -320,11 +320,11 @@ public void process(Node externs, Node root) {
tracker.recordPassStop(name, traceRuntime); tracker.recordPassStop(name, traceRuntime);
} }
maybePrintAstHashcodes(name, root); maybePrintAstHashcodes(name, root);
maybeSanityCheck(name, externs, root); maybeRunValidityCheck(name, externs, root);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// TODO(johnlenz): Remove this once the normalization checks report // TODO(johnlenz): Remove this once the normalization checks report
// errors instead of exceptions. // errors instead of exceptions.
throw new RuntimeException("Sanity check failed for " + name, e); throw new RuntimeException("Validity check failed for " + name, e);
} }
} }


Expand Down
5 changes: 2 additions & 3 deletions src/com/google/javascript/jscomp/PrepareAst.java
Expand Up @@ -57,9 +57,8 @@ public void process(Node externs, Node root) {
normalizeNodeTypes(root); normalizeNodeTypes(root);
} else { } else {
// Don't perform "PrepareAnnotations" when doing checks as // Don't perform "PrepareAnnotations" when doing checks as
// they currently aren't valid during sanity checks. In particular, // they currently aren't valid during validity checks. In particular,
// they DIRECT_EVAL shouldn't be applied after inlining has been // they DIRECT_EVAL shouldn't be applied after inlining has been performed.
// performed.
if (externs != null) { if (externs != null) {
NodeTraversal.traverseEs6( NodeTraversal.traverseEs6(
compiler, externs, new PrepareAnnotations()); compiler, externs, new PrepareAnnotations());
Expand Down
Expand Up @@ -42,18 +42,18 @@
class SourceInformationAnnotator extends class SourceInformationAnnotator extends
NodeTraversal.AbstractPostOrderCallback { NodeTraversal.AbstractPostOrderCallback {
private final String sourceFile; private final String sourceFile;
private final boolean doSanityChecks; private final boolean checkAnnotated;


public SourceInformationAnnotator( public SourceInformationAnnotator(
String sourceFile, boolean doSanityChecks) { String sourceFile, boolean checkAnnotated) {
this.sourceFile = sourceFile; this.sourceFile = sourceFile;
this.doSanityChecks = doSanityChecks; this.checkAnnotated = checkAnnotated;
} }


@Override @Override
public void visit(NodeTraversal t, Node n, Node parent) { public void visit(NodeTraversal t, Node n, Node parent) {
// Verify the source file is annotated. // Verify the source file is annotated.
if (doSanityChecks && sourceFile != null) { if (checkAnnotated && sourceFile != null) {
checkState(sourceFile.equals(n.getSourceFileName())); checkState(sourceFile.equals(n.getSourceFileName()));
} }


Expand Down
Expand Up @@ -25,7 +25,7 @@
* *
* @author nicksantos@google.com (Nick Santos) * @author nicksantos@google.com (Nick Santos)
*/ */
class SanityCheck implements CompilerPass { class ValidityCheck implements CompilerPass {


static final DiagnosticType CANNOT_PARSE_GENERATED_CODE = static final DiagnosticType CANNOT_PARSE_GENERATED_CODE =
DiagnosticType.error("JSC_CANNOT_PARSE_GENERATED_CODE", DiagnosticType.error("JSC_CANNOT_PARSE_GENERATED_CODE",
Expand All @@ -47,29 +47,29 @@ class SanityCheck implements CompilerPass {
private final AbstractCompiler compiler; private final AbstractCompiler compiler;
private final AstValidator astValidator; private final AstValidator astValidator;


SanityCheck(AbstractCompiler compiler) { ValidityCheck(AbstractCompiler compiler) {
this.compiler = compiler; this.compiler = compiler;
this.astValidator = new AstValidator(compiler); this.astValidator = new AstValidator(compiler);
} }


@Override @Override
public void process(Node externs, Node root) { public void process(Node externs, Node root) {
sanityCheckAst(externs, root); checkAst(externs, root);
sanityCheckNormalization(externs, root); checkNormalization(externs, root);
sanityCheckCodeGeneration(root); checkCodeGeneration(root);
sanityCheckVars(externs, root); checkVars(externs, root);
sanityCheckExternProperties(externs); checkExternProperties(externs);
} }


/** /**
* Sanity check the AST is structurally accurate. * Check that the AST is structurally accurate.
*/ */
private void sanityCheckAst(Node externs, Node root) { private void checkAst(Node externs, Node root) {
astValidator.validateCodeRoot(externs); astValidator.validateCodeRoot(externs);
astValidator.validateCodeRoot(root); astValidator.validateCodeRoot(root);
} }


private void sanityCheckVars(Node externs, Node root) { private void checkVars(Node externs, Node root) {
if (compiler.getLifeCycleStage().isNormalized()) { if (compiler.getLifeCycleStage().isNormalized()) {
(new VarCheck(compiler, true)).process(externs, root); (new VarCheck(compiler, true)).process(externs, root);
} }
Expand All @@ -82,15 +82,15 @@ private void sanityCheckVars(Node externs, Node root) {
* *
* @return The regenerated parse tree. Null on error. * @return The regenerated parse tree. Null on error.
*/ */
private Node sanityCheckCodeGeneration(Node root) { private Node checkCodeGeneration(Node root) {
if (compiler.hasHaltingErrors()) { if (compiler.hasHaltingErrors()) {
// Don't even bother checking code generation if we already know the // Don't even bother checking code generation if we already know the
// the code is bad. // the code is bad.
return null; return null;
} }


String source = compiler.toSource(root); String source = compiler.toSource(root);
Node root2 = compiler.parseSyntheticCode("<SanityCheck.java>", source); Node root2 = compiler.parseSyntheticCode("<ValidityCheck.java>", source);
if (compiler.hasHaltingErrors()) { if (compiler.hasHaltingErrors()) {
compiler.report(JSError.make(CANNOT_PARSE_GENERATED_CODE, compiler.report(JSError.make(CANNOT_PARSE_GENERATED_CODE,
Strings.truncateAtMaxLength(source, 100, true))); Strings.truncateAtMaxLength(source, 100, true)));
Expand All @@ -116,7 +116,7 @@ private Node sanityCheckCodeGeneration(Node root) {
* Sanity checks the AST. This is by verifying the normalization passes do * Sanity checks the AST. This is by verifying the normalization passes do
* nothing. * nothing.
*/ */
private void sanityCheckNormalization(Node externs, Node root) { private void checkNormalization(Node externs, Node root) {
// Verify nothing has inappropriately denormalize the AST. // Verify nothing has inappropriately denormalize the AST.
CodeChangeHandler handler = new ForbiddenChange(); CodeChangeHandler handler = new ForbiddenChange();
compiler.addChangeHandler(handler); compiler.addChangeHandler(handler);
Expand All @@ -138,7 +138,7 @@ private void sanityCheckNormalization(Node externs, Node root) {
compiler.removeChangeHandler(handler); compiler.removeChangeHandler(handler);
} }


private void sanityCheckExternProperties(Node externs) { private void checkExternProperties(Node externs) {
Set<String> externProperties = compiler.getExternProperties(); Set<String> externProperties = compiler.getExternProperties();
if (externProperties == null) { if (externProperties == null) {
// GatherExternProperties hasn't run yet. Don't report a violation. // GatherExternProperties hasn't run yet. Don't report a violation.
Expand Down

0 comments on commit dee15e7

Please sign in to comment.