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