Debian Codegraph @

test/com/google/javascript/jscomp/LiveVariableAnalysisTest.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.DataFlowAnalysis.FlowState;
20 import com.google.javascript.jscomp.Scope.Var;
21 import com.google.javascript.rhino.InputId;
22 import com.google.javascript.rhino.Node;
23 import com.google.javascript.rhino.Token;
24
25 import junit.framework.TestCase;
26
27 /**
28 * Tests for {@link LiveVariablesAnalysis}. Test cases are snippets of a
29 * function and assertions are made at the instruction labeled with {@code X}.
30 *
31 */
32 public class LiveVariableAnalysisTest extends TestCase {
33
34 private LiveVariablesAnalysis liveness = null;
35
36 public void testStraightLine() {
37 // A sample of simple straight line of code with different liveness changes.
38 assertNotLiveBeforeX("X:var a;", "a");
39 assertNotLiveAfterX("X:var a;", "a");
40 assertNotLiveAfterX("X:var a=1;", "a");
41 assertLiveAfterX("X:var a=1; a()", "a");
42 assertNotLiveBeforeX("X:var a=1; a()", "a");
43 assertLiveBeforeX("var a;X:a;", "a");
44 assertLiveBeforeX("var a;X:a=a+1;", "a");
45 assertLiveBeforeX("var a;X:a+=1;", "a");
46 assertLiveBeforeX("var a;X:a++;", "a");
47 assertNotLiveAfterX("var a,b;X:b();", "a");
48 assertNotLiveBeforeX("var a,b;X:b();", "a");
49 assertLiveBeforeX("var a,b;X:b(a);", "a");
50 assertLiveBeforeX("var a,b;X:b(1,2,3,b(a + 1));", "a");
51 assertNotLiveBeforeX("var a,b;X:a=1;b(a)", "a");
52 assertNotLiveAfterX("var a,b;X:b(a);b()", "a");
53 assertLiveBeforeX("var a,b;X:b();b=1;a()", "b");
54 assertLiveAfterX("X:a();var a;a()", "a");
55 assertNotLiveAfterX("X:a();var a=1;a()", "a");
56 assertLiveBeforeX("var a,b;X:a,b=1", "a");
57 }
58
59 public void testProperties() {
60 // Reading property of a local variable makes that variable live.
61 assertLiveBeforeX("var a,b;X:a.P;", "a");
62
63 // Assigning to a property doesn't kill "a". It makes it live instead.
64 assertLiveBeforeX("var a,b;X:a.P=1;b()", "a");
65 assertLiveBeforeX("var a,b;X:a.P.Q=1;b()", "a");
66
67 // An "a" in a different context.
68 assertNotLiveAfterX("var a,b;X:b.P.Q.a=1;", "a");
69
70 assertLiveBeforeX("var a,b;X:b.P.Q=a;", "a");
71 }
72
73 public void testConditions() {
74 // Reading the condition makes the variable live.
75 assertLiveBeforeX("var a,b;X:if(a){}", "a");
76 assertLiveBeforeX("var a,b;X:if(a||b) {}", "a");
77 assertLiveBeforeX("var a,b;X:if(b||a) {}", "a");
78 assertLiveBeforeX("var a,b;X:if(b||b(a)) {}", "a");
79 assertNotLiveAfterX("var a,b;X:b();if(a) {}", "b");
80
81 // We can kill within a condition as well.
82 assertNotLiveAfterX("var a,b;X:a();if(a=b){}a()", "a");
83 assertNotLiveAfterX("var a,b;X:a();while(a=b){}a()", "a");
84
85 // The kill can be "conditional" due to short circuit.
86 assertNotLiveAfterX("var a,b;X:a();if((a=b)&&b){}a()", "a");
87 assertNotLiveAfterX("var a,b;X:a();while((a=b)&&b){}a()", "a");
88 assertLiveBeforeX("var a,b;a();X:if(b&&(a=b)){}a()", "a"); // Assumed live.
89 assertLiveBeforeX("var a,b;a();X:if(a&&(a=b)){}a()", "a");
90 assertLiveBeforeX("var a,b;a();X:while(b&&(a=b)){}a()", "a");
91 assertLiveBeforeX("var a,b;a();X:while(a&&(a=b)){}a()", "a");
92 }
93
94 public void testArrays() {
95 assertLiveBeforeX("var a;X:a[1]", "a");
96 assertLiveBeforeX("var a,b;X:b[a]", "a");
97 assertLiveBeforeX("var a,b;X:b[1,2,3,4,b(a)]", "a");
98 assertLiveBeforeX("var a,b;X:b=[a,'a']", "a");
99 assertNotLiveBeforeX("var a,b;X:a=[];b(a)", "a");
100
101 // Element assignment doesn't kill the array.
102 assertLiveBeforeX("var a;X:a[1]=1", "a");
103 }
104
105 public void testTwoPaths() {
106 // Both Paths.
107 assertLiveBeforeX("var a,b;X:if(b){b(a)}else{b(a)};", "a");
108
109 // Only one path.
110 assertLiveBeforeX("var a,b;X:if(b){b(b)}else{b(a)};", "a");
111 assertLiveBeforeX("var a,b;X:if(b){b(a)}else{b(b)};", "a");
112
113 // None of the paths.
114 assertNotLiveAfterX("var a,b;X:if(b){b(b)}else{b(b)};", "a");
115
116 // At the very end.
117 assertLiveBeforeX("var a,b;X:if(b){b(b)}else{b(b)}a();", "a");
118
119 // The loop might or might not be executed.
120 assertLiveBeforeX("var a;X:while(param1){a()};", "a");
121 assertLiveBeforeX("var a;X:while(param1){a=1};a()", "a");
122
123 // Same idea with if.
124 assertLiveBeforeX("var a;X:if(param1){a()};", "a");
125 assertLiveBeforeX("var a;X:if(param1){a=1};a()", "a");
126
127 // This is different in DO. We know for sure at least one iteration is
128 // executed.
129 assertNotLiveAfterX("X:var a;do{a=1}while(param1);a()", "a");
130 }
131
132 public void testThreePaths() {
133 assertLiveBeforeX("var a;X:if(1){}else if(2){}else{a()};", "a");
134 assertLiveBeforeX("var a;X:if(1){}else if(2){a()}else{};", "a");
135 assertLiveBeforeX("var a;X:if(1){a()}else if(2){}else{};", "a");
136 assertLiveBeforeX("var a;X:if(1){}else if(2){}else{};a()", "a");
137 }
138
139 public void testHooks() {
140 assertLiveBeforeX("var a;X:1?a=1:1;a()", "a");
141
142 // Unfortunately, we cannot prove the following because we assume there is
143 // no control flow within a hook (i.e. no joins / set unions).
144 // assertNotLiveAfterX("var a;X:1?a=1:a=2;a", "a");
145 assertLiveBeforeX("var a,b;X:b=1?a:2", "a");
146 }
147
148 public void testForLoops() {
149 // Induction variable should not be live after the loop.
150 assertNotLiveBeforeX("var a,b;for(a=0;a<9;a++){b(a)};X:b", "a");
151 assertNotLiveBeforeX("var a,b;for(a in b){a()};X:b", "a");
152 assertNotLiveBeforeX("var a,b;for(a in b){a()};X:a", "b");
153 assertLiveBeforeX("var b;for(var a in b){X:a()};", "a");
154
155 // It should be live within the loop even if it is not used.
156 assertLiveBeforeX("var a,b;for(a=0;a<9;a++){X:1}", "a");
157 assertLiveAfterX("var a,b;for(a in b){X:b};", "a");
158 // For-In should serve as a gen as well.
159 assertLiveBeforeX("var a,b; X:for(a in b){ }", "a");
160
161 // "a in b" should kill "a" before it.
162 // Can't prove this unless we have branched backward DFA.
163 //assertNotLiveAfterX("var a,b;X:b;for(a in b){a()};", "a");
164
165 // Unless it is used before.
166 assertLiveBeforeX("var a,b;X:a();b();for(a in b){a()};", "a");
167
168 // Initializer
169 assertLiveBeforeX("var a,b;X:b;for(b=a;;){};", "a");
170 assertNotLiveBeforeX("var a,b;X:a;for(b=a;;){b()};b();", "b");
171 }
172
173 public void testNestedLoops() {
174 assertLiveBeforeX("var a;X:while(1){while(1){a()}}", "a");
175 assertLiveBeforeX("var a;X:while(1){while(1){while(1){a()}}}", "a");
176 assertLiveBeforeX("var a;X:while(1){while(1){a()};a=1}", "a");
177 assertLiveAfterX("var a;while(1){while(1){a()};X:a=1;}", "a");
178 assertLiveAfterX("var a;while(1){X:a=1;while(1){a()}}", "a");
179 assertNotLiveBeforeX(
180 "var a;X:1;do{do{do{a=1;}while(1)}while(1)}while(1);a()", "a");
181 }
182
183 public void testSwitches() {
184 assertLiveBeforeX("var a,b;X:switch(a){}", "a");
185 assertLiveBeforeX("var a,b;X:switch(b){case(a):break;}", "a");
186 assertLiveBeforeX("var a,b;X:switch(b){case(b):case(a):break;}", "a");
187 assertNotLiveBeforeX(
188 "var a,b;X:switch(b){case 1:a=1;break;default:a=2;break};a()", "a");
189
190 assertLiveBeforeX("var a,b;X:switch(b){default:a();break;}", "a");
191 }
192
193 public void testAssignAndReadInCondition() {
194 // BUG #1358904
195 // Technically, this isn't exactly true....but we haven't model control flow
196 // within an instruction.
197 assertLiveBeforeX("var a, b; X: if ((a = this) && (b = a)) {}", "a");
198 assertNotLiveBeforeX("var a, b; X: a = 1, b = 1;", "a");
199 assertNotLiveBeforeX("var a; X: a = 1, a = 1;", "a");
200 }
201
202 public void testParam() {
203 // Unused parameter should not be live.
204 assertNotLiveAfterX("var a;X:a()", "param1");
205 assertLiveBeforeX("var a;X:a(param1)", "param1");
206 assertNotLiveAfterX("var a;X:a();a(param2)", "param1");
207 }
208
209 public void testExpressionInForIn() {
210 assertLiveBeforeX("var a = [0]; X:for (a[1] in foo) { }", "a");
211 }
212
213 public void testArgumentsArray() {
214 // Check that use of arguments forces the parameters into the
215 // escaped set.
216 assertEscaped("arguments[0]", "param1");
217 assertEscaped("arguments[0]", "param2");
218 assertEscaped("var args = arguments", "param1");
219 assertEscaped("var args = arguments", "param2");
220 assertNotEscaped("arguments = []", "param1");
221 assertNotEscaped("arguments = []", "param2");
222 assertEscaped("arguments[0] = 1", "param1");
223 assertEscaped("arguments[0] = 1", "param2");
224 assertEscaped("arguments[arguments[0]] = 1", "param1");
225 assertEscaped("arguments[arguments[0]] = 1", "param2");
226 }
227
228 public void testTryCatchFinally() {
229 assertLiveAfterX("var a; try {X:a=1} finally {a}", "a");
230 assertLiveAfterX("var a; try {a()} catch(e) {X:a=1} finally {a}", "a");
231 // Because the outer catch doesn't catch any exceptions at all, the read of
232 // "a" within the catch block should not make "a" live.
233 assertNotLiveAfterX("var a = 1; try {" +
234 "try {a()} catch(e) {X:1} } catch(E) {a}", "a");
235 assertLiveAfterX("var a; while(1) { try {X:a=1;break} finally {a}}", "a");
236 }
237
238 public void testForInAssignment() {
239 assertLiveBeforeX("var a,b; for (var y in a = b) { X:a[y] }", "a");
240 // No one refers to b after the first iteration.
241 assertNotLiveBeforeX("var a,b; for (var y in a = b) { X:a[y] }", "b");
242 assertLiveBeforeX("var a,b; for (var y in a = b) { X:a[y] }", "y");
243 assertLiveAfterX("var a,b; for (var y in a = b) { a[y]; X: y();}", "a");
244 }
245
246 public void testExceptionThrowingAssignments() {
247 assertLiveBeforeX("try{var a; X:a=foo();a} catch(e) {e()}", "a");
248 assertLiveBeforeX("try{X:var a=foo();a} catch(e) {e()}", "a");
249 assertLiveBeforeX("try{X:var a=foo()} catch(e) {e(a)}", "a");
250 }
251
252 public void testInnerFunctions() {
253 assertLiveBeforeX("function a() {}; X: a()", "a");
254 assertNotLiveBeforeX("X: function a() {}", "a");
255 assertLiveBeforeX("a = function(){}; function a() {}; X: a()", "a");
256 // NOTE: function a() {} has no CFG node representation since it is not
257 // part of the control execution.
258 assertLiveAfterX("X: a = function(){}; function a() {}; a()", "a");
259 assertNotLiveBeforeX("X: a = function(){}; function a() {}; a()", "a");
260 }
261
262 public void testEscaped() {
263 assertEscaped("var a;function b(){a()}", "a");
264 assertEscaped("var a;function b(){param1()}", "param1");
265 assertEscaped("var a;function b(){function c(){a()}}", "a");
266 assertEscaped("var a;function b(){param1.x = function() {a()}}", "a");
267 assertEscaped("try{} catch(e){}", "e");
268 assertNotEscaped("var a;function b(){var c; c()}", "c");
269 assertNotEscaped("var a;function f(){function b(){var c;c()}}", "c");
270 assertNotEscaped("var a;function b(){};a()", "a");
271 assertNotEscaped("var a;function f(){function b(){}}a()", "a");
272 assertNotEscaped("var a;function b(){var a;a()};a()", "a");
273
274 // Escaped by exporting.
275 assertEscaped("var _x", "_x");
276 }
277
278 public void testEscapedLiveness() {
279 assertNotLiveBeforeX("var a;X:a();function b(){a()}", "a");
280 }
281
282 public void testBug1449316() {
283 assertLiveBeforeX("try {var x=[]; X:var y=x[0]} finally {foo()}", "x");
284 }
285
286 private void assertLiveBeforeX(String src, String var) {
287 FlowState<LiveVariablesAnalysis.LiveVariableLattice> state =
288 getFlowStateAtX(src);
289 assertNotNull(src + " should contain a label 'X:'", state);
290 assertTrue("Variable" + var + " should be live before X", state.getIn()
291 .isLive(liveness.getVarIndex(var)));
292 }
293
294 private void assertLiveAfterX(String src, String var) {
295 FlowState<LiveVariablesAnalysis.LiveVariableLattice> state =
296 getFlowStateAtX(src);
297 assertTrue("Label X should be in the input program.", state != null);
298 assertTrue("Variable" + var + " should be live after X", state.getOut()
299 .isLive(liveness.getVarIndex(var)));
300 }
301
302 private void assertNotLiveAfterX(String src, String var) {
303 FlowState<LiveVariablesAnalysis.LiveVariableLattice> state =
304 getFlowStateAtX(src);
305 assertTrue("Label X should be in the input program.", state != null);
306 assertTrue("Variable" + var + " should not be live after X", !state
307 .getOut().isLive(liveness.getVarIndex(var)));
308 }
309
310 private void assertNotLiveBeforeX(String src, String var) {
311 FlowState<LiveVariablesAnalysis.LiveVariableLattice> state =
312 getFlowStateAtX(src);
313 assertTrue("Label X should be in the input program.", state != null);
314 assertTrue("Variable" + var + " should not be live before X", !state
315 .getIn().isLive(liveness.getVarIndex(var)));
316 }
317
318 private FlowState<LiveVariablesAnalysis.LiveVariableLattice> getFlowStateAtX(
319 String src) {
320 liveness = computeLiveness(src);
321 return getFlowStateAtX(liveness.getCfg().getEntry().getValue(), liveness
322 .getCfg());
323 }
324
325 private FlowState<LiveVariablesAnalysis.LiveVariableLattice> getFlowStateAtX(
326 Node node, ControlFlowGraph<Node> cfg) {
327 if (node.isLabel()) {
328 if (node.getFirstChild().getString().equals("X")) {
329 return cfg.getNode(node.getLastChild()).getAnnotation();
330 }
331 }
332 for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
333 FlowState<LiveVariablesAnalysis.LiveVariableLattice> state =
334 getFlowStateAtX(c, cfg);
335 if (state != null) {
336 return state;
337 }
338 }
339 return null;
340 }
341
342 private static void assertEscaped(String src, String name) {
343 for (Var var : computeLiveness(src).getEscapedLocals()) {
344 if (var.name.equals(name)) {
345 return;
346 }
347 }
348 fail("Variable " + name + " should be in the escaped local list.");
349 }
350
351 private static void assertNotEscaped(String src, String name) {
352 for (Var var : computeLiveness(src).getEscapedLocals()) {
353 assertFalse(var.name.equals(name));
354 }
355 }
356
357 private static LiveVariablesAnalysis computeLiveness(String src) {
358 Compiler compiler = new Compiler();
359 CompilerOptions options = new CompilerOptions();
360 options.setCodingConvention(new GoogleCodingConvention());
361 compiler.initOptions(options);
362 src = "function _FUNCTION(param1, param2){" + src + "}";
363 Node n = compiler.parseTestCode(src).removeFirstChild();
364 Node script = new Node(Token.SCRIPT, n);
365 script.setInputId(new InputId("test"));
366 assertEquals(0, compiler.getErrorCount());
367 Scope scope = new SyntacticScopeCreator(compiler).createScope(
368 n, Scope.createGlobalScope(script));
369 ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, false, true);
370 cfa.process(null, n);
371 ControlFlowGraph<Node> cfg = cfa.getCfg();
372 LiveVariablesAnalysis analysis =
373 new LiveVariablesAnalysis(cfg, scope, compiler);
374 analysis.analyze();
375 return analysis;
376 }
377 }