Debian Codegraph @

src/com/google/javascript/jscomp/SanityCheck.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.javascript.rhino.Node;
20
21 /**
22 * A compiler pass that verifies the structure of the AST conforms
23 * to a number of invariants. Because this can add a lot of overhead,
24 * we only run this in development mode.
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 * Sanity check the AST is structurally accurate.
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 * Sanity checks code generation by performing it once, parsing the result,
73 * then generating code from the second parse tree to verify that it matches
74 * the code generated from the first parse tree.
75 *
76 * @return The regenerated parse tree. Null on error.
77 */
78 private Node sanityCheckCodeGeneration(Node root) {
79 if (compiler.hasHaltingErrors()) {
80 // Don't even bother checking code generation if we already know the
81 // the code is bad.
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 // Throw an exception, so that the infrastructure will tell us
92 // which pass violated the sanity check.
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 // Throw an exception, so that the infrastructure will tell us
101 // which pass violated the sanity check.
102 throw new IllegalStateException("Sanity Check failed");
103 }
104
105 return root2;
106 }
107
108 /**
109 * Sanity checks the AST. This is by verifying the normalization passes do
110 * nothing.
111 */
112 private void sanityCheckNormalization(Node externs, Node root) {
113 // Verify nothing has inappropriately denormalize the AST.
114 CodeChangeHandler handler = new ForbiddenChange();
115 compiler.addChangeHandler(handler);
116
117 // TODO(johnlenz): Change these normalization checks Preconditions and
118 // Exceptions into Errors so that it is easier to find the root cause
119 // when there are cascading issues.
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 }