Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2010 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.ImmutableList;
20 import com.google.common.collect.Lists;
21 import com.google.common.collect.Sets;
22 import com.google.javascript.rhino.Node;
23 import com.google.javascript.rhino.Token;
24
25 import java.util.List;
26 import java.util.Set;
27
28 /**
29 * Unit tests for PeepholeOptimizationsPass.
30 *
31 */
32 public class PeepholeOptimizationsPassTest extends CompilerTestCase {
33
34 private ImmutableList<AbstractPeepholeOptimization> currentPeepholePasses;
35
36 @Override
37 public void setUp() throws Exception {
38 super.setUp();
39 super.enableLineNumberCheck(true);
40 }
41
42 @Override
43 public CompilerPass getProcessor(final Compiler compiler) {
44 return new PeepholeOptimizationsPass(compiler,
45 currentPeepholePasses.toArray(
46 new AbstractPeepholeOptimization[currentPeepholePasses.size()]));
47 }
48
49 @Override
50 protected int getNumRepetitions() {
51 // Our tests do not require multiple passes to reach a fixed-point.
52 return 1;
53 }
54
55 /**
56 * PeepholeOptimizationsPass should handle the case when no peephole
57 * optimizations are turned on.
58 */
59 public void testEmptyPass() {
60 currentPeepholePasses = ImmutableList.<AbstractPeepholeOptimization>of();
61
62 testSame("var x; var y;");
63 }
64
65 public void testOptimizationOrder() {
66 /*
67 * We need to make sure that: 1) We are only traversing the AST once 2) For
68 * each node, we visit the optimizations in the client-supplied order
69 *
70 * To test this, we create two fake optimizations that each make an entry in
71 * the visitationLog when they are passed a name node to optimize.
72 *
73 * Each entry is of the form nameX where 'name' is the name of the name node
74 * visited and X is the identity of the optimization (1 or 2 in this case).
75 * After the pass is run, we verify the correct ordering by querying the
76 * log.
77 *
78 * Using a log, rather than, say, transforming nodes, allows us to ensure
79 * not only that we are visiting each node but that our visits occur in the
80 * right order (i.e. we need to make sure we're not traversing the entire
81 * AST for the first optimization and then a second time for the second).
82 */
83
84 final List<String> visitationLog = Lists.newArrayList();
85
86 AbstractPeepholeOptimization note1Applied =
87 new AbstractPeepholeOptimization() {
88 @Override
89 public Node optimizeSubtree(Node node) {
90 if (node.isName()) {
91 visitationLog.add(node.getString() + "1");
92 }
93
94 return node;
95 }
96 };
97
98 AbstractPeepholeOptimization note2Applied =
99 new AbstractPeepholeOptimization() {
100 @Override
101 public Node optimizeSubtree(Node node) {
102 if (node.isName()) {
103 visitationLog.add(node.getString() + "2");
104 }
105
106 return node;
107 }
108 };
109
110 currentPeepholePasses =
111 ImmutableList.<
112 AbstractPeepholeOptimization>of(note1Applied, note2Applied);
113
114 test("var x; var y", "var x; var y");
115
116 /*
117 * We expect the optimization order to be: "x" visited by optimization1 "x"
118 * visited by optimization2 "y" visited by optimization1 "y" visited by
119 * optimization2
120 */
121
122 assertEquals(4, visitationLog.size());
123 assertEquals("x1", visitationLog.get(0));
124 assertEquals("x2", visitationLog.get(1));
125 assertEquals("y1", visitationLog.get(2));
126 assertEquals("y2", visitationLog.get(3));
127 }
128
129 /**
130 * A peephole optimization that, given a subtree consisting of a VAR node,
131 * removes children of that node named "x".
132 */
133 private static class RemoveNodesNamedXUnderVarOptimization
134 extends AbstractPeepholeOptimization {
135 @Override
136 public Node optimizeSubtree(Node node) {
137 if (node.isVar()) {
138 Set<Node> nodesToRemove = Sets.newHashSet();
139
140 for (Node child : node.children()) {
141 if ("x".equals(child.getString())) {
142 nodesToRemove.add(child);
143 }
144 }
145
146 for (Node childToRemove : nodesToRemove) {
147 node.removeChild(childToRemove);
148 reportCodeChange();
149 }
150 }
151
152 return node;
153 }
154 }
155
156 /**
157 * A peephole optimization that, given a subtree consisting of a name node
158 * named "x" removes that node.
159 */
160 private static class RemoveNodesNamedXOptimization
161 extends AbstractPeepholeOptimization {
162 @Override
163 public Node optimizeSubtree(Node node) {
164 if (node.isName() && "x".equals(node.getString())) {
165 node.getParent().removeChild(node);
166 reportCodeChange();
167
168 return null;
169 }
170
171 return node;
172 }
173 }
174
175 /**
176 * A peephole optimization that, given a subtree consisting of a name node
177 * named "x" whose parent is a VAR node, removes the parent VAR node.
178 */
179 private static class RemoveParentVarsForNodesNamedX
180 extends AbstractPeepholeOptimization {
181 @Override
182 public Node optimizeSubtree(Node node) {
183 if (node.isName() && "x".equals(node.getString())) {
184 Node parent = node.getParent();
185 if (parent.isVar()) {
186 parent.getParent().removeChild(parent);
187 reportCodeChange();
188 return null;
189 }
190 }
191 return node;
192 }
193 }
194
195 /**
196 * A peephole optimization that, given a subtree consisting of a name node
197 * named "y", replaces it with a name node named "x";
198 */
199 private static class RenameYToX extends AbstractPeepholeOptimization {
200 @Override
201 public Node optimizeSubtree(Node node) {
202 if (node.isName() && "y".equals(node.getString())) {
203 Node replacement = Node.newString(Token.NAME, "x");
204
205 node.getParent().replaceChild(node, replacement);
206 reportCodeChange();
207
208 return replacement;
209 }
210 return node;
211 }
212 }
213
214 public void testOptimizationRemovingSubtreeChild() {
215 currentPeepholePasses = ImmutableList.<AbstractPeepholeOptimization>of(new
216 RemoveNodesNamedXUnderVarOptimization());
217
218 test("var x,y;", "var y;");
219 test("var y,x;", "var y;");
220 test("var x,y,x;", "var y;");
221 }
222
223 public void testOptimizationRemovingSubtree() {
224 currentPeepholePasses = ImmutableList.<AbstractPeepholeOptimization>of(new
225 RemoveNodesNamedXOptimization());
226
227 test("var x,y;", "var y;");
228 test("var y,x;", "var y;");
229 test("var x,y,x;", "var y;");
230 }
231
232 public void testOptimizationRemovingSubtreeParent() {
233 currentPeepholePasses = ImmutableList.<AbstractPeepholeOptimization>of(new
234 RemoveParentVarsForNodesNamedX());
235
236 test("var x; var y", "var y");
237 }
238
239 /**
240 * Test the case where the first peephole optimization removes a node and the
241 * second wants to remove (the now nonexistent) parent of that node.
242 */
243 public void testOptimizationsRemoveParentAfterRemoveChild() {
244 currentPeepholePasses = ImmutableList.<AbstractPeepholeOptimization>of(
245 new RemoveNodesNamedXOptimization(),
246 new RemoveParentVarsForNodesNamedX());
247
248 test("var x,y; var z;", "var y; var z;");
249 }
250
251 public void testOptimizationReplacingNode() {
252 currentPeepholePasses = ImmutableList.<AbstractPeepholeOptimization>of(
253 new RenameYToX(),
254 new RemoveParentVarsForNodesNamedX());
255
256 test("var y; var z;", "var z;");
257 }
258 }