-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
UnreachableCodeEliminationTest.java
482 lines (411 loc) · 15.3 KB
/
UnreachableCodeEliminationTest.java
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
/*
* Copyright 2008 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;
/**
* Test for {@link UnreachableCodeElimination}.
*
*/
public final class UnreachableCodeEliminationTest extends CompilerTestCase {
private boolean removeNoOpStatements = true;
@Override
protected CompilerPass getProcessor(Compiler compiler) {
return new UnreachableCodeElimination(compiler, removeNoOpStatements);
}
@Override protected void setUp() throws Exception {
super.setUp();
enableComputeSideEffects();
removeNoOpStatements = true;
}
public void testRemoveUnreachableCode() {
// switch statement with stuff after "return"
test("function foo(){switch(foo){case 1:x=1;return;break;" +
"case 2:{x=2;return;break}default:}}",
"function foo(){switch(foo){case 1:x=1;return;" +
"case 2:{x=2}default:}}");
// if/else statements with returns
test("function bar(){if(foo)x=1;else if(bar){return;x=2}" +
"else{x=3;return;x=4}return 5;x=5}",
"function bar(){if(foo)x=1;else if(bar){return}" +
"else{x=3;return}return 5}");
// if statements without blocks
test("function foo(){if(x==3)return;x=4;y++;while(y==4){return;x=3}}",
"function foo(){if(x==3)return;x=4;y++;while(y==4){return}}");
// for/do/while loops
test("function baz(){for(i=0;i<n;i++){x=3;break;x=4}" +
"do{x=2;break;x=4}while(x==4);" +
"while(i<4){x=3;return;x=6}}",
"function baz(){for(i=0;i<n;){x=3;break}" +
"do{x=2;break}while(x==4);" +
"while(i<4){x=3;return}}");
// return statements on the same level as conditionals
test("function foo(){if(x==3){return}return 5;while(y==4){x++;return;x=4}}",
"function foo(){if(x==3){return}return 5}");
// return statements on the same level as conditionals
test("function foo(){return 3;for(;y==4;){x++;return;x=4}}",
"function foo(){return 3}");
// try/catch statements
test("function foo(){try{x=3;return x+1;x=5}catch(e){x=4;return 5;x=5}}",
"function foo(){try{x=3;return x+1}catch(e){x=4;return 5}}");
// try/finally statements
test("function foo(){try{x=3;return x+1;x=5}finally{x=4;return 5;x=5}}",
"function foo(){try{x=3;return x+1}finally{x=4;return 5}}");
// try/catch/finally statements
test("function foo(){try{x=3;return x+1;x=5}catch(e){x=3;return;x=2}" +
"finally{x=4;return 5;x=5}}",
"function foo(){try{x=3;return x+1}catch(e){x=3;return}" +
"finally{x=4;return 5}}");
// test a combination of blocks
test("function foo(){x=3;if(x==4){x=5;return;x=6}else{x=7}return 5;x=3}",
"function foo(){x=3;if(x==4){x=5;return}else{x=7}return 5}");
// test removing multiple statements
test("function foo() { return 1; var x = 2; var y = 10; return 2;}",
"function foo() { var y; var x; return 1}");
test("function foo() { return 1; x = 2; y = 10; return 2;}",
"function foo(){ return 1}");
}
public void testRemoveUselessNameStatements() {
test("a;", "");
test("a.b;", "");
test("a.b.MyClass.prototype.memberName;", "");
}
public void testRemoveUselessStrings() {
test("'a';", "");
}
public void testNoRemoveUseStrict() {
test("'use strict';", "'use strict'");
}
public void testNoRemoveUselessNameStatements() {
removeNoOpStatements = false;
testSame("a;");
testSame("a.b;");
testSame("a.b.MyClass.prototype.memberName;");
}
public void testRemoveDo() {
testSame("do { print(1); break } while(1)");
test("while(1) { break; do { print(1); break } while(1) }",
"while(1) { break; do {} while(1) }");
}
public void testRemoveUselessLiteralValueStatements() {
test("true;", "");
test("'hi';", "");
test("if (x) 1;", "");
test("while (x) 1;", "while (x);");
test("do 1; while (x);", "do ; while (x);");
test("for (;;) 1;", "for (;;);");
test("switch(x){case 1:true;case 2:'hi';default:true}",
"switch(x){case 1:case 2:default:}");
}
public void testConditionalDeadCode() {
test("function f() { if (1) return 5; else return 5; x = 1}",
"function f() { if (1) return 5; else return 5; }");
}
public void testSwitchCase() {
test("function f() { switch(x) { default: return 5; foo()}}",
"function f() { switch(x) { default: return 5;}}");
testSame("function f() { switch(x) { default: return; case 1: foo(); bar()}}");
test("function f() { switch(x) { default: return; case 1: return 5;bar()}}",
"function f() { switch(x) { default: return; case 1: return 5;}}");
}
public void testTryCatchFinally() {
testSame("try {foo()} catch (e) {bar()}");
testSame("try { try {foo()} catch (e) {bar()}} catch (x) {bar()}");
test("try {var x = 1} catch (e) {e()}", "try {var x = 1} finally {}");
test("try {var x = 1} catch (e) {e()} finally {x()}",
" try {var x = 1} finally {x()}");
test("try {var x = 1} catch (e) {e()} finally {}",
" try {var x = 1} finally {}");
testSame("try {var x = 1} finally {x()}");
testSame("try {var x = 1} finally {}");
test("function f() {return; try{var x = 1}catch(e){} }",
"function f() {var x;}");
}
public void testRemovalRequiresRedeclaration() {
test("while(1) { break; var x = 1}", "var x; while(1) { break } ");
test("while(1) { break; var x=1; var y=1}",
"var y; var x; while(1) { break } ");
}
public void testAssignPropertyOnCreatedObject() {
testSame("this.foo = 3;");
testSame("a.foo = 3;");
testSame("bar().foo = 3;");
testSame("({}).foo = bar();");
testSame("(new X()).foo = 3;");
test("({}).foo = 3;", "");
test("(function() {}).prototype.toString = function(){};", "");
test("(function() {}).prototype['toString'] = function(){};", "");
test("(function() {}).prototype[f] = function(){};", "");
}
public void testUselessUnconditionalReturn() {
test("function foo() { return }", " function foo() { }");
test("function foo() { return; return; x=1 }", "function foo() { }");
test("function foo() { return; return; var x=1}", "function foo() {var x}");
test("function foo() { return; function bar() {} }",
"function foo() { function bar() {} }" );
testSame("function foo() { return 5 }");
test("function f() {switch (a) { case 'a': return}}",
"function f() {switch (a) { case 'a': }}");
testSame("function f() {switch (a) { case 'a': case foo(): }}");
testSame("function f() {switch (a) {" +
" default: return; case 'a': alert(1)}}");
testSame("function f() {switch (a) {" +
" case 'a': return; default: alert(1)}}");
}
public void testUselessUnconditionalContinue() {
test("for(;1;) {continue}", " for(;1;) {}");
test("for(;0;) {continue}", " for(;0;) {}");
testSame("X: for(;1;) { for(;1;) { if (x()) {continue X} x = 1}}");
test("for(;1;) { X: for(;1;) { if (x()) {continue X} }}",
"for(;1;) { X: for(;1;) { if (x()) {}}}");
test("do { continue } while(1);", "do { } while(1);");
}
public void testUselessUnconditionalBreak() {
test("switch (a) { case 'a': break }", "switch (a) { case 'a': }");
test("switch (a) { case 'a': break; case foo(): }",
"switch (a) { case 'a': case foo(): }");
test("switch (a) { default: break; case 'a': }",
"switch (a) { default: case 'a': }");
testSame("switch (a) { case 'a': alert(a); break; default: alert(a); }");
testSame("switch (a) { default: alert(a); break; case 'a': alert(a); }");
test("X: {switch (a) { case 'a': break X}}",
"X: {switch (a) { case 'a': }}");
testSame("X: {switch (a) { case 'a': if (a()) {break X} a = 1}}");
test("X: {switch (a) { case 'a': if (a()) {break X}}}",
"X: {switch (a) { case 'a': if (a()) {}}}");
test("X: {switch (a) { case 'a': if (a()) {break X}}}",
"X: {switch (a) { case 'a': if (a()) {}}}");
testSame("do { break } while(1);");
testSame("for(;1;) { break }");
}
// These tests all require the analysis to go to a fixpoint in order to pass
public void testIteratedRemoval() {
test("switch (a) { case 'a': break; case 'b': break; case 'c': break }",
" switch (a) { case 'a': case 'b': case 'c': }");
test("function foo() {" +
" switch (a) { case 'a':return; case 'b':return; case 'c':return }}",
" function foo() { switch (a) { case 'a': case 'b': case 'c': }}");
test("for (;;) {\n" +
" switch (a) {\n" +
" case 'a': continue;\n" +
" case 'b': continue;\n" +
" case 'c': continue;\n" +
" }\n" +
" }",
" for (;;) { switch (a) { case 'a': case 'b': case 'c': } }");
test("function foo() { if (x) { return; } if (x) { return; } x; }",
" function foo() {}");
test("var x; \n" +
" out: { \n" +
" try { break out; } catch (e) { break out; } \n" +
" x = undefined; \n" +
" }",
" var x; out: {}");
}
public void testIssue311() {
test("function a(b) {\n" +
" switch (b.v) {\n" +
" case 'SWITCH':\n" +
" if (b.i >= 0) {\n" +
" return b.o;\n" +
" } else {\n" +
" return;\n" +
" }\n" +
" break;\n" +
" }\n" +
"}",
"function a(b) {\n" +
" switch (b.v) {\n" +
" case 'SWITCH':\n" +
" if (b.i >= 0) {\n" +
" return b.o;\n" +
" } else {\n" +
" }\n" +
" }\n" +
"}");
}
public void testIssue4177428a() {
testSame(
"f = function() {\n" +
" var action;\n" +
" a: {\n" +
" var proto = null;\n" +
" try {\n" +
" proto = new Proto\n" +
" } finally {\n" +
" action = proto;\n" +
" break a\n" + // Keep this...
" }\n" +
" }\n" +
" alert(action)\n" + // and this.
"};");
}
public void testIssue4177428b() {
testSame(
"f = function() {\n" +
" var action;\n" +
" a: {\n" +
" var proto = null;\n" +
" try {\n" +
" try {\n" +
" proto = new Proto\n" +
" } finally {\n" +
" action = proto;\n" +
" break a\n" + // Keep this...
" }\n" +
" } finally {\n" +
" }\n" +
" }\n" +
" alert(action)\n" + // and this.
"};");
}
public void testIssue4177428c() {
testSame(
"f = function() {\n" +
" var action;\n" +
" a: {\n" +
" var proto = null;\n" +
" try {\n" +
" } finally {\n" +
" try {\n" +
" proto = new Proto\n" +
" } finally {\n" +
" action = proto;\n" +
" break a\n" + // Keep this...
" }\n" +
" }\n" +
" }\n" +
" alert(action)\n" + // and this.
"};");
}
public void testIssue4177428_continue() {
testSame(
"f = function() {\n" +
" var action;\n" +
" a: do {\n" +
" var proto = null;\n" +
" try {\n" +
" proto = new Proto\n" +
" } finally {\n" +
" action = proto;\n" +
" continue a\n" + // Keep this...
" }\n" +
" } while(false)\n" +
" alert(action)\n" + // and this.
"};");
}
public void testIssue4177428_return() {
test(
"f = function() {\n" +
" var action;\n" +
" a: {\n" +
" var proto = null;\n" +
" try {\n" +
" proto = new Proto\n" +
" } finally {\n" +
" action = proto;\n" +
" return\n" + // Keep this...
" }\n" +
" }\n" +
" alert(action)\n" + // and remove this.
"};",
"f = function() {\n" +
" var action;\n" +
" a: {\n" +
" var proto = null;\n" +
" try {\n" +
" proto = new Proto\n" +
" } finally {\n" +
" action = proto;\n" +
" return\n" +
" }\n" +
" }\n" +
"};"
);
}
public void testIssue4177428_multifinally() {
testSame(
"a: {\n" +
" try {\n" +
" try {\n" +
" } finally {\n" +
" break a;\n" +
" }\n" +
" } finally {\n" +
" x = 1;\n" +
" }\n" +
"}");
}
public void testIssue5215541_deadVarDeclar() {
testSame("throw 1; var x");
testSame("throw 1; function x() {}");
testSame("throw 1; var x; var y;");
test("throw 1; var x = foo", "var x; throw 1");
}
public void testForInLoop() {
testSame("for(var x in y) {}");
}
public void testDontRemoveBreakInTryFinally() throws Exception {
testSame("function f() {b:try{throw 9} finally {break b} return 1;}");
}
public void testDontRemoveBreakInTryFinallySwitch() throws Exception {
testSame("function f() {b:try{throw 9} finally {" +
"switch(x) {case 1: break b} } return 1;}");
}
public void testIssue1001() throws Exception {
test("function f(x) { x.property = 3; } f({})",
"function f(x) { x.property = 3; }");
test("function f(x) { x.property = 3; } new f({})",
"function f(x) { x.property = 3; }");
}
public void testLetConstBlocks() {
test("function f() {return 1; let a; }", "function f() {return 1;}");
test("function f() {return 1; const a = 1; }", "function f() {return 1;}");
test(
"function f() { x = 1; {let g; return x} let y}",
"function f() { x = 1; {let g; return x;}} ");
}
public void testArrowFunctions() {
test("f(x => {return x; j = 1})", "f(x => {return x;})");
testSame("f( () => {return 1;})");
}
public void testGenerators() {
test(
LINE_JOINER.join(
"function* f() {", " while(true) {", " yield 1;", " }", " x = 1;", "}"),
LINE_JOINER.join("function* f() {", " while(true) {", " yield 1;", " }", "}"));
testSame(LINE_JOINER.join("function* f() {", " while(true) {", " yield 1;", " }", "}"));
testSame(
LINE_JOINER.join(
"function* f() {",
" let i = 0;",
" while (true) {",
" if (i < 10) {",
" yield i;",
" } else {",
" break;",
" }",
" }",
" let x = 1;",
"}"));
}
public void testForOf() {
test("for(x of i){ 1; }", "for(x of i) {}");
testSame("for(x of i){}");
}
public void testRemoveUselessTemplateStrings() {
test("`hi`", "");
testSame("`hello visitor # ${i++}`");
}
}