| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class PeepholeIntegrationTest extends CompilerTestCase { |
| 23 |
|
| 24 |
private boolean late = true; |
| 25 |
|
| 26 |
|
| 27 |
private PeepholeIntegrationTest(boolean compareAsTree) { |
| 28 |
super("", compareAsTree); |
| 29 |
} |
| 30 |
|
| 31 |
public PeepholeIntegrationTest() { |
| 32 |
super(""); |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
public void setUp() throws Exception { |
| 37 |
super.setUp(); |
| 38 |
this.late = false; |
| 39 |
enableLineNumberCheck(true); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
disableNormalize(); |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public CompilerPass getProcessor(final Compiler compiler) { |
| 48 |
PeepholeOptimizationsPass peepholePass = |
| 49 |
new PeepholeOptimizationsPass(compiler, |
| 50 |
new PeepholeSubstituteAlternateSyntax(late), |
| 51 |
new PeepholeRemoveDeadCode(), |
| 52 |
new PeepholeFoldConstants(late) |
| 53 |
); |
| 54 |
|
| 55 |
return peepholePass; |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
protected int getNumRepetitions() { |
| 60 |
|
| 61 |
return 4; |
| 62 |
} |
| 63 |
|
| 64 |
private void foldSame(String js) { |
| 65 |
testSame(js); |
| 66 |
} |
| 67 |
|
| 68 |
private void fold(String js, String expected) { |
| 69 |
test(js, expected); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private void assertResultString(String js, String expected) { |
| 75 |
PeepholeIntegrationTest scTest = new PeepholeIntegrationTest(false); |
| 76 |
|
| 77 |
scTest.disableNormalize(); |
| 78 |
|
| 79 |
scTest.test(js, expected); |
| 80 |
} |
| 81 |
|
| 82 |
public void testTrueFalse() { |
| 83 |
late = false; |
| 84 |
foldSame("x = true"); |
| 85 |
foldSame("x = false"); |
| 86 |
fold("x = !1", "x = false"); |
| 87 |
fold("x = !0", "x = true"); |
| 88 |
late = true; |
| 89 |
fold("x = true", "x = !0"); |
| 90 |
fold("x = false", "x = !1"); |
| 91 |
foldSame("x = !1"); |
| 92 |
foldSame("x = !0"); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public void testFoldOneChildBlocksIntegration() { |
| 97 |
fold("function f(){switch(foo()){default:{break}}}", |
| 98 |
"function f(){foo()}"); |
| 99 |
|
| 100 |
fold("function f(){switch(x){default:{break}}}", |
| 101 |
"function f(){}"); |
| 102 |
|
| 103 |
fold("function f(){switch(x){default:x;case 1:return 2}}", |
| 104 |
"function f(){switch(x){default:case 1:return 2}}"); |
| 105 |
|
| 106 |
|
| 107 |
fold("if(x){if(true){foo();foo()}else{bar();bar()}}", |
| 108 |
"if(x){foo();foo()}"); |
| 109 |
|
| 110 |
fold("if(x){if(false){foo();foo()}else{bar();bar()}}", |
| 111 |
"if(x){bar();bar()}"); |
| 112 |
|
| 113 |
|
| 114 |
fold("if(x()){}", "x()"); |
| 115 |
|
| 116 |
fold("if(x()){} else {x()}", "x()||x()"); |
| 117 |
fold("if(x){}", ""); |
| 118 |
fold("if(a()){A()} else if (b()) {} else {C()}", "a()?A():b()||C()"); |
| 119 |
|
| 120 |
fold("if(a()){} else if (b()) {} else {C()}", |
| 121 |
"a()||b()||C()"); |
| 122 |
fold("if(a()){A()} else if (b()) {} else if (c()) {} else{D()}", |
| 123 |
"a()?A():b()||c()||D()"); |
| 124 |
fold("if(a()){} else if (b()) {} else if (c()) {} else{D()}", |
| 125 |
"a()||b()||c()||D()"); |
| 126 |
fold("if(a()){A()} else if (b()) {} else if (c()) {} else{}", |
| 127 |
"a()?A():b()||c()"); |
| 128 |
|
| 129 |
|
| 130 |
fold("function foo(){if(x()){}}", "function foo(){x()}"); |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
public void testFoldOneChildBlocksStringCompare() { |
| 135 |
|
| 136 |
assertResultString("if(x){if(y){var x;}}else{var z;}", |
| 137 |
"if(x){if(y)var x}else var z"); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
public void testNecessaryDanglingElse() { |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
assertResultString( |
| 146 |
"if(x)if(y){y();z()}else;else x()", "if(x){if(y){y();z()}}else x()"); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
public void testFoldReturnsIntegration() { |
| 151 |
|
| 152 |
fold("function f(){if(x)return;else return}", |
| 153 |
"function f(){}"); |
| 154 |
} |
| 155 |
|
| 156 |
public void testBug1059649() { |
| 157 |
|
| 158 |
fold("if(x){var y=3;}var z=5", "if(x)var y=3;var z=5"); |
| 159 |
|
| 160 |
|
| 161 |
foldSame("if(x){var y=3;}else{var y=4;}var z=5"); |
| 162 |
fold("while(x){var y=3;}var z=5", "while(x)var y=3;var z=5"); |
| 163 |
fold("for(var i=0;i<10;i++){var y=3;}var z=5", |
| 164 |
"for(var i=0;i<10;i++)var y=3;var z=5"); |
| 165 |
fold("for(var i in x){var y=3;}var z=5", |
| 166 |
"for(var i in x)var y=3;var z=5"); |
| 167 |
fold("do{var y=3;}while(x);var z=5", "do var y=3;while(x);var z=5"); |
| 168 |
} |
| 169 |
|
| 170 |
public void testHookIfIntegration() { |
| 171 |
fold("if (false){ x = 1; } else if (cond) { x = 2; } else { x = 3; }", |
| 172 |
"x=cond?2:3"); |
| 173 |
|
| 174 |
fold("x?void 0:y()", "x||y()"); |
| 175 |
fold("!x?void 0:y()", "(!x)||y()"); |
| 176 |
fold("x?y():void 0", "x&&y()"); |
| 177 |
} |
| 178 |
|
| 179 |
public void testRemoveDuplicateStatementsIntegration() { |
| 180 |
fold("function z() {if (a) { return true }" + |
| 181 |
"else if (b) { return true }" + |
| 182 |
"else { return true }}", |
| 183 |
"function z() {return true;}"); |
| 184 |
|
| 185 |
fold("function z() {if (a()) { return true }" + |
| 186 |
"else if (b()) { return true }" + |
| 187 |
"else { return true }}", |
| 188 |
"function z() {a()||b();return true;}"); |
| 189 |
} |
| 190 |
|
| 191 |
public void testFoldLogicalOpIntegration() { |
| 192 |
test("if(x && true) z()", "x&&z()"); |
| 193 |
test("if(x && false) z()", ""); |
| 194 |
fold("if(x || 3) z()", "z()"); |
| 195 |
fold("if(x || false) z()", "x&&z()"); |
| 196 |
test("if(x==y && false) z()", ""); |
| 197 |
|
| 198 |
fold("if(y() || x || 3) z()", "(y()||1)&&z()"); |
| 199 |
} |
| 200 |
|
| 201 |
public void testFoldBitwiseOpStringCompareIntegration() { |
| 202 |
assertResultString("while(-1 | 0){}", "while(1);"); |
| 203 |
} |
| 204 |
|
| 205 |
public void testVarLiftingIntegration() { |
| 206 |
fold("if(true);else var a;", "var a"); |
| 207 |
fold("if(false) foo();else var a;", "var a"); |
| 208 |
fold("if(true)var a;else;", "var a"); |
| 209 |
fold("if(false)var a;else;", "var a"); |
| 210 |
fold("if(false)var a,b;", "var b; var a"); |
| 211 |
fold("if(false){var a;var a;}", "var a"); |
| 212 |
fold("if(false)var a=function(){var b};", "var a"); |
| 213 |
fold("if(a)if(false)var a;else var b;", "var a;if(a)var b"); |
| 214 |
} |
| 215 |
|
| 216 |
public void testBug1438784() throws Exception { |
| 217 |
fold("for(var i=0;i<10;i++)if(x)x.y;", "for(var i=0;i<10;i++);"); |
| 218 |
} |
| 219 |
|
| 220 |
public void testFoldUselessWhileIntegration() { |
| 221 |
fold("while(!true) { foo() }", ""); |
| 222 |
fold("while(!false) foo() ", "while(1) foo()"); |
| 223 |
fold("while(!void 0) foo()", "while(1) foo()"); |
| 224 |
|
| 225 |
|
| 226 |
fold("if(foo())while(false){foo()}else bar()", "foo()||bar()"); |
| 227 |
} |
| 228 |
|
| 229 |
public void testFoldUselessForIntegration() { |
| 230 |
fold("for(;!true;) { foo() }", ""); |
| 231 |
fold("for(;void 0;) { foo() }", ""); |
| 232 |
fold("for(;undefined;) { foo() }", ""); |
| 233 |
fold("for(;1;) foo()", "for(;;) foo()"); |
| 234 |
fold("for(;!void 0;) foo()", "for(;;) foo()"); |
| 235 |
|
| 236 |
|
| 237 |
fold("if(foo())for(;false;){foo()}else bar()", "foo()||bar()"); |
| 238 |
} |
| 239 |
|
| 240 |
public void testFoldUselessDoIntegration() { |
| 241 |
test("do { foo() } while(!true);", "foo()"); |
| 242 |
fold("do { foo() } while(void 0);", "foo()"); |
| 243 |
fold("do { foo() } while(undefined);", "foo()"); |
| 244 |
fold("do { foo() } while(!void 0);", "do { foo() } while(1);"); |
| 245 |
|
| 246 |
|
| 247 |
test("if(foo())do {foo()} while(false) else bar()", "foo()?foo():bar()"); |
| 248 |
} |
| 249 |
|
| 250 |
public void testMinimizeWhileConstantConditionIntegration() { |
| 251 |
fold("while(!false) foo()", "while(1) foo()"); |
| 252 |
fold("while(202) foo()", "while(1) foo()"); |
| 253 |
fold("while(Infinity) foo()", "while(1) foo()"); |
| 254 |
fold("while('text') foo()", "while(1) foo()"); |
| 255 |
fold("while([]) foo()", "while(1) foo()"); |
| 256 |
fold("while({}) foo()", "while(1) foo()"); |
| 257 |
fold("while(/./) foo()", "while(1) foo()"); |
| 258 |
} |
| 259 |
|
| 260 |
public void testMinimizeExpr() { |
| 261 |
test("!!true", ""); |
| 262 |
|
| 263 |
fold("!!x()", "x()"); |
| 264 |
test("!(!x()&&!y())", "x()||y()"); |
| 265 |
fold("x()||!!y()", "x()||y()"); |
| 266 |
|
| 267 |
|
| 268 |
fold("!!x()&&y()", "x()&&y()"); |
| 269 |
} |
| 270 |
|
| 271 |
public void testBug1509085() { |
| 272 |
PeepholeIntegrationTest oneRepetitiontest = new PeepholeIntegrationTest() { |
| 273 |
@Override |
| 274 |
protected int getNumRepetitions() { |
| 275 |
return 1; |
| 276 |
} |
| 277 |
}; |
| 278 |
|
| 279 |
oneRepetitiontest.test("x ? x() : void 0", "x&&x();"); |
| 280 |
oneRepetitiontest.foldSame("y = x ? x() : void 0"); |
| 281 |
} |
| 282 |
|
| 283 |
public void testBugIssue3() { |
| 284 |
foldSame("function foo() {" + |
| 285 |
" if(sections.length != 1) children[i] = 0;" + |
| 286 |
" else var selectedid = children[i]" + |
| 287 |
"}"); |
| 288 |
} |
| 289 |
|
| 290 |
public void testBugIssue43() { |
| 291 |
foldSame("function foo() {" + |
| 292 |
" if (a) { var b = 1; } else { a.b = 1; }" + |
| 293 |
"}"); |
| 294 |
} |
| 295 |
|
| 296 |
public void testFoldNegativeBug() { |
| 297 |
fold("while(-3){};", "while(1);"); |
| 298 |
} |
| 299 |
|
| 300 |
public void testNoNormalizeLabeledExpr() { |
| 301 |
enableNormalize(true); |
| 302 |
foldSame("var x; foo:{x = 3;}"); |
| 303 |
foldSame("var x; foo:x = 3;"); |
| 304 |
} |
| 305 |
|
| 306 |
public void testShortCircuit1() { |
| 307 |
test("1 && a()", "a()"); |
| 308 |
} |
| 309 |
|
| 310 |
public void testShortCircuit2() { |
| 311 |
test("1 && a() && 2", "a()"); |
| 312 |
} |
| 313 |
|
| 314 |
public void testShortCircuit3() { |
| 315 |
test("a() && 1 && 2", "a()"); |
| 316 |
} |
| 317 |
|
| 318 |
public void testShortCircuit4() { |
| 319 |
test("a() && (1 && b())", "a() && b()"); |
| 320 |
test("a() && 1 && b()", "a() && b()"); |
| 321 |
test("(a() && 1) && b()", "a() && b()"); |
| 322 |
} |
| 323 |
|
| 324 |
public void testMinimizeExprCondition() { |
| 325 |
fold("(x || true) && y()", "y()"); |
| 326 |
fold("(x || false) && y()", "x&&y()"); |
| 327 |
fold("(x && true) && y()", "x && y()"); |
| 328 |
fold("(x && false) && y()", ""); |
| 329 |
fold("a = x || false ? b : c", "a=x?b:c"); |
| 330 |
fold("do {x()} while((x && false) && y())", "x()"); |
| 331 |
} |
| 332 |
|
| 333 |
public void testTrueFalseFolding() { |
| 334 |
late = true; |
| 335 |
fold("x = true", "x = !0"); |
| 336 |
fold("x = false", "x = !1"); |
| 337 |
fold("x = !3", "x = !1"); |
| 338 |
fold("x = true && !0", "x = !0"); |
| 339 |
fold("x = !!!!!!!!!!!!3", "x = !0"); |
| 340 |
fold("if(!3){x()}", ""); |
| 341 |
fold("if(!!3){x()}", "x()"); |
| 342 |
} |
| 343 |
|
| 344 |
public void testCommaSplitingConstantCondition() { |
| 345 |
late = false; |
| 346 |
fold("(b=0,b=1);if(b)x=b;", "b=0;b=1;x=b;"); |
| 347 |
fold("(b=0,b=1);if(b)x=b;", "b=0;b=1;x=b;"); |
| 348 |
} |
| 349 |
|
| 350 |
public void testAvoidCommaSplitting() { |
| 351 |
late = false; |
| 352 |
fold("x(),y(),z()", "x();y();z()"); |
| 353 |
late = true; |
| 354 |
foldSame("x(),y(),z()"); |
| 355 |
} |
| 356 |
|
| 357 |
public void testObjectLiteral() { |
| 358 |
test("({})", ""); |
| 359 |
test("({a:1})", ""); |
| 360 |
test("({a:foo()})", "foo()"); |
| 361 |
test("({'a':foo()})", "foo()"); |
| 362 |
} |
| 363 |
|
| 364 |
public void testArrayLiteral() { |
| 365 |
test("([])", ""); |
| 366 |
test("([1])", ""); |
| 367 |
test("([a])", ""); |
| 368 |
test("([foo()])", "foo()"); |
| 369 |
} |
| 370 |
|
| 371 |
public void testFoldIfs1() { |
| 372 |
fold("function f() {if (x) return 1; else if (y) return 1;}", |
| 373 |
"function f() {if (x||y) return 1;}"); |
| 374 |
fold("function f() {if (x) return 1; else {if (y) return 1; else foo();}}", |
| 375 |
"function f() {if (x||y) return 1; foo();}"); |
| 376 |
} |
| 377 |
|
| 378 |
public void testFoldIfs2() { |
| 379 |
fold("function f() {if (x) { a(); } else if (y) { a() }}", |
| 380 |
"function f() {x?a():y&&a();}"); |
| 381 |
} |
| 382 |
|
| 383 |
public void testFoldHook2() { |
| 384 |
fold("function f(a) {if (!a) return a; else return a;}", |
| 385 |
"function f(a) {return a}"); |
| 386 |
} |
| 387 |
|
| 388 |
public void disable_testFoldHook1() { |
| 389 |
fold("function f(a) {return (!a)?a:a;}", |
| 390 |
"function f(a) {return a}"); |
| 391 |
} |
| 392 |
} |