Debian Codegraph @

test/com/google/javascript/jscomp/FlowSensitiveInlineVariablesTest.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 * Unit tests for {@link FlowSensitiveInlineVariables}.
23 *
24 */
25 public class FlowSensitiveInlineVariablesTest extends CompilerTestCase {
26
27 public static final String EXTERN_FUNCTIONS = "" +
28 "var print;\n" +
29 "/** @nosideeffects */ function noSFX() {} \n" +
30 " function hasSFX() {} \n";
31
32 public FlowSensitiveInlineVariablesTest() {
33 enableNormalize(true);
34 }
35
36 @Override
37 public int getNumRepetitions() {
38 // Test repeatedly inline.
39 return 3;
40 }
41
42 @Override
43 protected CompilerPass getProcessor(final Compiler compiler) {
44 //return new FlowSensitiveInlineVariables(compiler);
45 return new CompilerPass() {
46 @Override
47 public void process(Node externs, Node root) {
48 (new MarkNoSideEffectCalls(compiler)).process(externs, root);
49 (new FlowSensitiveInlineVariables(compiler)).process(externs, root);
50 }
51 };
52 }
53
54 public void testSimpleAssign() {
55 inline("var x; x = 1; print(x)", "var x; print(1)");
56 inline("var x; x = 1; x", "var x; 1");
57 inline("var x; x = 1; var a = x", "var x; var a = 1");
58 inline("var x; x = 1; x = x + 1", "var x; x = 1 + 1");
59 }
60
61 public void testSimpleVar() {
62 inline("var x = 1; print(x)", "var x; print(1)");
63 inline("var x = 1; x", "var x; 1");
64 inline("var x = 1; var a = x", "var x; var a = 1");
65 inline("var x = 1; x = x + 1", "var x; x = 1 + 1");
66 }
67
68 public void testSimpleForIn() {
69 inline("var a,b,x = a in b; x",
70 "var a,b,x; a in b");
71 noInline("var a, b; var x = a in b; print(1); x");
72 noInline("var a,b,x = a in b; delete a[b]; x");
73 }
74
75 public void testExported() {
76 noInline("var _x = 1; print(_x)");
77 }
78
79 public void testDoNotInlineIncrement() {
80 noInline("var x = 1; x++;");
81 noInline("var x = 1; x--;");
82 }
83
84 public void testDoNotInlineAssignmentOp() {
85 noInline("var x = 1; x += 1;");
86 noInline("var x = 1; x -= 1;");
87 }
88
89 public void testDoNotInlineIntoLhsOfAssign() {
90 noInline("var x = 1; x += 3;");
91 }
92
93 public void testMultiUse() {
94 noInline("var x; x = 1; print(x); print (x);");
95 }
96
97 public void testMultiUseInSameCfgNode() {
98 noInline("var x; x = 1; print(x) || print (x);");
99 }
100
101 public void testMultiUseInTwoDifferentPath() {
102 noInline("var x = 1; if (print) { print(x) } else { alert(x) }");
103 }
104
105 public void testAssignmentBeforeDefinition() {
106 inline("x = 1; var x = 0; print(x)","x = 1; var x; print(0)" );
107 }
108
109 public void testVarInConditionPath() {
110 noInline("if (foo) { var x = 0 } print(x)");
111 }
112
113 public void testMultiDefinitionsBeforeUse() {
114 inline("var x = 0; x = 1; print(x)", "var x = 0; print(1)");
115 }
116
117 public void testMultiDefinitionsInSameCfgNode() {
118 noInline("var x; (x = 1) || (x = 2); print(x)");
119 noInline("var x; x = (1 || (x = 2)); print(x)");
120 noInline("var x;(x = 1) && (x = 2); print(x)");
121 noInline("var x;x = (1 && (x = 2)); print(x)");
122 noInline("var x; x = 1 , x = 2; print(x)");
123 }
124
125 public void testNotReachingDefinitions() {
126 noInline("var x; if (foo) { x = 0 } print (x)");
127 }
128
129 public void testNoInlineLoopCarriedDefinition() {
130 // First print is undefined instead.
131 noInline("var x; while(true) { print(x); x = 1; }");
132
133 // Prints 0 1 1 1 1....
134 noInline("var x = 0; while(true) { print(x); x = 1; }");
135 }
136
137 public void testDoNotExitLoop() {
138 noInline("while (z) { var x = 3; } var y = x;");
139 }
140
141 public void testDoNotInlineWithinLoop() {
142 noInline("var y = noSFX(); do { var z = y.foo(); } while (true);");
143 }
144
145 public void testDoNotInlineCatchExpression1() {
146 noInline(
147 "var a;\n" +
148 "try {\n" +
149 " throw Error(\"\");\n" +
150 "}catch(err) {" +
151 " a = err;\n" +
152 "}\n" +
153 "return a.stack\n");
154 }
155
156 public void testDoNotInlineCatchExpression1a() {
157 noInline(
158 "var a;\n" +
159 "try {\n" +
160 " throw Error(\"\");\n" +
161 "}catch(err) {" +
162 " a = err + 1;\n" +
163 "}\n" +
164 "return a.stack\n");
165 }
166
167 public void testDoNotInlineCatchExpression2() {
168 noInline(
169 "var a;\n" +
170 "try {\n" +
171 " if (x) {throw Error(\"\");}\n" +
172 "}catch(err) {" +
173 " a = err;\n" +
174 "}\n" +
175 "return a.stack\n");
176 }
177
178 public void testDoNotInlineCatchExpression3() {
179 noInline(
180 "var a;\n" +
181 "try {\n" +
182 " throw Error(\"\");\n" +
183 "} catch(err) {" +
184 " err = x;\n" +
185 " a = err;\n" +
186 "}\n" +
187 "return a.stack\n");
188 }
189
190 public void testDoNotInlineCatchExpression4() {
191 // Note: it is valid to inline "x" here but we currently don't.
192 noInline(
193 "try {\n" +
194 " stuff();\n" +
195 "} catch (e) {\n" +
196 " x = e;\n" +
197 " print(x);\n" +
198 "}");
199 }
200
201 public void testDefinitionAfterUse() {
202 inline("var x = 0; print(x); x = 1", "var x; print(0); x = 1");
203 }
204
205 public void testInlineSameVariableInStraightLine() {
206 inline("var x; x = 1; print(x); x = 2; print(x)",
207 "var x; print(1); print(2)");
208 }
209
210 public void testInlineInDifferentPaths() {
211 inline("var x; if (print) {x = 1; print(x)} else {x = 2; print(x)}",
212 "var x; if (print) {print(1)} else {print(2)}");
213 }
214
215 public void testNoInlineInMergedPath() {
216 noInline(
217 "var x,y;x = 1;while(y) { if(y){ print(x) } else { x = 1 } } print(x)");
218 }
219
220 public void testInlineIntoExpressions() {
221 inline("var x = 1; print(x + 1);", "var x; print(1 + 1)");
222 }
223
224 public void testInlineExpressions1() {
225 inline("var a, b; var x = a+b; print(x)", "var a, b; var x; print(a+b)");
226 }
227
228 public void testInlineExpressions2() {
229 // We can't inline because of the redefinition of "a".
230 noInline("var a, b; var x = a + b; a = 1; print(x)");
231 }
232
233 public void testInlineExpressions3() {
234 inline("var a,b,x; x=a+b; x=a-b ; print(x)",
235 "var a,b,x; x=a+b; print(a-b)");
236 }
237
238 public void testInlineExpressions4() {
239 // Precision is lost due to comma's.
240 noInline("var a,b,x; x=a+b, x=a-b; print(x)");
241 }
242
243 public void testInlineExpressions5() {
244 noInline("var a; var x = a = 1; print(x)");
245 }
246
247 public void testInlineExpressions6() {
248 noInline("var a, x; a = 1 + (x = 1); print(x)");
249 }
250
251 public void testInlineExpression7() {
252 // Possible side effects in foo() that might conflict with bar();
253 noInline("var x = foo() + 1; bar(); print(x)");
254
255 // This is a possible case but we don't have analysis to prove this yet.
256 // TODO(user): It is possible to cover this case with the same algorithm
257 // as the missing return check.
258 noInline("var x = foo() + 1; print(x)");
259 }
260
261 public void testInlineExpression8() {
262 // The same variable inlined twice.
263 inline(
264 "var a,b;" +
265 "var x = a + b; print(x); x = a - b; print(x)",
266 "var a,b;" +
267 "var x; print(a + b); print(a - b)");
268 }
269
270 public void testInlineExpression9() {
271 // Check for actual control flow sensitivity.
272 inline(
273 "var a,b;" +
274 "var x; if (g) { x= a + b; print(x) } x = a - b; print(x)",
275 "var a,b;" +
276 "var x; if (g) { print(a + b)} print(a - b)");
277 }
278
279 public void testInlineExpression10() {
280 // The DFA is not fine grain enough for this.
281 noInline("var x, y; x = ((y = 1), print(y))");
282 }
283
284 public void testInlineExpressions11() {
285 inline("var x; x = x + 1; print(x)", "var x; print(x + 1)");
286 noInline("var x; x = x + 1; print(x); print(x)");
287 }
288
289 public void testInlineExpressions12() {
290 // ++ is an assignment and considered to modify state so it will not be
291 // inlined.
292 noInline("var x = 10; x = c++; print(x)");
293 }
294
295 public void testInlineExpressions13() {
296 inline("var a = 1, b = 2;" +
297 "var x = a;" +
298 "var y = b;" +
299 "var z = x + y;" +
300 "var i = z;" +
301 "var j = z + y;" +
302 "var k = i;",
303
304 "var a, b;" +
305 "var x;" +
306 "var y = 2;" +
307 "var z = 1 + y;" +
308 "var i;" +
309 "var j = z + y;" +
310 "var k = z;");
311 }
312
313 public void testNoInlineIfDefinitionMayNotReach() {
314 noInline("var x; if (x=1) {} x;");
315 }
316
317 public void testNoInlineEscapedToInnerFunction() {
318 noInline("var x = 1; function foo() { x = 2 }; print(x)");
319 }
320
321 public void testNoInlineLValue() {
322 noInline("var x; if (x = 1) { print(x) }");
323 }
324
325 public void testSwitchCase() {
326 inline("var x = 1; switch(x) { }", "var x; switch(1) { }");
327 }
328
329 public void testShadowedVariableInnerFunction() {
330 inline("var x = 1; print(x) || (function() { var x; x = 1; print(x)})()",
331 "var x; print(1) || (function() { var x; print(1)})()");
332 }
333
334 public void testCatch() {
335 noInline("var x = 0; try { } catch (x) { }");
336 noInline("try { } catch (x) { print(x) }");
337 }
338
339 public void testNoInlineGetProp() {
340 // We don't know if j alias a.b
341 noInline("var x = a.b.c; j.c = 1; print(x);");
342 }
343
344 public void testNoInlineGetProp2() {
345 noInline("var x = 1 * a.b.c; j.c = 1; print(x);");
346 }
347
348 public void testNoInlineGetProp3() {
349 // Anything inside a function is fine.
350 inline("var x = function(){1 * a.b.c}; print(x);",
351 "var x; print(function(){1 * a.b.c});");
352 }
353
354 public void testNoInlineGetEle() {
355 // Again we don't know if i = j
356 noInline("var x = a[i]; a[j] = 2; print(x); ");
357 }
358
359 // TODO(user): These should be inlinable.
360 public void testNoInlineConstructors() {
361 noInline("var x = new Iterator(); x.next();");
362 }
363
364 // TODO(user): These should be inlinable.
365 public void testNoInlineArrayLits() {
366 noInline("var x = []; print(x)");
367 }
368
369 // TODO(user): These should be inlinable.
370 public void testNoInlineObjectLits() {
371 noInline("var x = {}; print(x)");
372 }
373
374 // TODO(user): These should be inlinable after the REGEX checks.
375 public void testNoInlineRegExpLits() {
376 noInline("var x = /y/; print(x)");
377 }
378
379 public void testInlineConstructorCallsIntoLoop() {
380 // Don't inline construction into loops.
381 noInline("var x = new Iterator();" +
382 "for(i = 0; i < 10; i++) {j = x.next()}");
383 }
384
385 public void testRemoveWithLabels() {
386 inline("var x = 1; L: x = 2; print(x)", "var x = 1; L:{} print(2)");
387 inline("var x = 1; L: M: x = 2; print(x)", "var x = 1; L:M:{} print(2)");
388 inline("var x = 1; L: M: N: x = 2; print(x)",
389 "var x = 1; L:M:N:{} print(2)");
390 }
391
392 public void testInlineAcrossSideEffect1() {
393 // This can't be inlined because print() has side-effects and might change
394 // the definition of noSFX.
395 //
396 // noSFX must be both const and pure in order to inline it.
397 noInline("var y; var x = noSFX(y); print(x)");
398 //inline("var y; var x = noSFX(y); print(x)", "var y;var x;print(noSFX(y))");
399 }
400
401 public void testInlineAcrossSideEffect2() {
402 // Think noSFX() as a function that reads y.foo and return it
403 // and SFX() write some new value of y.foo. If that's the case,
404 // inlining across hasSFX() is not valid.
405
406 // This is a case where hasSFX is right of the source of the inlining.
407 noInline("var y; var x = noSFX(y), z = hasSFX(y); print(x)");
408 noInline("var y; var x = noSFX(y), z = new hasSFX(y); print(x)");
409 noInline("var y; var x = new noSFX(y), z = new hasSFX(y); print(x)");
410 }
411
412 public void testInlineAcrossSideEffect3() {
413 // This is a case where hasSFX is left of the destination of the inlining.
414 noInline("var y; var x = noSFX(y); hasSFX(y), print(x)");
415 noInline("var y; var x = noSFX(y); new hasSFX(y), print(x)");
416 noInline("var y; var x = new noSFX(y); new hasSFX(y), print(x)");
417 }
418
419 public void testInlineAcrossSideEffect4() {
420 // This is a case where hasSFX is some control flow path between the
421 // source and its destination.
422 noInline("var y; var x = noSFX(y); hasSFX(y); print(x)");
423 noInline("var y; var x = noSFX(y); new hasSFX(y); print(x)");
424 noInline("var y; var x = new noSFX(y); new hasSFX(y); print(x)");
425 }
426
427 public void testCanInlineAcrossNoSideEffect() {
428 // This can't be inlined because print() has side-effects and might change
429 // the definition of noSFX. We should be able to mark noSFX as const
430 // in some way.
431 noInline(
432 "var y; var x = noSFX(y), z = noSFX(); noSFX(); noSFX(), print(x)");
433 //inline(
434 // "var y; var x = noSFX(y), z = noSFX(); noSFX(); noSFX(), print(x)",
435 // "var y; var x, z = noSFX(); noSFX(); noSFX(), print(noSFX(y))");
436 }
437
438 public void testDependOnOuterScopeVariables() {
439 noInline("var x; function foo() { var y = x; x = 0; print(y) }");
440 noInline("var x; function foo() { var y = x; x++; print(y) }");
441
442 // Sadly, we don't understand the data flow of outer scoped variables as
443 // it can be modified by code outside of this scope. We can't inline
444 // at all if the definition has dependence on such variable.
445 noInline("var x; function foo() { var y = x; print(y) }");
446 }
447
448 public void testInlineIfNameIsLeftSideOfAssign() {
449 inline("var x = 1; x = print(x) + 1", "var x; x = print(1) + 1");
450 inline("var x = 1; L: x = x + 2", "var x; L: x = 1 + 2");
451 inline("var x = 1; x = (x = x + 1)", "var x; x = (x = 1 + 1)");
452
453 noInline("var x = 1; x = (x = (x = 10) + x)");
454 noInline("var x = 1; x = (f(x) + (x = 10) + x);");
455 noInline("var x = 1; x=-1,foo(x)");
456 noInline("var x = 1; x-=1,foo(x)");
457 }
458
459 public void testInlineArguments() {
460 testSame("function _func(x) { print(x) }");
461 testSame("function _func(x,y) { if(y) { x = 1 }; print(x) }");
462
463 test("function f(x, y) { x = 1; print(x) }",
464 "function f(x, y) { print(1) }");
465
466 test("function f(x, y) { if (y) { x = 1; print(x) }}",
467 "function f(x, y) { if (y) { print(1) }}");
468 }
469
470 public void testInvalidInlineArguments1() {
471 testSame("function f(x, y) { x = 1; arguments[0] = 2; print(x) }");
472 testSame("function f(x, y) { x = 1; var z = arguments;" +
473 "z[0] = 2; z[1] = 3; print(x)}");
474 testSame("function g(a){a[0]=2} function f(x){x=1;g(arguments);print(x)}");
475 }
476
477 public void testInvalidInlineArguments2() {
478 testSame("function f(c) {var f = c; arguments[0] = this;" +
479 "f.apply(this, arguments); return this;}");
480 }
481
482 public void testForIn() {
483 noInline("var x; var y = {}; for(x in y){}");
484 noInline("var x; var y = {}; var z; for(x in z = y){print(z)}");
485 noInline("var x; var y = {}; var z; for(x in y){print(z)}");
486
487 }
488
489 public void testNotOkToSkipCheckPathBetweenNodes() {
490 noInline("var x; for(x = 1; foo(x);) {}");
491 noInline("var x; for(; x = 1;foo(x)) {}");
492 }
493
494 public void testIssue698() {
495 // Most of the flow algorithms operate on Vars. We want to make
496 // sure the algorithm bails out appropriately if it sees
497 // a var that it doesn't know about.
498 inline(
499 "var x = ''; "
500 + "unknown.length < 2 && (unknown='0' + unknown);"
501 + "x = x + unknown; "
502 + "unknown.length < 3 && (unknown='0' + unknown);"
503 + "x = x + unknown; "
504 + "return x;",
505 "var x; "
506 + "unknown.length < 2 && (unknown='0' + unknown);"
507 + "x = '' + unknown; "
508 + "unknown.length < 3 && (unknown='0' + unknown);"
509 + "x = x + unknown; "
510 + "return x;");
511 }
512
513 public void testIssue777() {
514 test(
515 "function f(cmd, ta) {" +
516 " var temp = cmd;" +
517 " var temp2 = temp >> 2;" +
518 " cmd = STACKTOP;" +
519 " for (var src = temp2, dest = cmd >> 2, stop = src + 37;" +
520 " src < stop;" +
521 " src++, dest++) {" +
522 " HEAP32[dest] = HEAP32[src];" +
523 " }" +
524 " temp = ta;" +
525 " temp2 = temp >> 2;" +
526 " ta = STACKTOP;" +
527 " STACKTOP += 8;" +
528 " HEAP32[ta >> 2] = HEAP32[temp2];" +
529 " HEAP32[ta + 4 >> 2] = HEAP32[temp2 + 1];" +
530 "}",
531 "function f(cmd, ta){" +
532 " var temp;" +
533 " var temp2 = cmd >> 2;" +
534 " cmd = STACKTOP;" +
535 " var src = temp2;" +
536 " var dest = cmd >> 2;" +
537 " var stop = src + 37;" +
538 " for(;src<stop;src++,dest++)HEAP32[dest]=HEAP32[src];" +
539 " temp2 = ta >> 2;" +
540 " ta = STACKTOP;" +
541 " STACKTOP += 8;" +
542 " HEAP32[ta>>2] = HEAP32[temp2];" +
543 " HEAP32[ta+4>>2] = HEAP32[temp2+1];" +
544 "}");
545 }
546
547 public void testTransitiveDependencies1() {
548 test(
549 "function f(x) { var a = x; var b = a; x = 3; return b; }",
550 "function f(x) { var a; var b = x; x = 3; return b; }");
551 }
552
553 public void testTransitiveDependencies2() {
554 test(
555 "function f(x) { var a = x; var b = a; var c = b; x = 3; return c; }",
556 "function f(x) { var a ; var b = x; var c ; x = 3; return b; }");
557 }
558
559 public void testIssue794a() {
560 noInline(
561 "var x = 1; " +
562 "try { x += someFunction(); } catch (e) {}" +
563 "x += 1;" +
564 "try { x += someFunction(); } catch (e) {}" +
565 "return x;");
566 }
567
568 public void testIssue794b() {
569 noInline(
570 "var x = 1; " +
571 "try { x = x + someFunction(); } catch (e) {}" +
572 "x = x + 1;" +
573 "try { x = x + someFunction(); } catch (e) {}" +
574 "return x;");
575 }
576
577 private void noInline(String input) {
578 inline(input, input);
579 }
580
581 private void inline(String input, String expected) {
582 test(EXTERN_FUNCTIONS, "function _func() {" + input + "}",
583 "function _func() {" + expected + "}", null, null);
584 }
585 }