| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.javascript.rhino.Node; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class SanityCheck implements CompilerPass { |
| 28 |
|
| 29 |
static final DiagnosticType CANNOT_PARSE_GENERATED_CODE = |
| 30 |
DiagnosticType.error("JSC_CANNOT_PARSE_GENERATED_CODE", |
| 31 |
"Internal compiler error. Cannot parse generated code: {0}"); |
| 32 |
|
| 33 |
static final DiagnosticType GENERATED_BAD_CODE = DiagnosticType.error( |
| 34 |
"JSC_GENERATED_BAD_CODE", |
| 35 |
"Internal compiler error. Generated bad code." + |
| 36 |
"----------------------------------------\n" + |
| 37 |
"Expected:\n{0}\n" + |
| 38 |
"----------------------------------------\n" + |
| 39 |
"Actual:\n{1}"); |
| 40 |
|
| 41 |
private final AbstractCompiler compiler; |
| 42 |
|
| 43 |
private final AstValidator astValidator = new AstValidator(); |
| 44 |
|
| 45 |
SanityCheck(AbstractCompiler compiler) { |
| 46 |
this.compiler = compiler; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void process(Node externs, Node root) { |
| 51 |
sanityCheckAst(externs, root); |
| 52 |
sanityCheckNormalization(externs, root); |
| 53 |
sanityCheckCodeGeneration(root); |
| 54 |
sanityCheckVars(externs, root); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private void sanityCheckAst(Node externs, Node root) { |
| 61 |
astValidator.validateCodeRoot(externs); |
| 62 |
astValidator.validateCodeRoot(root); |
| 63 |
} |
| 64 |
|
| 65 |
private void sanityCheckVars(Node externs, Node root) { |
| 66 |
if (compiler.getLifeCycleStage().isNormalized()) { |
| 67 |
(new VarCheck(compiler, true)).process(externs, root); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private Node sanityCheckCodeGeneration(Node root) { |
| 79 |
if (compiler.hasHaltingErrors()) { |
| 80 |
|
| 81 |
|
| 82 |
return null; |
| 83 |
} |
| 84 |
|
| 85 |
String source = compiler.toSource(root); |
| 86 |
Node root2 = compiler.parseSyntheticCode(source); |
| 87 |
if (compiler.hasHaltingErrors()) { |
| 88 |
compiler.report(JSError.make(CANNOT_PARSE_GENERATED_CODE, |
| 89 |
Strings.truncateAtMaxLength(source, 100, true))); |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
throw new IllegalStateException("Sanity Check failed"); |
| 94 |
} |
| 95 |
|
| 96 |
String source2 = compiler.toSource(root2); |
| 97 |
if (!source.equals(source2)) { |
| 98 |
compiler.report(JSError.make(GENERATED_BAD_CODE, source, source2)); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
throw new IllegalStateException("Sanity Check failed"); |
| 103 |
} |
| 104 |
|
| 105 |
return root2; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private void sanityCheckNormalization(Node externs, Node root) { |
| 113 |
|
| 114 |
CodeChangeHandler handler = new ForbiddenChange(); |
| 115 |
compiler.addChangeHandler(handler); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
new PrepareAst(compiler, true).process(null, root); |
| 121 |
if (compiler.getLifeCycleStage().isNormalized()) { |
| 122 |
(new Normalize(compiler, true)).process(externs, root); |
| 123 |
|
| 124 |
if (compiler.getLifeCycleStage().isNormalizedUnobfuscated()) { |
| 125 |
boolean checkUserDeclarations = true; |
| 126 |
CompilerPass pass = new Normalize.VerifyConstants( |
| 127 |
compiler, checkUserDeclarations); |
| 128 |
pass.process(externs, root); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
compiler.removeChangeHandler(handler); |
| 133 |
} |
| 134 |
} |