| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.javascript.rhino.Node; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class MinimizeExitPointsTest extends CompilerTestCase { |
| 25 |
|
| 26 |
@Override |
| 27 |
public void setUp() { |
| 28 |
super.enableLineNumberCheck(true); |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
protected CompilerPass getProcessor(final Compiler compiler) { |
| 33 |
return new CompilerPass() { |
| 34 |
@Override |
| 35 |
public void process(Node externs, Node js) { |
| 36 |
NodeTraversal.traverse(compiler, js, new MinimizeExitPoints(compiler)); |
| 37 |
} |
| 38 |
}; |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
protected int getNumRepetitions() { |
| 43 |
return 1; |
| 44 |
} |
| 45 |
|
| 46 |
void foldSame(String js) { |
| 47 |
testSame(js); |
| 48 |
} |
| 49 |
|
| 50 |
void fold(String js, String expected) { |
| 51 |
test(js, expected); |
| 52 |
} |
| 53 |
|
| 54 |
void fold(String js, String expected, DiagnosticType warning) { |
| 55 |
test(js, expected, warning); |
| 56 |
} |
| 57 |
|
| 58 |
public void testBreakOptimization() throws Exception { |
| 59 |
fold("f:{if(true){a();break f;}else;b();}", |
| 60 |
"f:{if(true){a()}else{b()}}"); |
| 61 |
fold("f:{if(false){a();break f;}else;b();break f;}", |
| 62 |
"f:{if(false){a()}else{b()}}"); |
| 63 |
fold("f:{if(a()){b();break f;}else;c();}", |
| 64 |
"f:{if(a()){b();}else{c();}}"); |
| 65 |
fold("f:{if(a()){b()}else{c();break f;}}", |
| 66 |
"f:{if(a()){b()}else{c();}}"); |
| 67 |
fold("f:{if(a()){b();break f;}else;}", |
| 68 |
"f:{if(a()){b();}else;}"); |
| 69 |
fold("f:{if(a()){break f;}else;}", |
| 70 |
"f:{if(a()){}else;}"); |
| 71 |
|
| 72 |
fold("f:while(a())break f;", |
| 73 |
"f:while(a())break f"); |
| 74 |
foldSame("f:for(x in a())break f"); |
| 75 |
|
| 76 |
fold("f:{while(a())break;}", |
| 77 |
"f:{while(a())break;}"); |
| 78 |
foldSame("f:{for(x in a())break}"); |
| 79 |
|
| 80 |
fold("f:try{break f;}catch(e){break f;}", |
| 81 |
"f:try{}catch(e){}"); |
| 82 |
fold("f:try{if(a()){break f;}else{break f;} break f;}catch(e){}", |
| 83 |
"f:try{if(a()){}else{}}catch(e){}"); |
| 84 |
|
| 85 |
fold("f:g:break f", |
| 86 |
""); |
| 87 |
fold("f:g:{if(a()){break f;}else{break f;} break f;}", |
| 88 |
"f:g:{if(a()){}else{}}"); |
| 89 |
} |
| 90 |
|
| 91 |
public void testFunctionReturnOptimization() throws Exception { |
| 92 |
fold("function f(){if(a()){b();if(c())return;}}", |
| 93 |
"function f(){if(a()){b();if(c());}}"); |
| 94 |
fold("function f(){if(x)return; x=3; return; }", |
| 95 |
"function f(){if(x); else x=3}"); |
| 96 |
fold("function f(){if(true){a();return;}else;b();}", |
| 97 |
"function f(){if(true){a();}else{b();}}"); |
| 98 |
fold("function f(){if(false){a();return;}else;b();return;}", |
| 99 |
"function f(){if(false){a();}else{b();}}"); |
| 100 |
fold("function f(){if(a()){b();return;}else;c();}", |
| 101 |
"function f(){if(a()){b();}else{c();}}"); |
| 102 |
fold("function f(){if(a()){b()}else{c();return;}}", |
| 103 |
"function f(){if(a()){b()}else{c();}}"); |
| 104 |
fold("function f(){if(a()){b();return;}else;}", |
| 105 |
"function f(){if(a()){b();}else;}"); |
| 106 |
fold("function f(){if(a()){return;}else{return;} return;}", |
| 107 |
"function f(){if(a()){}else{}}"); |
| 108 |
fold("function f(){if(a()){return;}else{return;} b();}", |
| 109 |
"function f(){if(a()){}else{return;b()}}"); |
| 110 |
fold("function f(){ if (x) return; if (y) return; if (z) return; w(); }", |
| 111 |
" function f() {" + |
| 112 |
" if (x) {} else { if (y) {} else { if (z) {} else w(); }}" + |
| 113 |
" }"); |
| 114 |
|
| 115 |
fold("function f(){while(a())return;}", |
| 116 |
"function f(){while(a())return}"); |
| 117 |
foldSame("function f(){for(x in a())return}"); |
| 118 |
|
| 119 |
fold("function f(){while(a())break;}", |
| 120 |
"function f(){while(a())break}"); |
| 121 |
foldSame("function f(){for(x in a())break}"); |
| 122 |
|
| 123 |
fold("function f(){try{return;}catch(e){return;}finally{return}}", |
| 124 |
"function f(){try{}catch(e){}finally{}}"); |
| 125 |
fold("function f(){try{return;}catch(e){return;}}", |
| 126 |
"function f(){try{}catch(e){}}"); |
| 127 |
fold("function f(){try{return;}finally{return;}}", |
| 128 |
"function f(){try{}finally{}}"); |
| 129 |
fold("function f(){try{if(a()){return;}else{return;} return;}catch(e){}}", |
| 130 |
"function f(){try{if(a()){}else{}}catch(e){}}"); |
| 131 |
|
| 132 |
fold("function f(){g:return}", |
| 133 |
"function f(){}"); |
| 134 |
fold("function f(){g:if(a()){return;}else{return;} return;}", |
| 135 |
"function f(){g:if(a()){}else{}}"); |
| 136 |
fold("function f(){try{g:if(a()){} return;}finally{return}}", |
| 137 |
"function f(){try{g:if(a()){}}finally{}}"); |
| 138 |
} |
| 139 |
|
| 140 |
public void testWhileContinueOptimization() throws Exception { |
| 141 |
fold("while(true){if(x)continue; x=3; continue; }", |
| 142 |
"while(true)if(x);else x=3"); |
| 143 |
foldSame("while(true){a();continue;b();}"); |
| 144 |
fold("while(true){if(true){a();continue;}else;b();}", |
| 145 |
"while(true){if(true){a();}else{b()}}"); |
| 146 |
fold("while(true){if(false){a();continue;}else;b();continue;}", |
| 147 |
"while(true){if(false){a()}else{b();}}"); |
| 148 |
fold("while(true){if(a()){b();continue;}else;c();}", |
| 149 |
"while(true){if(a()){b();}else{c();}}"); |
| 150 |
fold("while(true){if(a()){b();}else{c();continue;}}", |
| 151 |
"while(true){if(a()){b();}else{c();}}"); |
| 152 |
fold("while(true){if(a()){b();continue;}else;}", |
| 153 |
"while(true){if(a()){b();}else;}"); |
| 154 |
fold("while(true){if(a()){continue;}else{continue;} continue;}", |
| 155 |
"while(true){if(a()){}else{}}"); |
| 156 |
fold("while(true){if(a()){continue;}else{continue;} b();}", |
| 157 |
"while(true){if(a()){}else{continue;b();}}"); |
| 158 |
|
| 159 |
fold("while(true)while(a())continue;", |
| 160 |
"while(true)while(a());"); |
| 161 |
fold("while(true)for(x in a())continue", |
| 162 |
"while(true)for(x in a());"); |
| 163 |
|
| 164 |
fold("while(true)while(a())break;", |
| 165 |
"while(true)while(a())break"); |
| 166 |
fold("while(true)for(x in a())break", |
| 167 |
"while(true)for(x in a())break"); |
| 168 |
|
| 169 |
fold("while(true){try{continue;}catch(e){continue;}}", |
| 170 |
"while(true){try{}catch(e){}}"); |
| 171 |
fold("while(true){try{if(a()){continue;}else{continue;}" + |
| 172 |
"continue;}catch(e){}}", |
| 173 |
"while(true){try{if(a()){}else{}}catch(e){}}"); |
| 174 |
|
| 175 |
fold("while(true){g:continue}", |
| 176 |
"while(true){}"); |
| 177 |
|
| 178 |
fold("while(true){g:if(a()){continue;}else{continue;} continue;}", |
| 179 |
"while(true){g:if(a());else;}"); |
| 180 |
} |
| 181 |
|
| 182 |
public void testDoContinueOptimization() throws Exception { |
| 183 |
fold("do{if(x)continue; x=3; continue; }while(true)", |
| 184 |
"do if(x); else x=3; while(true)"); |
| 185 |
foldSame("do{a();continue;b()}while(true)"); |
| 186 |
fold("do{if(true){a();continue;}else;b();}while(true)", |
| 187 |
"do{if(true){a();}else{b();}}while(true)"); |
| 188 |
fold("do{if(false){a();continue;}else;b();continue;}while(true)", |
| 189 |
"do{if(false){a();}else{b();}}while(true)"); |
| 190 |
fold("do{if(a()){b();continue;}else;c();}while(true)", |
| 191 |
"do{if(a()){b();}else{c()}}while(true)"); |
| 192 |
fold("do{if(a()){b();}else{c();continue;}}while(true)", |
| 193 |
"do{if(a()){b();}else{c();}}while(true)"); |
| 194 |
fold("do{if(a()){b();continue;}else;}while(true)", |
| 195 |
"do{if(a()){b();}else;}while(true)"); |
| 196 |
fold("do{if(a()){continue;}else{continue;} continue;}while(true)", |
| 197 |
"do{if(a()){}else{}}while(true)"); |
| 198 |
fold("do{if(a()){continue;}else{continue;} b();}while(true)", |
| 199 |
"do{if(a()){}else{continue; b();}}while(true)"); |
| 200 |
|
| 201 |
fold("do{while(a())continue;}while(true)", |
| 202 |
"do while(a());while(true)"); |
| 203 |
fold("do{for(x in a())continue}while(true)", |
| 204 |
"do for(x in a());while(true)"); |
| 205 |
|
| 206 |
fold("do{while(a())break;}while(true)", |
| 207 |
"do while(a())break;while(true)"); |
| 208 |
fold("do for(x in a())break;while(true)", |
| 209 |
"do for(x in a())break;while(true)"); |
| 210 |
|
| 211 |
fold("do{try{continue;}catch(e){continue;}}while(true)", |
| 212 |
"do{try{}catch(e){}}while(true)"); |
| 213 |
fold("do{try{if(a()){continue;}else{continue;}" + |
| 214 |
"continue;}catch(e){}}while(true)", |
| 215 |
"do{try{if(a()){}else{}}catch(e){}}while(true)"); |
| 216 |
|
| 217 |
fold("do{g:continue}while(true)", |
| 218 |
"do{}while(true)"); |
| 219 |
|
| 220 |
fold("do{g:if(a()){continue;}else{continue;} continue;}while(true)", |
| 221 |
"do{g:if(a());else;}while(true)"); |
| 222 |
|
| 223 |
fold("do { foo(); continue; } while(false)", |
| 224 |
"do { foo(); } while(false)"); |
| 225 |
fold("do { foo(); break; } while(false)", |
| 226 |
"do { foo(); } while(false)"); |
| 227 |
} |
| 228 |
|
| 229 |
public void testForContinueOptimization() throws Exception { |
| 230 |
fold("for(x in y){if(x)continue; x=3; continue; }", |
| 231 |
"for(x in y)if(x);else x=3"); |
| 232 |
foldSame("for(x in y){a();continue;b()}"); |
| 233 |
fold("for(x in y){if(true){a();continue;}else;b();}", |
| 234 |
"for(x in y){if(true)a();else b();}"); |
| 235 |
fold("for(x in y){if(false){a();continue;}else;b();continue;}", |
| 236 |
"for(x in y){if(false){a();}else{b()}}"); |
| 237 |
fold("for(x in y){if(a()){b();continue;}else;c();}", |
| 238 |
"for(x in y){if(a()){b();}else{c();}}"); |
| 239 |
fold("for(x in y){if(a()){b();}else{c();continue;}}", |
| 240 |
"for(x in y){if(a()){b();}else{c();}}"); |
| 241 |
fold("for(x=0;x<y;x++){if(a()){b();continue;}else;}", |
| 242 |
"for(x=0;x<y;x++){if(a()){b();}else;}"); |
| 243 |
fold("for(x=0;x<y;x++){if(a()){continue;}else{continue;} continue;}", |
| 244 |
"for(x=0;x<y;x++){if(a()){}else{}}"); |
| 245 |
fold("for(x=0;x<y;x++){if(a()){continue;}else{continue;} b();}", |
| 246 |
"for(x=0;x<y;x++){if(a()){}else{continue; b();}}"); |
| 247 |
|
| 248 |
fold("for(x=0;x<y;x++)while(a())continue;", |
| 249 |
"for(x=0;x<y;x++)while(a());"); |
| 250 |
fold("for(x=0;x<y;x++)for(x in a())continue", |
| 251 |
"for(x=0;x<y;x++)for(x in a());"); |
| 252 |
|
| 253 |
fold("for(x=0;x<y;x++)while(a())break;", |
| 254 |
"for(x=0;x<y;x++)while(a())break"); |
| 255 |
foldSame("for(x=0;x<y;x++)for(x in a())break"); |
| 256 |
|
| 257 |
fold("for(x=0;x<y;x++){try{continue;}catch(e){continue;}}", |
| 258 |
"for(x=0;x<y;x++){try{}catch(e){}}"); |
| 259 |
fold("for(x=0;x<y;x++){try{if(a()){continue;}else{continue;}" + |
| 260 |
"continue;}catch(e){}}", |
| 261 |
"for(x=0;x<y;x++){try{if(a()){}else{}}catch(e){}}"); |
| 262 |
|
| 263 |
fold("for(x=0;x<y;x++){g:continue}", |
| 264 |
"for(x=0;x<y;x++){}"); |
| 265 |
|
| 266 |
fold("for(x=0;x<y;x++){g:if(a()){continue;}else{continue;} continue;}", |
| 267 |
"for(x=0;x<y;x++){g:if(a());else;}"); |
| 268 |
} |
| 269 |
|
| 270 |
public void testCodeMotionDoesntBreakFunctionHoisting() throws Exception { |
| 271 |
fold("function f() { if (x) return; foo(); function foo() {} }", |
| 272 |
"function f() { if (x); else { function foo() {} foo(); } }"); |
| 273 |
} |
| 274 |
} |