Debian Codegraph @

test/com/google/javascript/jscomp/MaybeReachingVariableUseTest.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.common.collect.Lists;
20 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
21 import com.google.javascript.rhino.Node;
22 import junit.framework.TestCase;
23
24 import java.util.Collection;
25 import java.util.List;
26
27 /**
28 * Tests for {@link MaybeReachingVariableUse}.
29 *
30 */
31 public class MaybeReachingVariableUseTest extends TestCase {
32
33 private MaybeReachingVariableUse useDef = null;
34 private Node def = null;
35 private List<Node> uses = null;
36
37 /*
38 * The test cases consist of a short code snippet that has an instruction
39 * labeled with D and one or more with label starting with U. When assertMatch
40 * is called, the test suite verifies that all the uses with label starting
41 * with U is reachable to the definition label at D.
42 */
43
44 public void testStraightLine() {
45 assertMatch("D:var x=1; U: x");
46 assertMatch("var x; D:x=1; U: x");
47 assertNotMatch("D:var x=1; x = 2; U: x");
48 assertMatch("var x=1; D:x=2; U: x");
49 assertNotMatch("U:x; D:var x = 1");
50 assertMatch("D: var x = 1; var y = 2; y; U:x");
51 }
52
53 public void testIf() {
54 assertMatch("var x; if(a){ D:x=1 }else { x=2 }; U:x");
55 assertMatch("var x; if(a){ x=1 }else { D:x=2 }; U:x");
56 assertMatch("D:var x=1; if(a){ U1: x }else { U2: x };");
57 }
58
59 public void testLoops() {
60 assertMatch("var x=0; while(a){ D:x=1 }; U:x");
61 assertMatch("var x=0; for(;;) { D:x=1 }; U:x");
62
63 assertMatch("D:var x=1; while(a) { U:x }");
64 assertMatch("D:var x=1; for(;;) { U:x }");
65 }
66
67 public void testConditional() {
68 assertMatch("var x=0; var y; D:(x=1)&&y; U:x");
69 assertMatch("var x=0; var y; D:y&&(x=1); U:x");
70 assertMatch("var x=0; var y=0; D:(x=1)&&(y=0); U:x");
71 assertMatch("var x=0; var y=0; D:(y=0)&&(x=1); U:x");
72 assertNotMatch("D: var x=0; var y=0; (x=1)&&(y=0); U:x");
73 assertMatch("D: var x=0; var y=0; (y=1)&&((y=2)||(x=1)); U:x");
74 assertMatch("D: var x=0; var y=0; (y=0)&&(x=1); U:x");
75 }
76
77 public void testUseAndDefInSameInstruction() {
78 assertNotMatch("D:var x=0; U:x=1,x");
79 assertMatch("D:var x=0; U:x,x=1");
80 }
81
82 public void testAssignmentInExpressions() {
83 assertMatch("var x=0; D:foo(bar(x=1)); U:x");
84 assertMatch("var x=0; D:foo(bar + (x = 1)); U:x");
85 }
86
87 public void testHook() {
88 assertMatch("var x=0; D:foo() ? x=1 : bar(); U:x");
89 assertMatch("var x=0; D:foo() ? x=1 : x=2; U:x");
90 }
91
92 public void testAssignmentOps() {
93 assertNotMatch("D: var x = 0; U: x = 100");
94 assertMatch("D: var x = 0; U: x += 100");
95 assertMatch("D: var x = 0; U: x -= 100");
96 }
97
98 public void testInc() {
99 assertMatch("D: var x = 0; U:x++");
100 assertMatch("var x = 0; D:x++; U:x");
101 }
102
103 public void testForIn() {
104 // Uses within FOR-IN header are hard to test. They are covered
105 // by the tests in the flow sensitive inliner.
106 assertNotMatch("D: var x = [], foo; U: for (x in foo) { }");
107 assertNotMatch("D: var x = [], foo; for (x in foo) { U:x }");
108 assertMatch("var x = [], foo; D: for (x in foo) { U:x }");
109 }
110
111 public void testTryCatch() {
112 assertMatch(
113 "D: var x = 1; " +
114 "try { U: var y = foo() + x; } catch (e) {} " +
115 "U: var z = x;");
116 }
117
118 /**
119 * The def of x at D: may be used by the read of x at U:.
120 */
121 private void assertMatch(String src) {
122 computeUseDef(src);
123 Collection<Node> result = useDef.getUses("x", def);
124 assertTrue(result.size() == uses.size());
125 assertTrue(result.containsAll(uses));
126 }
127
128 /**
129 * The def of x at D: is not used by the read of x at U:.
130 */
131 private void assertNotMatch(String src) {
132 computeUseDef(src);
133 assertFalse(useDef.getUses("x", def).contains(uses));
134 }
135
136 /**
137 * Computes reaching use on given source.
138 */
139 private void computeUseDef(String src) {
140 Compiler compiler = new Compiler();
141 src = "function _FUNCTION(param1, param2){" + src + "}";
142 Node n = compiler.parseTestCode(src).getFirstChild();
143 assertEquals(0, compiler.getErrorCount());
144 Scope scope = new SyntacticScopeCreator(compiler).createScope(n, null);
145 ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, false, true);
146 cfa.process(null, n);
147 ControlFlowGraph<Node> cfg = cfa.getCfg();
148 useDef = new MaybeReachingVariableUse(cfg, scope, compiler);
149 useDef.analyze();
150 def = null;
151 uses = Lists.newArrayList();
152 new NodeTraversal(compiler,new LabelFinder()).traverse(n);
153 assertNotNull("Code should have an instruction labeled D", def);
154 assertFalse("Code should have an instruction labeled starting withing U",
155 uses.isEmpty());
156 }
157
158 /**
159 * Finds the D: and U: label and store which node they point to.
160 */
161 private class LabelFinder extends AbstractPostOrderCallback {
162 @Override
163 public void visit(NodeTraversal t, Node n, Node parent) {
164 if (n.isLabel()) {
165 if (n.getFirstChild().getString().equals("D")) {
166 def = n.getLastChild();
167 } else if (n.getFirstChild().getString().startsWith("U")) {
168 uses.add(n.getLastChild());
169 }
170 }
171 }
172 }
173 }