| 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 |
|
| 23 |
public class UnreachableCodeEliminationTest extends CompilerTestCase { |
| 24 |
private boolean removeNoOpStatements = true; |
| 25 |
|
| 26 |
@Override |
| 27 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 28 |
return new UnreachableCodeElimination(compiler, removeNoOpStatements); |
| 29 |
} |
| 30 |
|
| 31 |
@Override public void setUp() throws Exception { |
| 32 |
super.setUp(); |
| 33 |
removeNoOpStatements = true; |
| 34 |
} |
| 35 |
|
| 36 |
public void testRemoveUnreachableCode() { |
| 37 |
|
| 38 |
test("function foo(){switch(foo){case 1:x=1;return;break;" + |
| 39 |
"case 2:{x=2;return;break}default:}}", |
| 40 |
"function foo(){switch(foo){case 1:x=1;return;" + |
| 41 |
"case 2:{x=2}default:}}"); |
| 42 |
|
| 43 |
|
| 44 |
test("function bar(){if(foo)x=1;else if(bar){return;x=2}" + |
| 45 |
"else{x=3;return;x=4}return 5;x=5}", |
| 46 |
"function bar(){if(foo)x=1;else if(bar){return}" + |
| 47 |
"else{x=3;return}return 5}"); |
| 48 |
|
| 49 |
|
| 50 |
test("function foo(){if(x==3)return;x=4;y++;while(y==4){return;x=3}}", |
| 51 |
"function foo(){if(x==3)return;x=4;y++;while(y==4){return}}"); |
| 52 |
|
| 53 |
|
| 54 |
test("function baz(){for(i=0;i<n;i++){x=3;break;x=4}" + |
| 55 |
"do{x=2;break;x=4}while(x==4);" + |
| 56 |
"while(i<4){x=3;return;x=6}}", |
| 57 |
"function baz(){for(i=0;i<n;){x=3;break}" + |
| 58 |
"do{x=2;break}while(x==4);" + |
| 59 |
"while(i<4){x=3;return}}"); |
| 60 |
|
| 61 |
|
| 62 |
test("function foo(){if(x==3){return}return 5;while(y==4){x++;return;x=4}}", |
| 63 |
"function foo(){if(x==3){return}return 5}"); |
| 64 |
|
| 65 |
|
| 66 |
test("function foo(){return 3;for(;y==4;){x++;return;x=4}}", |
| 67 |
"function foo(){return 3}"); |
| 68 |
|
| 69 |
|
| 70 |
test("function foo(){try{x=3;return x+1;x=5}catch(e){x=4;return 5;x=5}}", |
| 71 |
"function foo(){try{x=3;return x+1}catch(e){x=4;return 5}}"); |
| 72 |
|
| 73 |
|
| 74 |
test("function foo(){try{x=3;return x+1;x=5}finally{x=4;return 5;x=5}}", |
| 75 |
"function foo(){try{x=3;return x+1}finally{x=4;return 5}}"); |
| 76 |
|
| 77 |
|
| 78 |
test("function foo(){try{x=3;return x+1;x=5}catch(e){x=3;return;x=2}" + |
| 79 |
"finally{x=4;return 5;x=5}}", |
| 80 |
|
| 81 |
"function foo(){try{x=3;return x+1}catch(e){x=3;return}" + |
| 82 |
"finally{x=4;return 5}}"); |
| 83 |
|
| 84 |
|
| 85 |
test("function foo(){x=3;if(x==4){x=5;return;x=6}else{x=7}return 5;x=3}", |
| 86 |
"function foo(){x=3;if(x==4){x=5;return}else{x=7}return 5}"); |
| 87 |
|
| 88 |
|
| 89 |
test("function foo() { return 1; var x = 2; var y = 10; return 2;}", |
| 90 |
"function foo() { var y; var x; return 1}"); |
| 91 |
|
| 92 |
test("function foo() { return 1; x = 2; y = 10; return 2;}", |
| 93 |
"function foo(){ return 1}"); |
| 94 |
} |
| 95 |
|
| 96 |
public void testRemoveUselessNameStatements() { |
| 97 |
test("a;", ""); |
| 98 |
test("a.b;", ""); |
| 99 |
test("a.b.MyClass.prototype.memberName;", ""); |
| 100 |
} |
| 101 |
|
| 102 |
public void testRemoveUselessStrings() { |
| 103 |
test("'a';", ""); |
| 104 |
} |
| 105 |
|
| 106 |
public void testNoRemoveUseStrict() { |
| 107 |
test("'use strict';", "'use strict'"); |
| 108 |
} |
| 109 |
|
| 110 |
public void testNoRemoveUselessNameStatements() { |
| 111 |
removeNoOpStatements = false; |
| 112 |
testSame("a;"); |
| 113 |
testSame("a.b;"); |
| 114 |
testSame("a.b.MyClass.prototype.memberName;"); |
| 115 |
} |
| 116 |
|
| 117 |
public void testRemoveDo() { |
| 118 |
test("do { print(1); break } while(1)", "do { print(1); break } while(1)"); |
| 119 |
test("while(1) { break; do { print(1); break } while(1) }", |
| 120 |
"while(1) { break; do {} while(1) }"); |
| 121 |
} |
| 122 |
|
| 123 |
public void testRemoveUselessLiteralValueStatements() { |
| 124 |
test("true;", ""); |
| 125 |
test("'hi';", ""); |
| 126 |
test("if (x) 1;", ""); |
| 127 |
test("while (x) 1;", "while (x);"); |
| 128 |
test("do 1; while (x);", "do ; while (x);"); |
| 129 |
test("for (;;) 1;", "for (;;);"); |
| 130 |
test("switch(x){case 1:true;case 2:'hi';default:true}", |
| 131 |
"switch(x){case 1:case 2:default:}"); |
| 132 |
} |
| 133 |
|
| 134 |
public void testConditionalDeadCode() { |
| 135 |
test("function f() { if (1) return 5; else return 5; x = 1}", |
| 136 |
"function f() { if (1) return 5; else return 5; }"); |
| 137 |
} |
| 138 |
|
| 139 |
public void testSwitchCase() { |
| 140 |
test("function f() { switch(x) { default: return 5; foo()}}", |
| 141 |
"function f() { switch(x) { default: return 5;}}"); |
| 142 |
test("function f() { switch(x) { default: return; case 1: foo(); bar()}}", |
| 143 |
"function f() { switch(x) { default: return; case 1: foo(); bar()}}"); |
| 144 |
test("function f() { switch(x) { default: return; case 1: return 5;bar()}}", |
| 145 |
"function f() { switch(x) { default: return; case 1: return 5;}}"); |
| 146 |
} |
| 147 |
|
| 148 |
public void testTryCatchFinally() { |
| 149 |
testSame("try {foo()} catch (e) {bar()}"); |
| 150 |
testSame("try { try {foo()} catch (e) {bar()}} catch (x) {bar()}"); |
| 151 |
test("try {var x = 1} catch (e) {e()}", "try {var x = 1} finally {}"); |
| 152 |
test("try {var x = 1} catch (e) {e()} finally {x()}", |
| 153 |
" try {var x = 1} finally {x()}"); |
| 154 |
test("try {var x = 1} catch (e) {e()} finally {}", |
| 155 |
" try {var x = 1} finally {}"); |
| 156 |
testSame("try {var x = 1} finally {x()}"); |
| 157 |
testSame("try {var x = 1} finally {}"); |
| 158 |
test("function f() {return; try{var x = 1}catch(e){} }", |
| 159 |
"function f() {var x;}"); |
| 160 |
} |
| 161 |
|
| 162 |
public void testRemovalRequiresRedeclaration() { |
| 163 |
test("while(1) { break; var x = 1}", "var x; while(1) { break } "); |
| 164 |
test("while(1) { break; var x=1; var y=1}", |
| 165 |
"var y; var x; while(1) { break } "); |
| 166 |
} |
| 167 |
|
| 168 |
public void testAssignPropertyOnCreatedObject() { |
| 169 |
testSame("this.foo = 3;"); |
| 170 |
testSame("a.foo = 3;"); |
| 171 |
testSame("bar().foo = 3;"); |
| 172 |
testSame("({}).foo = bar();"); |
| 173 |
testSame("(new X()).foo = 3;"); |
| 174 |
|
| 175 |
test("({}).foo = 3;", ""); |
| 176 |
test("(function() {}).prototype.toString = function(){};", ""); |
| 177 |
test("(function() {}).prototype['toString'] = function(){};", ""); |
| 178 |
test("(function() {}).prototype[f] = function(){};", ""); |
| 179 |
} |
| 180 |
|
| 181 |
public void testUselessUnconditionalReturn() { |
| 182 |
test("function foo() { return }", " function foo() { }"); |
| 183 |
test("function foo() { return; return; x=1 }", "function foo() { }"); |
| 184 |
test("function foo() { return; return; var x=1}", "function foo() {var x}"); |
| 185 |
test("function foo() { return; function bar() {} }", |
| 186 |
"function foo() { function bar() {} }" ); |
| 187 |
testSame("function foo() { return 5 }"); |
| 188 |
|
| 189 |
test("function f() {switch (a) { case 'a': return}}", |
| 190 |
"function f() {switch (a) { case 'a': }}"); |
| 191 |
testSame("function f() {switch (a) { case 'a': case foo(): }}"); |
| 192 |
testSame("function f() {switch (a) {" + |
| 193 |
" default: return; case 'a': alert(1)}}"); |
| 194 |
testSame("function f() {switch (a) {" + |
| 195 |
" case 'a': return; default: alert(1)}}"); |
| 196 |
} |
| 197 |
|
| 198 |
public void testUselessUnconditionalContinue() { |
| 199 |
test("for(;1;) {continue}", " for(;1;) {}"); |
| 200 |
test("for(;0;) {continue}", " for(;0;) {}"); |
| 201 |
|
| 202 |
testSame("X: for(;1;) { for(;1;) { if (x()) {continue X} x = 1}}"); |
| 203 |
test("for(;1;) { X: for(;1;) { if (x()) {continue X} }}", |
| 204 |
"for(;1;) { X: for(;1;) { if (x()) {}}}"); |
| 205 |
|
| 206 |
test("do { continue } while(1);", "do { } while(1);"); |
| 207 |
} |
| 208 |
|
| 209 |
public void testUselessUnconditonalBreak() { |
| 210 |
test("switch (a) { case 'a': break }", "switch (a) { case 'a': }"); |
| 211 |
test("switch (a) { case 'a': break; case foo(): }", |
| 212 |
"switch (a) { case 'a': case foo(): }"); |
| 213 |
test("switch (a) { default: break; case 'a': }", |
| 214 |
"switch (a) { default: case 'a': }"); |
| 215 |
|
| 216 |
testSame("switch (a) { case 'a': alert(a); break; default: alert(a); }"); |
| 217 |
testSame("switch (a) { default: alert(a); break; case 'a': alert(a); }"); |
| 218 |
|
| 219 |
|
| 220 |
test("X: {switch (a) { case 'a': break X}}", |
| 221 |
"X: {switch (a) { case 'a': }}"); |
| 222 |
|
| 223 |
testSame("X: {switch (a) { case 'a': if (a()) {break X} a = 1}}"); |
| 224 |
test("X: {switch (a) { case 'a': if (a()) {break X}}}", |
| 225 |
"X: {switch (a) { case 'a': if (a()) {}}}"); |
| 226 |
|
| 227 |
test("X: {switch (a) { case 'a': if (a()) {break X}}}", |
| 228 |
"X: {switch (a) { case 'a': if (a()) {}}}"); |
| 229 |
|
| 230 |
|
| 231 |
testSame("do { break } while(1);"); |
| 232 |
testSame("for(;1;) { break }"); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
public void testIteratedRemoval() { |
| 237 |
test("switch (a) { case 'a': break; case 'b': break; case 'c': break }", |
| 238 |
" switch (a) { case 'a': case 'b': case 'c': }"); |
| 239 |
|
| 240 |
test("function foo() {" + |
| 241 |
" switch (a) { case 'a':return; case 'b':return; case 'c':return }}", |
| 242 |
" function foo() { switch (a) { case 'a': case 'b': case 'c': }}"); |
| 243 |
|
| 244 |
test("for (;;) {\n" + |
| 245 |
" switch (a) {\n" + |
| 246 |
" case 'a': continue;\n" + |
| 247 |
" case 'b': continue;\n" + |
| 248 |
" case 'c': continue;\n" + |
| 249 |
" }\n" + |
| 250 |
" }", |
| 251 |
" for (;;) { switch (a) { case 'a': case 'b': case 'c': } }"); |
| 252 |
|
| 253 |
test("function foo() { if (x) { return; } if (x) { return; } x; }", |
| 254 |
" function foo() {}"); |
| 255 |
|
| 256 |
test("var x; \n" + |
| 257 |
" out: { \n" + |
| 258 |
" try { break out; } catch (e) { break out; } \n" + |
| 259 |
" x = undefined; \n" + |
| 260 |
" }", |
| 261 |
" var x; out: {}"); |
| 262 |
} |
| 263 |
|
| 264 |
public void testIssue311() { |
| 265 |
test("function a(b) {\n" + |
| 266 |
" switch (b.v) {\n" + |
| 267 |
" case 'SWITCH':\n" + |
| 268 |
" if (b.i >= 0) {\n" + |
| 269 |
" return b.o;\n" + |
| 270 |
" } else {\n" + |
| 271 |
" return;\n" + |
| 272 |
" }\n" + |
| 273 |
" break;\n" + |
| 274 |
" }\n" + |
| 275 |
"}", |
| 276 |
"function a(b) {\n" + |
| 277 |
" switch (b.v) {\n" + |
| 278 |
" case 'SWITCH':\n" + |
| 279 |
" if (b.i >= 0) {\n" + |
| 280 |
" return b.o;\n" + |
| 281 |
" } else {\n" + |
| 282 |
" }\n" + |
| 283 |
" }\n" + |
| 284 |
"}"); |
| 285 |
} |
| 286 |
|
| 287 |
public void testIssue4177428a() { |
| 288 |
test( |
| 289 |
"f = function() {\n" + |
| 290 |
" var action;\n" + |
| 291 |
" a: {\n" + |
| 292 |
" var proto = null;\n" + |
| 293 |
" try {\n" + |
| 294 |
" proto = new Proto\n" + |
| 295 |
" } finally {\n" + |
| 296 |
" action = proto;\n" + |
| 297 |
" break a\n" + |
| 298 |
" }\n" + |
| 299 |
" }\n" + |
| 300 |
" alert(action)\n" + |
| 301 |
"};", |
| 302 |
"f = function() {\n" + |
| 303 |
" var action;\n" + |
| 304 |
" a: {\n" + |
| 305 |
" var proto = null;\n" + |
| 306 |
" try {\n" + |
| 307 |
" proto = new Proto\n" + |
| 308 |
" } finally {\n" + |
| 309 |
" action = proto;\n" + |
| 310 |
" }\n" + |
| 311 |
" }\n" + |
| 312 |
" alert(action)\n" + |
| 313 |
"};" |
| 314 |
); |
| 315 |
} |
| 316 |
|
| 317 |
public void testIssue4177428b() { |
| 318 |
test( |
| 319 |
"f = function() {\n" + |
| 320 |
" var action;\n" + |
| 321 |
" a: {\n" + |
| 322 |
" var proto = null;\n" + |
| 323 |
" try {\n" + |
| 324 |
" try {\n" + |
| 325 |
" proto = new Proto\n" + |
| 326 |
" } finally {\n" + |
| 327 |
" action = proto;\n" + |
| 328 |
" break a\n" + |
| 329 |
" }\n" + |
| 330 |
" } finally {\n" + |
| 331 |
" }\n" + |
| 332 |
" }\n" + |
| 333 |
" alert(action)\n" + |
| 334 |
"};", |
| 335 |
"f = function() {\n" + |
| 336 |
" var action;\n" + |
| 337 |
" a: {\n" + |
| 338 |
" var proto = null;\n" + |
| 339 |
" try {\n" + |
| 340 |
" try {\n" + |
| 341 |
" proto = new Proto\n" + |
| 342 |
" } finally {\n" + |
| 343 |
" action = proto;\n" + |
| 344 |
" break a\n" + |
| 345 |
" }\n" + |
| 346 |
" } finally {\n" + |
| 347 |
" }\n" + |
| 348 |
" }\n" + |
| 349 |
" alert(action)\n" + |
| 350 |
"};" |
| 351 |
); |
| 352 |
} |
| 353 |
|
| 354 |
public void testIssue4177428c() { |
| 355 |
test( |
| 356 |
"f = function() {\n" + |
| 357 |
" var action;\n" + |
| 358 |
" a: {\n" + |
| 359 |
" var proto = null;\n" + |
| 360 |
" try {\n" + |
| 361 |
" } finally {\n" + |
| 362 |
" try {\n" + |
| 363 |
" proto = new Proto\n" + |
| 364 |
" } finally {\n" + |
| 365 |
" action = proto;\n" + |
| 366 |
" break a\n" + |
| 367 |
" }\n" + |
| 368 |
" }\n" + |
| 369 |
" }\n" + |
| 370 |
" alert(action)\n" + |
| 371 |
"};", |
| 372 |
"f = function() {\n" + |
| 373 |
" var action;\n" + |
| 374 |
" a: {\n" + |
| 375 |
" var proto = null;\n" + |
| 376 |
" try {\n" + |
| 377 |
" } finally {\n" + |
| 378 |
" try {\n" + |
| 379 |
" proto = new Proto\n" + |
| 380 |
" } finally {\n" + |
| 381 |
" action = proto;\n" + |
| 382 |
" }\n" + |
| 383 |
" }\n" + |
| 384 |
" }\n" + |
| 385 |
" alert(action)\n" + |
| 386 |
"};" |
| 387 |
); |
| 388 |
} |
| 389 |
|
| 390 |
public void testIssue4177428_continue() { |
| 391 |
test( |
| 392 |
"f = function() {\n" + |
| 393 |
" var action;\n" + |
| 394 |
" a: do {\n" + |
| 395 |
" var proto = null;\n" + |
| 396 |
" try {\n" + |
| 397 |
" proto = new Proto\n" + |
| 398 |
" } finally {\n" + |
| 399 |
" action = proto;\n" + |
| 400 |
" continue a\n" + |
| 401 |
" }\n" + |
| 402 |
" } while(false)\n" + |
| 403 |
" alert(action)\n" + |
| 404 |
"};", |
| 405 |
"f = function() {\n" + |
| 406 |
" var action;\n" + |
| 407 |
" a: do {\n" + |
| 408 |
" var proto = null;\n" + |
| 409 |
" try {\n" + |
| 410 |
" proto = new Proto\n" + |
| 411 |
" } finally {\n" + |
| 412 |
" action = proto;\n" + |
| 413 |
" }\n" + |
| 414 |
" } while (false)\n" + |
| 415 |
" alert(action)\n" + |
| 416 |
"};" |
| 417 |
); |
| 418 |
} |
| 419 |
|
| 420 |
public void testIssue4177428_return() { |
| 421 |
test( |
| 422 |
"f = function() {\n" + |
| 423 |
" var action;\n" + |
| 424 |
" a: {\n" + |
| 425 |
" var proto = null;\n" + |
| 426 |
" try {\n" + |
| 427 |
" proto = new Proto\n" + |
| 428 |
" } finally {\n" + |
| 429 |
" action = proto;\n" + |
| 430 |
" return\n" + |
| 431 |
" }\n" + |
| 432 |
" }\n" + |
| 433 |
" alert(action)\n" + |
| 434 |
"};", |
| 435 |
"f = function() {\n" + |
| 436 |
" var action;\n" + |
| 437 |
" a: {\n" + |
| 438 |
" var proto = null;\n" + |
| 439 |
" try {\n" + |
| 440 |
" proto = new Proto\n" + |
| 441 |
" } finally {\n" + |
| 442 |
" action = proto;\n" + |
| 443 |
" }\n" + |
| 444 |
" }\n" + |
| 445 |
"};" |
| 446 |
); |
| 447 |
} |
| 448 |
|
| 449 |
public void testIssue4177428_multifinally() { |
| 450 |
testSame( |
| 451 |
"a: {\n" + |
| 452 |
" try {\n" + |
| 453 |
" try {\n" + |
| 454 |
" } finally {\n" + |
| 455 |
" break a;\n" + |
| 456 |
" }\n" + |
| 457 |
" } finally {\n" + |
| 458 |
" x = 1;\n" + |
| 459 |
" }\n" + |
| 460 |
"}"); |
| 461 |
} |
| 462 |
|
| 463 |
public void testIssue5215541_deadVarDeclar() { |
| 464 |
testSame("throw 1; var x"); |
| 465 |
testSame("throw 1; function x() {}"); |
| 466 |
testSame("throw 1; var x; var y;"); |
| 467 |
test("throw 1; var x = foo", "var x; throw 1"); |
| 468 |
} |
| 469 |
|
| 470 |
public void testForInLoop() { |
| 471 |
testSame("for(var x in y) {}"); |
| 472 |
} |
| 473 |
} |