Debian Codegraph @

test/com/google/javascript/jscomp/MustBeReachingVariableDefTest.java

raw symbols SCIP
1 /*
2 * Copyright 2008 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.jscomp.NodeTraversal.AbstractPostOrderCallback;
20 import com.google.javascript.rhino.Node;
21 import junit.framework.TestCase;
22
23 /**
24 * Tests for {@link MustBeReachingVariableDef}.
25 *
26 */
27 public class MustBeReachingVariableDefTest extends TestCase {
28
29 private MustBeReachingVariableDef defUse = null;
30 private Node def = null;
31 private Node use = null;
32
33 public static final String EXTERNS = "var goog = {}";
34
35 public void testStraightLine() {
36 assertMatch("D:var x=1; U: x");
37 assertMatch("var x; D:x=1; U: x");
38 assertNotMatch("D:var x=1; x = 2; U: x");
39 assertMatch("var x=1; D:x=2; U: x");
40 assertNotMatch("U:x; D:var x = 1");
41 assertNotMatch("D:var x; U:x; x=1");
42 assertNotMatch("D:var x; U:x; x=1; x");
43 assertMatch("D: var x = 1; var y = 2; y; U:x");
44 }
45
46 public void testIf() {
47 assertNotMatch("var x; if(a){ D:x=1 } else { x=2 }; U:x");
48 assertNotMatch("var x; if(a){ x=1 } else { D:x=2 }; U:x");
49 assertMatch("D:var x=1; if(a){ U:x } else { x };");
50 assertMatch("D:var x=1; if(a){ x } else { U:x };");
51 assertNotMatch("var x; if(a) { D: x = 1 }; U:x;");
52 }
53
54 public void testLoops() {
55 assertNotMatch("var x=0; while(a){ D:x=1 }; U:x");
56 assertNotMatch("var x=0; for(;;) { D:x=1 }; U:x");
57 assertMatch("D:var x=1; while(a) { U:x }");
58 assertMatch("D:var x=1; for(;;) { U:x }");
59 }
60
61 public void testConditional() {
62 assertMatch("var x=0,y; D:(x=1)&&y; U:x");
63 assertNotMatch("var x=0,y; D:y&&(x=1); U:x");
64 }
65
66 public void testUseAndDefInSameInstruction() {
67 assertMatch("D:var x=0; U:x=1,x");
68 assertMatch("D:var x=0; U:x,x=1");
69 }
70
71 public void testAssignmentInExpressions() {
72 assertMatch("var x=0; D:foo(bar(x=1)); U:x");
73 assertMatch("var x=0; D:foo(bar + (x = 1)); U:x");
74 }
75
76 public void testHook() {
77 assertNotMatch("var x=0; D:foo() ? x=1 : bar(); U:x");
78 assertNotMatch("var x=0; D:foo() ? x=1 : x=2; U:x");
79 }
80
81 public void testExpressionVariableReassignment() {
82 assertMatch("var a,b; D: var x = a + b; U:x");
83 assertNotMatch("var a,b,c; D: var x = a + b; a = 1; U:x");
84 assertNotMatch("var a,b,c; D: var x = a + b; f(b = 1); U:x");
85 assertMatch("var a,b,c; D: var x = a + b; c = 1; U:x");
86
87 // Even if the sub-expression is change conditionally
88 assertNotMatch("var a,b,c; D: var x = a + b; c ? a = 1 : 0; U:x");
89 }
90
91 public void testMergeDefinitions() {
92 assertNotMatch("var x,y; D: y = x + x; if(x) { x = 1 }; U:y");
93 }
94
95 public void testMergesWithOneDefinition() {
96 assertNotMatch(
97 "var x,y; while(y) { if (y) { print(x) } else { D: x = 1 } } U:x");
98 }
99
100 public void testRedefinitionUsingItself() {
101 assertMatch("var x = 1; D: x = x + 1; U:x;");
102 assertNotMatch("var x = 1; D: x = x + 1; x = 1; U:x;");
103 }
104
105 public void testMultipleDefinitionsWithDependence() {
106 assertMatch("var x, a, b; D: x = a, x = b; U: x");
107 assertMatch("var x, a, b; D: x = a, x = b; a = 1; U: x");
108 assertNotMatch("var x, a, b; D: x = a, x = b; b = 1; U: x");
109 }
110
111 public void testExterns() {
112 assertNotMatch("D: goog = {}; U: goog");
113 }
114
115 public void testAssignmentOp() {
116 assertMatch("var x = 0; D: x += 1; U: x");
117 assertMatch("var x = 0; D: x *= 1; U: x");
118 assertNotMatch("D: var x = 0; x += 1; U: x");
119 }
120
121 public void testIncAndDec() {
122 assertMatch("var x; D: x++; U: x");
123 assertMatch("var x; D: x--; U: x");
124 }
125
126 public void testFunctionParams1() {
127 computeDefUse("if (param2) { D: param1 = 1; U: param1 }");
128 assertSame(def, defUse.getDefNode("param1", use));
129 }
130
131 public void testFunctionParams2() {
132 computeDefUse("if (param2) { D: param1 = 1} U: param1");
133 assertNotSame(def, defUse.getDefNode("param1", use));
134 }
135
136 public void testArgumentsObjectModifications() {
137 computeDefUse("D: param1 = 1; arguments[0] = 2; U: param1");
138 assertNotSame(def, defUse.getDefNode("param1", use));
139 }
140
141 public void testArgumentsObjectEscaped() {
142 computeDefUse("D: param1 = 1; var x = arguments; x[0] = 2; U: param1");
143 assertNotSame(def, defUse.getDefNode("param1", use));
144 }
145
146 public void testArgumentsObjectEscapedDependents() {
147 assertNotMatch("param1=1; var x; D:x=param1; var y=arguments; U:x");
148 }
149
150 /**
151 * The use of x at U: is the definition of x at D:.
152 */
153 private void assertMatch(String src) {
154 computeDefUse(src);
155 assertSame(def, defUse.getDefNode("x", use));
156 }
157
158 /**
159 * The use of x at U: is not the definition of x at D:.
160 */
161 private void assertNotMatch(String src) {
162 computeDefUse(src);
163 assertNotSame(def, defUse.getDefNode("x", use));
164 }
165
166 /**
167 * Computes reaching definition on given source.
168 */
169 private void computeDefUse(String src) {
170 Compiler compiler = new Compiler();
171 src = "function _FUNCTION(param1, param2){" + src + "}";
172 Node root = compiler.parseTestCode(src).getFirstChild();
173 assertEquals(0, compiler.getErrorCount());
174 Scope scope = new SyntacticScopeCreator(compiler).createScope(root, null);
175 ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, false, true);
176 cfa.process(null, root);
177 ControlFlowGraph<Node> cfg = cfa.getCfg();
178 defUse = new MustBeReachingVariableDef(cfg, scope, compiler);
179 defUse.analyze();
180 def = null;
181 use = null;
182 new NodeTraversal(compiler,new LabelFinder()).traverse(root);
183 assertNotNull("Code should have an instruction labeled D", def);
184 assertNotNull("Code should have an instruction labeled U", use);
185 }
186
187 /**
188 * Finds the D: and U: label and store which node they point to.
189 */
190 private class LabelFinder extends AbstractPostOrderCallback {
191 @Override
192 public void visit(NodeTraversal t, Node n, Node parent) {
193 if (n.isLabel()) {
194 if (n.getFirstChild().getString().equals("D")) {
195 def = n.getLastChild();
196 } else if (n.getFirstChild().getString().equals("U")) {
197 use = n.getLastChild();
198 }
199 }
200 }
201 }
202 }