| 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.common.collect.Lists; |
| 20 |
import com.google.javascript.jscomp.ControlFlowGraph.Branch; |
| 21 |
import com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge; |
| 22 |
import com.google.javascript.jscomp.graph.DiGraph.DiGraphNode; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
import com.google.javascript.rhino.Token; |
| 25 |
|
| 26 |
import junit.framework.TestCase; |
| 27 |
|
| 28 |
import java.util.Collections; |
| 29 |
import java.util.Iterator; |
| 30 |
import java.util.List; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class ControlFlowAnalysisTest extends TestCase { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private void testCfg(String input, String expected) { |
| 48 |
testCfg(input, expected, true); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private static List<DiGraphEdge<Node, Branch>> getAllEdges( |
| 55 |
ControlFlowGraph<Node> cfg) { |
| 56 |
List<DiGraphEdge<Node, Branch>> edges = Lists.newArrayList(); |
| 57 |
for (DiGraphNode<Node, Branch> n : cfg.getDirectedGraphNodes()) { |
| 58 |
for (DiGraphEdge<Node, Branch> e : cfg.getOutEdges(n.getValue())) { |
| 59 |
edges.add(e); |
| 60 |
} |
| 61 |
} |
| 62 |
return edges; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private static List<DiGraphEdge<Node, Branch>> getAllEdges( |
| 70 |
ControlFlowGraph<Node> cfg, int startToken, int endToken) { |
| 71 |
List<DiGraphEdge<Node, Branch>> edges = getAllEdges(cfg); |
| 72 |
Iterator<DiGraphEdge<Node, Branch>> it = edges.iterator(); |
| 73 |
while (it.hasNext()) { |
| 74 |
DiGraphEdge<Node, Branch> edge = it.next(); |
| 75 |
Node startNode = edge.getSource().getValue(); |
| 76 |
Node endNode = edge.getDestination().getValue(); |
| 77 |
if (startNode == null || endNode == null || |
| 78 |
startNode.getType() != startToken || endNode.getType() != endToken) { |
| 79 |
it.remove(); |
| 80 |
} |
| 81 |
} |
| 82 |
return edges; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private static List<DiGraphEdge<Node, Branch>> getAllEdges( |
| 90 |
ControlFlowGraph<Node> cfg, int startToken, int endToken, Branch type) { |
| 91 |
List<DiGraphEdge<Node, Branch>> edges = |
| 92 |
getAllEdges(cfg, startToken, endToken); |
| 93 |
Iterator<DiGraphEdge<Node, Branch>> it = edges.iterator(); |
| 94 |
while (it.hasNext()) { |
| 95 |
if (type != it.next().getValue()) { |
| 96 |
it.remove(); |
| 97 |
} |
| 98 |
} |
| 99 |
return edges; |
| 100 |
} |
| 101 |
|
| 102 |
private static boolean isAncestor(Node n, Node maybeDescendent) { |
| 103 |
for (Node current = n.getFirstChild(); current != null; |
| 104 |
current = current.getNext()) { |
| 105 |
if (current == maybeDescendent || |
| 106 |
isAncestor(current, maybeDescendent)) { |
| 107 |
return true; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
private static List<DiGraphEdge<Node, Branch>> getAllDownEdges( |
| 120 |
ControlFlowGraph<Node> cfg, int startToken, int endToken, Branch type) { |
| 121 |
List<DiGraphEdge<Node, Branch>> edges = |
| 122 |
getAllEdges(cfg, startToken, endToken, type); |
| 123 |
Iterator<DiGraphEdge<Node, Branch>> it = edges.iterator(); |
| 124 |
while (it.hasNext()) { |
| 125 |
DiGraphEdge<Node, Branch> edge = it.next(); |
| 126 |
Node source = edge.getSource().getValue(); |
| 127 |
Node dest = edge.getDestination().getValue(); |
| 128 |
if (!isAncestor(source, dest)) { |
| 129 |
it.remove(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return edges; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private static void assertNoEdge(ControlFlowGraph<Node> cfg, int startToken, |
| 141 |
int endToken) { |
| 142 |
assertEquals(0, getAllEdges(cfg, startToken, endToken).size()); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
private static void assertDownEdge(ControlFlowGraph<Node> cfg, |
| 151 |
int startToken, int endToken, Branch type) { |
| 152 |
assertTrue("No down edge found", |
| 153 |
0 != getAllDownEdges(cfg, startToken, endToken, type).size()); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
private static void assertUpEdge(ControlFlowGraph<Node> cfg, |
| 162 |
int startToken, int endToken, Branch type) { |
| 163 |
assertTrue("No up edge found", |
| 164 |
0 != getAllDownEdges(cfg, endToken, startToken, type).size()); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
private static void assertCrossEdge(ControlFlowGraph<Node> cfg, |
| 173 |
int startToken, int endToken, Branch type) { |
| 174 |
int numDownEdges = getAllDownEdges(cfg, startToken, endToken, type).size(); |
| 175 |
int numUpEdges = getAllDownEdges(cfg, endToken, startToken, type).size(); |
| 176 |
int numEdges = getAllEdges(cfg, startToken, endToken, type).size(); |
| 177 |
assertTrue("No cross edges found", numDownEdges + numUpEdges < numEdges); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
private static void assertReturnEdge(ControlFlowGraph<Node> cfg, |
| 185 |
int startToken) { |
| 186 |
List<DiGraphEdge<Node, Branch>> edges = getAllEdges(cfg); |
| 187 |
for (DiGraphEdge<Node, Branch> edge : edges) { |
| 188 |
Node source = edge.getSource().getValue(); |
| 189 |
DiGraphNode<Node, Branch> dest = edge.getDestination(); |
| 190 |
if (source.getType() == startToken && |
| 191 |
cfg.isImplicitReturn(dest)) { |
| 192 |
return; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
fail("No return edge found"); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
private static void assertNoReturnEdge(ControlFlowGraph<Node> cfg, |
| 204 |
int startToken) { |
| 205 |
List<DiGraphEdge<Node, Branch>> edges = getAllEdges(cfg); |
| 206 |
for (DiGraphEdge<Node, Branch> edge : edges) { |
| 207 |
Node source = edge.getSource().getValue(); |
| 208 |
DiGraphNode<Node, Branch> dest = edge.getDestination(); |
| 209 |
if (source.getType() == startToken) { |
| 210 |
assertTrue("Token " + startToken + " should not have an out going" + |
| 211 |
" edge to the implicit return", !cfg.isImplicitReturn(dest)); |
| 212 |
return; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
private ControlFlowGraph<Node> createCfg(String input, |
| 223 |
boolean runSynBlockPass) { |
| 224 |
Compiler compiler = new Compiler(); |
| 225 |
ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, true, true); |
| 226 |
|
| 227 |
Node root = compiler.parseSyntheticCode("cfgtest", input); |
| 228 |
if (runSynBlockPass) { |
| 229 |
CreateSyntheticBlocks pass = new CreateSyntheticBlocks( |
| 230 |
compiler, "START", "END"); |
| 231 |
pass.process(null, root); |
| 232 |
} |
| 233 |
cfa.process(null, root); |
| 234 |
return cfa.getCfg(); |
| 235 |
} |
| 236 |
|
| 237 |
private ControlFlowGraph<Node> createCfg(String input) { |
| 238 |
return createCfg(input, false); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
private void testCfg(String input, String expected, |
| 253 |
boolean shouldTraverseFunctions) { |
| 254 |
Compiler compiler = new Compiler(); |
| 255 |
ControlFlowAnalysis cfa = |
| 256 |
new ControlFlowAnalysis(compiler, shouldTraverseFunctions, true); |
| 257 |
|
| 258 |
Node root = compiler.parseSyntheticCode("cfgtest", input); |
| 259 |
cfa.process(null, root); |
| 260 |
ControlFlowGraph<Node> cfg = cfa.getCfg(); |
| 261 |
try { |
| 262 |
assertEquals(expected, DotFormatter.toDot(root, cfg)); |
| 263 |
} catch (java.io.IOException e) { |
| 264 |
fail("Tests failed with IOExceptions"); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
public void testSimpleStatements() { |
| 269 |
String src = "var a; a = a; a = a"; |
| 270 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 271 |
assertDownEdge(cfg, Token.SCRIPT, Token.VAR, Branch.UNCOND); |
| 272 |
assertCrossEdge(cfg, Token.VAR, Token.EXPR_RESULT, Branch.UNCOND); |
| 273 |
assertCrossEdge(cfg, Token.EXPR_RESULT, Token.EXPR_RESULT, Branch.UNCOND); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
public void testSimpleIf() { |
| 278 |
String src = "var x; if (x) { x() } else { x() };"; |
| 279 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 280 |
assertDownEdge(cfg, Token.SCRIPT, Token.VAR, Branch.UNCOND); |
| 281 |
assertCrossEdge(cfg, Token.VAR, Token.IF, Branch.UNCOND); |
| 282 |
assertDownEdge(cfg, Token.IF, Token.BLOCK, Branch.ON_TRUE); |
| 283 |
assertDownEdge(cfg, Token.BLOCK, Token.EXPR_RESULT, Branch.UNCOND); |
| 284 |
assertNoEdge(cfg, Token.EXPR_RESULT, Token.CALL); |
| 285 |
assertDownEdge(cfg, Token.IF, Token.BLOCK, Branch.ON_FALSE); |
| 286 |
assertReturnEdge(cfg, Token.EMPTY); |
| 287 |
} |
| 288 |
|
| 289 |
public void testBreakingBlock() { |
| 290 |
|
| 291 |
String src = "X: { while(1) { break } }"; |
| 292 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 293 |
assertUpEdge(cfg, Token.BREAK, Token.BLOCK, Branch.UNCOND); |
| 294 |
} |
| 295 |
|
| 296 |
public void testBreakingTryBlock() { |
| 297 |
String src = "a: try { break a; } finally {} if(x) {}"; |
| 298 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 299 |
assertCrossEdge(cfg, Token.BREAK, Token.IF, Branch.UNCOND); |
| 300 |
|
| 301 |
src = "a: try {} finally {break a;} if(x) {}"; |
| 302 |
cfg = createCfg(src); |
| 303 |
assertCrossEdge(cfg, Token.BREAK, Token.IF, Branch.UNCOND); |
| 304 |
|
| 305 |
src = "a: try {} catch(e) {break a;} if(x) {}"; |
| 306 |
cfg = createCfg(src); |
| 307 |
assertCrossEdge(cfg, Token.BREAK, Token.IF, Branch.UNCOND); |
| 308 |
} |
| 309 |
|
| 310 |
public void testWithStatement() { |
| 311 |
String src = "var x, y; with(x) { y() }"; |
| 312 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 313 |
assertDownEdge(cfg, Token.WITH, Token.BLOCK, Branch.UNCOND); |
| 314 |
assertNoEdge(cfg, Token.WITH, Token.NAME); |
| 315 |
assertNoEdge(cfg, Token.NAME, Token.BLOCK); |
| 316 |
assertDownEdge(cfg, Token.BLOCK, Token.EXPR_RESULT, Branch.UNCOND); |
| 317 |
assertReturnEdge(cfg, Token.EXPR_RESULT); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
public void testSimpleWhile() { |
| 322 |
String src = "var x; while (x) { x(); if (x) { break; } x() }"; |
| 323 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 324 |
assertDownEdge(cfg, Token.WHILE, Token.BLOCK, Branch.ON_TRUE); |
| 325 |
assertDownEdge(cfg, Token.BLOCK, Token.EXPR_RESULT, Branch.UNCOND); |
| 326 |
assertDownEdge(cfg, Token.IF, Token.BLOCK, Branch.ON_TRUE); |
| 327 |
assertReturnEdge(cfg, Token.BREAK); |
| 328 |
} |
| 329 |
|
| 330 |
public void testSimpleSwitch() { |
| 331 |
String src = "var x; switch(x){ case(1): x(); case('x'): x(); break" + |
| 332 |
"; default: x();}"; |
| 333 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 334 |
assertCrossEdge(cfg, Token.VAR, Token.SWITCH, Branch.UNCOND); |
| 335 |
assertNoEdge(cfg, Token.SWITCH, Token.NAME); |
| 336 |
|
| 337 |
assertDownEdge(cfg, Token.SWITCH, Token.CASE, Branch.UNCOND); |
| 338 |
assertCrossEdge(cfg, Token.CASE, Token.CASE, Branch.ON_FALSE); |
| 339 |
assertCrossEdge(cfg, Token.CASE, Token.DEFAULT_CASE, Branch.ON_FALSE); |
| 340 |
|
| 341 |
assertDownEdge(cfg, Token.CASE, Token.BLOCK, Branch.ON_TRUE); |
| 342 |
assertDownEdge(cfg, Token.BLOCK, Token.EXPR_RESULT, Branch.UNCOND); |
| 343 |
assertNoEdge(cfg, Token.EXPR_RESULT, Token.CALL); |
| 344 |
assertNoEdge(cfg, Token.CALL, Token.NAME); |
| 345 |
} |
| 346 |
|
| 347 |
public void testSimpleNoDefault() { |
| 348 |
String src = "var x; switch(x){ case(1): break; } x();"; |
| 349 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 350 |
assertCrossEdge(cfg, Token.CASE, Token.EXPR_RESULT, Branch.ON_FALSE); |
| 351 |
} |
| 352 |
|
| 353 |
public void testSwitchDefaultFirst() { |
| 354 |
|
| 355 |
String src = "var x; switch(x){ default: break; case 1: break; }"; |
| 356 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 357 |
assertDownEdge(cfg, Token.SWITCH, Token.CASE, Branch.UNCOND); |
| 358 |
assertCrossEdge(cfg, Token.CASE, Token.DEFAULT_CASE, Branch.ON_FALSE); |
| 359 |
} |
| 360 |
|
| 361 |
public void testSwitchDefaultInMiddle() { |
| 362 |
|
| 363 |
String src = "var x; switch(x){ case 1: break; default: break; " + |
| 364 |
"case 2: break; }"; |
| 365 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 366 |
assertDownEdge(cfg, Token.SWITCH, Token.CASE, Branch.UNCOND); |
| 367 |
assertCrossEdge(cfg, Token.CASE, Token.CASE, Branch.ON_FALSE); |
| 368 |
assertCrossEdge(cfg, Token.CASE, Token.DEFAULT_CASE, Branch.ON_FALSE); |
| 369 |
} |
| 370 |
|
| 371 |
public void testSwitchEmpty() { |
| 372 |
|
| 373 |
String src = "var x; switch(x){}; x()"; |
| 374 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 375 |
assertCrossEdge(cfg, Token.SWITCH, Token.EMPTY, Branch.UNCOND); |
| 376 |
assertCrossEdge(cfg, Token.EMPTY, Token.EXPR_RESULT, Branch.UNCOND); |
| 377 |
} |
| 378 |
|
| 379 |
public void testReturnThrowingException() { |
| 380 |
String src = "function f() {try { return a(); } catch (e) {e()}}"; |
| 381 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 382 |
assertCrossEdge(cfg, Token.RETURN, Token.BLOCK, Branch.ON_EX); |
| 383 |
assertDownEdge(cfg, Token.BLOCK, Token.CATCH, Branch.UNCOND); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
public void testSimpleFor() { |
| 388 |
String src = "var a; for (var x = 0; x < 100; x++) { a(); }"; |
| 389 |
String expected = "digraph AST {\n" + |
| 390 |
" node [color=lightblue2, style=filled];\n" + |
| 391 |
" node0 [label=\"SCRIPT\"];\n" + |
| 392 |
" node1 [label=\"VAR\"];\n" + |
| 393 |
" node0 -> node1 [weight=1];\n" + |
| 394 |
" node2 [label=\"NAME\"];\n" + |
| 395 |
" node1 -> node2 [weight=1];\n" + |
| 396 |
" node3 [label=\"VAR\"];\n" + |
| 397 |
" node1 -> node3 " + |
| 398 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 399 |
" node4 [label=\"FOR\"];\n" + |
| 400 |
" node0 -> node4 [weight=1];\n" + |
| 401 |
" node4 -> node3 [weight=1];\n" + |
| 402 |
" node5 [label=\"NAME\"];\n" + |
| 403 |
" node3 -> node5 [weight=1];\n" + |
| 404 |
" node6 [label=\"NUMBER\"];\n" + |
| 405 |
" node5 -> node6 [weight=1];\n" + |
| 406 |
" node3 -> node4 " + |
| 407 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 408 |
" node7 [label=\"LT\"];\n" + |
| 409 |
" node4 -> node7 [weight=1];\n" + |
| 410 |
" node8 [label=\"NAME\"];\n" + |
| 411 |
" node7 -> node8 [weight=1];\n" + |
| 412 |
" node9 [label=\"NUMBER\"];\n" + |
| 413 |
" node7 -> node9 [weight=1];\n" + |
| 414 |
" node10 [label=\"INC\"];\n" + |
| 415 |
" node4 -> node10 [weight=1];\n" + |
| 416 |
" node11 [label=\"NAME\"];\n" + |
| 417 |
" node10 -> node11 [weight=1];\n" + |
| 418 |
" node10 -> node4 " + |
| 419 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 420 |
" node12 [label=\"BLOCK\"];\n" + |
| 421 |
" node4 -> node12 [weight=1];\n" + |
| 422 |
" node13 [label=\"EXPR_RESULT\"];\n" + |
| 423 |
" node12 -> node13 [weight=1];\n" + |
| 424 |
" node14 [label=\"CALL\"];\n" + |
| 425 |
" node13 -> node14 [weight=1];\n" + |
| 426 |
" node15 [label=\"NAME\"];\n" + |
| 427 |
" node14 -> node15 [weight=1];\n" + |
| 428 |
" node13 -> node10 " + |
| 429 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 430 |
" node12 -> node13 " + |
| 431 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 432 |
" node4 -> RETURN " + |
| 433 |
"[label=\"ON_FALSE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 434 |
" node4 -> node12 " + |
| 435 |
"[label=\"ON_TRUE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 436 |
" node0 -> node1 " + |
| 437 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 438 |
"}\n"; |
| 439 |
testCfg(src, expected); |
| 440 |
} |
| 441 |
|
| 442 |
public void testSimpleForWithContinue() { |
| 443 |
String src = "var a; for (var x = 0; x < 100; x++) {a();continue;a()}"; |
| 444 |
String expected = "digraph AST {\n" + |
| 445 |
" node [color=lightblue2, style=filled];\n" + |
| 446 |
" node0 [label=\"SCRIPT\"];\n" + |
| 447 |
" node1 [label=\"VAR\"];\n" + |
| 448 |
" node0 -> node1 [weight=1];\n" + |
| 449 |
" node2 [label=\"NAME\"];\n" + |
| 450 |
" node1 -> node2 [weight=1];\n" + |
| 451 |
" node3 [label=\"VAR\"];\n" + |
| 452 |
" node1 -> node3 " + |
| 453 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 454 |
" node4 [label=\"FOR\"];\n" + |
| 455 |
" node0 -> node4 [weight=1];\n" + |
| 456 |
" node4 -> node3 [weight=1];\n" + |
| 457 |
" node5 [label=\"NAME\"];\n" + |
| 458 |
" node3 -> node5 [weight=1];\n" + |
| 459 |
" node6 [label=\"NUMBER\"];\n" + |
| 460 |
" node5 -> node6 [weight=1];\n" + |
| 461 |
" node3 -> node4 " + |
| 462 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 463 |
" node7 [label=\"LT\"];\n" + |
| 464 |
" node4 -> node7 [weight=1];\n" + |
| 465 |
" node8 [label=\"NAME\"];\n" + |
| 466 |
" node7 -> node8 [weight=1];\n" + |
| 467 |
" node9 [label=\"NUMBER\"];\n" + |
| 468 |
" node7 -> node9 [weight=1];\n" + |
| 469 |
" node10 [label=\"INC\"];\n" + |
| 470 |
" node4 -> node10 [weight=1];\n" + |
| 471 |
" node11 [label=\"NAME\"];\n" + |
| 472 |
" node10 -> node11 [weight=1];\n" + |
| 473 |
" node10 -> node4 " + |
| 474 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 475 |
" node12 [label=\"BLOCK\"];\n" + |
| 476 |
" node4 -> node12 [weight=1];\n" + |
| 477 |
" node13 [label=\"EXPR_RESULT\"];\n" + |
| 478 |
" node12 -> node13 [weight=1];\n" + |
| 479 |
" node14 [label=\"CALL\"];\n" + |
| 480 |
" node13 -> node14 [weight=1];\n" + |
| 481 |
" node15 [label=\"NAME\"];\n" + |
| 482 |
" node14 -> node15 [weight=1];\n" + |
| 483 |
" node16 [label=\"CONTINUE\"];\n" + |
| 484 |
" node13 -> node16 " + |
| 485 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 486 |
" node12 -> node16 [weight=1];\n" + |
| 487 |
" node16 -> node10 " + |
| 488 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 489 |
" node17 [label=\"EXPR_RESULT\"];\n" + |
| 490 |
" node12 -> node17 [weight=1];\n" + |
| 491 |
" node18 [label=\"CALL\"];\n" + |
| 492 |
" node17 -> node18 [weight=1];\n" + |
| 493 |
" node19 [label=\"NAME\"];\n" + |
| 494 |
" node18 -> node19 [weight=1];\n" + |
| 495 |
" node17 -> node10 " + |
| 496 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 497 |
" node12 -> node13 " + |
| 498 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 499 |
" node4 -> RETURN " + |
| 500 |
"[label=\"ON_FALSE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 501 |
" node4 -> node12 " + |
| 502 |
"[label=\"ON_TRUE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 503 |
" node0 -> node1 " + |
| 504 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 505 |
"}\n"; |
| 506 |
testCfg(src, expected); |
| 507 |
} |
| 508 |
|
| 509 |
public void testNestedFor() { |
| 510 |
|
| 511 |
String src = "var a,b;a();for(var x=0;x<100;x++){for(var y=0;y<100;y++){" + |
| 512 |
"continue;b();}}"; |
| 513 |
String expected = "digraph AST {\n" + |
| 514 |
" node [color=lightblue2, style=filled];\n" + |
| 515 |
" node0 [label=\"SCRIPT\"];\n" + |
| 516 |
" node1 [label=\"VAR\"];\n" + |
| 517 |
" node0 -> node1 [weight=1];\n" + |
| 518 |
" node2 [label=\"NAME\"];\n" + |
| 519 |
" node1 -> node2 [weight=1];\n" + |
| 520 |
" node3 [label=\"NAME\"];\n" + |
| 521 |
" node1 -> node3 [weight=1];\n" + |
| 522 |
" node4 [label=\"EXPR_RESULT\"];\n" + |
| 523 |
" node1 -> node4 " + |
| 524 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 525 |
" node0 -> node4 [weight=1];\n" + |
| 526 |
" node5 [label=\"CALL\"];\n" + |
| 527 |
" node4 -> node5 [weight=1];\n" + |
| 528 |
" node6 [label=\"NAME\"];\n" + |
| 529 |
" node5 -> node6 [weight=1];\n" + |
| 530 |
" node7 [label=\"VAR\"];\n" + |
| 531 |
" node4 -> node7 " + |
| 532 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 533 |
" node8 [label=\"FOR\"];\n" + |
| 534 |
" node0 -> node8 [weight=1];\n" + |
| 535 |
" node8 -> node7 [weight=1];\n" + |
| 536 |
" node9 [label=\"NAME\"];\n" + |
| 537 |
" node7 -> node9 [weight=1];\n" + |
| 538 |
" node10 [label=\"NUMBER\"];\n" + |
| 539 |
" node9 -> node10 [weight=1];\n" + |
| 540 |
" node7 -> node8 " + |
| 541 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 542 |
" node11 [label=\"LT\"];\n" + |
| 543 |
" node8 -> node11 [weight=1];\n" + |
| 544 |
" node12 [label=\"NAME\"];\n" + |
| 545 |
" node11 -> node12 [weight=1];\n" + |
| 546 |
" node13 [label=\"NUMBER\"];\n" + |
| 547 |
" node11 -> node13 [weight=1];\n" + |
| 548 |
" node14 [label=\"INC\"];\n" + |
| 549 |
" node8 -> node14 [weight=1];\n" + |
| 550 |
" node15 [label=\"NAME\"];\n" + |
| 551 |
" node14 -> node15 [weight=1];\n" + |
| 552 |
" node14 -> node8 " + |
| 553 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 554 |
" node16 [label=\"BLOCK\"];\n" + |
| 555 |
" node8 -> node16 [weight=1];\n" + |
| 556 |
" node17 [label=\"FOR\"];\n" + |
| 557 |
" node16 -> node17 [weight=1];\n" + |
| 558 |
" node18 [label=\"VAR\"];\n" + |
| 559 |
" node17 -> node18 [weight=1];\n" + |
| 560 |
" node19 [label=\"NAME\"];\n" + |
| 561 |
" node18 -> node19 [weight=1];\n" + |
| 562 |
" node20 [label=\"NUMBER\"];\n" + |
| 563 |
" node19 -> node20 [weight=1];\n" + |
| 564 |
" node18 -> node17 " + |
| 565 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 566 |
" node21 [label=\"LT\"];\n" + |
| 567 |
" node17 -> node21 [weight=1];\n" + |
| 568 |
" node22 [label=\"NAME\"];\n" + |
| 569 |
" node21 -> node22 [weight=1];\n" + |
| 570 |
" node23 [label=\"NUMBER\"];\n" + |
| 571 |
" node21 -> node23 [weight=1];\n" + |
| 572 |
" node24 [label=\"INC\"];\n" + |
| 573 |
" node17 -> node24 [weight=1];\n" + |
| 574 |
" node25 [label=\"NAME\"];\n" + |
| 575 |
" node24 -> node25 [weight=1];\n" + |
| 576 |
" node24 -> node17 " + |
| 577 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 578 |
" node26 [label=\"BLOCK\"];\n" + |
| 579 |
" node17 -> node26 [weight=1];\n" + |
| 580 |
" node27 [label=\"CONTINUE\"];\n" + |
| 581 |
" node26 -> node27 [weight=1];\n" + |
| 582 |
" node27 -> node24 " + |
| 583 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 584 |
" node28 [label=\"EXPR_RESULT\"];\n" + |
| 585 |
" node26 -> node28 [weight=1];\n" + |
| 586 |
" node29 [label=\"CALL\"];\n" + |
| 587 |
" node28 -> node29 [weight=1];\n" + |
| 588 |
" node30 [label=\"NAME\"];\n" + |
| 589 |
" node29 -> node30 [weight=1];\n" + |
| 590 |
" node28 -> node24 " + |
| 591 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 592 |
" node26 -> node27 " + |
| 593 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 594 |
" node17 -> node14 " + |
| 595 |
"[label=\"ON_FALSE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 596 |
" node17 -> node26 " + |
| 597 |
"[label=\"ON_TRUE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 598 |
" node16 -> node18 " + |
| 599 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 600 |
" node8 -> RETURN " + |
| 601 |
"[label=\"ON_FALSE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 602 |
" node8 -> node16 " + |
| 603 |
"[label=\"ON_TRUE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 604 |
" node0 -> node1 " + |
| 605 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 606 |
"}\n"; |
| 607 |
testCfg(src, expected); |
| 608 |
} |
| 609 |
|
| 610 |
public void testNestedDoWithBreak() { |
| 611 |
|
| 612 |
String src = "var a;do{do{break}while(a);do{a()}while(a)}while(a);"; |
| 613 |
String expected = "digraph AST {\n" + |
| 614 |
" node [color=lightblue2, style=filled];\n" + |
| 615 |
" node0 [label=\"SCRIPT\"];\n" + |
| 616 |
" node1 [label=\"VAR\"];\n" + |
| 617 |
" node0 -> node1 [weight=1];\n" + |
| 618 |
" node2 [label=\"NAME\"];\n" + |
| 619 |
" node1 -> node2 [weight=1];\n" + |
| 620 |
" node3 [label=\"BLOCK\"];\n" + |
| 621 |
" node1 -> node3 " + |
| 622 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 623 |
" node4 [label=\"DO\"];\n" + |
| 624 |
" node0 -> node4 [weight=1];\n" + |
| 625 |
" node4 -> node3 [weight=1];\n" + |
| 626 |
" node5 [label=\"DO\"];\n" + |
| 627 |
" node3 -> node5 [weight=1];\n" + |
| 628 |
" node6 [label=\"BLOCK\"];\n" + |
| 629 |
" node5 -> node6 [weight=1];\n" + |
| 630 |
" node7 [label=\"BREAK\"];\n" + |
| 631 |
" node6 -> node7 [weight=1];\n" + |
| 632 |
" node8 [label=\"BLOCK\"];\n" + |
| 633 |
" node7 -> node8 " + |
| 634 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 635 |
" node6 -> node7 " + |
| 636 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 637 |
" node9 [label=\"NAME\"];\n" + |
| 638 |
" node5 -> node9 [weight=1];\n" + |
| 639 |
" node5 -> node6 " + |
| 640 |
"[label=\"ON_TRUE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 641 |
" node5 -> node8 " + |
| 642 |
"[label=\"ON_FALSE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 643 |
" node10 [label=\"DO\"];\n" + |
| 644 |
" node3 -> node10 [weight=1];\n" + |
| 645 |
" node10 -> node8 [weight=1];\n" + |
| 646 |
" node11 [label=\"EXPR_RESULT\"];\n" + |
| 647 |
" node8 -> node11 [weight=1];\n" + |
| 648 |
" node12 [label=\"CALL\"];\n" + |
| 649 |
" node11 -> node12 [weight=1];\n" + |
| 650 |
" node13 [label=\"NAME\"];\n" + |
| 651 |
" node12 -> node13 [weight=1];\n" + |
| 652 |
" node11 -> node10 " + |
| 653 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 654 |
" node8 -> node11 " + |
| 655 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 656 |
" node14 [label=\"NAME\"];\n" + |
| 657 |
" node10 -> node14 [weight=1];\n" + |
| 658 |
" node10 -> node4 " + |
| 659 |
"[label=\"ON_FALSE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 660 |
" node10 -> node8 " + |
| 661 |
"[label=\"ON_TRUE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 662 |
" node3 -> node6 " + |
| 663 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 664 |
" node15 [label=\"NAME\"];\n" + |
| 665 |
" node4 -> node15 [weight=1];\n" + |
| 666 |
" node4 -> RETURN " + |
| 667 |
"[label=\"ON_FALSE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 668 |
" node4 -> node3 " + |
| 669 |
"[label=\"ON_TRUE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 670 |
" node0 -> node1 " + |
| 671 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 672 |
"}\n"; |
| 673 |
testCfg(src, expected); |
| 674 |
} |
| 675 |
|
| 676 |
public void testForIn() { |
| 677 |
String src = "var a,b;for(a in b){a()};"; |
| 678 |
String expected = "digraph AST {\n" + |
| 679 |
" node [color=lightblue2, style=filled];\n" + |
| 680 |
" node0 [label=\"SCRIPT\"];\n" + |
| 681 |
" node1 [label=\"VAR\"];\n" + |
| 682 |
" node0 -> node1 [weight=1];\n" + |
| 683 |
" node2 [label=\"NAME\"];\n" + |
| 684 |
" node1 -> node2 [weight=1];\n" + |
| 685 |
" node3 [label=\"NAME\"];\n" + |
| 686 |
" node1 -> node3 [weight=1];\n" + |
| 687 |
" node4 [label=\"NAME\"];\n" + |
| 688 |
" node1 -> node4 [label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 689 |
" node5 [label=\"FOR\"];\n" + |
| 690 |
" node0 -> node5 [weight=1];\n" + |
| 691 |
" node6 [label=\"NAME\"];\n" + |
| 692 |
" node5 -> node6 [weight=1];\n" + |
| 693 |
" node5 -> node4 [weight=1];\n" + |
| 694 |
" node4 -> node5 [label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 695 |
" node7 [label=\"BLOCK\"];\n" + |
| 696 |
" node5 -> node7 [weight=1];\n" + |
| 697 |
" node8 [label=\"EXPR_RESULT\"];\n" + |
| 698 |
" node7 -> node8 [weight=1];\n" + |
| 699 |
" node9 [label=\"CALL\"];\n" + |
| 700 |
" node8 -> node9 [weight=1];\n" + |
| 701 |
" node10 [label=\"NAME\"];\n" + |
| 702 |
" node9 -> node10 [weight=1];\n" + |
| 703 |
" node8 -> node5 [label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 704 |
" node7 -> node8 [label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 705 |
" node11 [label=\"EMPTY\"];\n" + |
| 706 |
" node5 -> node11 [label=\"ON_FALSE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 707 |
" node5 -> node7 [label=\"ON_TRUE\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 708 |
" node0 -> node11 [weight=1];\n" + |
| 709 |
" node11 -> RETURN [label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 710 |
" node0 -> node1 [label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 711 |
"}\n"; |
| 712 |
testCfg(src, expected); |
| 713 |
} |
| 714 |
|
| 715 |
public void testThrow() { |
| 716 |
String src = "function f() { throw 1; f() }"; |
| 717 |
String expected = "digraph AST {\n" + |
| 718 |
" node [color=lightblue2, style=filled];\n" + |
| 719 |
" node0 [label=\"SCRIPT\"];\n" + |
| 720 |
" node1 [label=\"FUNCTION\"];\n" + |
| 721 |
" node0 -> node1 [weight=1];\n" + |
| 722 |
" node2 [label=\"NAME\"];\n" + |
| 723 |
" node1 -> node2 [weight=1];\n" + |
| 724 |
" node3 [label=\"PARAM_LIST\"];\n" + |
| 725 |
" node1 -> node3 [weight=1];\n" + |
| 726 |
" node4 [label=\"BLOCK\"];\n" + |
| 727 |
" node1 -> node4 [weight=1];\n" + |
| 728 |
" node5 [label=\"THROW\"];\n" + |
| 729 |
" node4 -> node5 [weight=1];\n" + |
| 730 |
" node6 [label=\"NUMBER\"];\n" + |
| 731 |
" node5 -> node6 [weight=1];\n" + |
| 732 |
" node7 [label=\"EXPR_RESULT\"];\n" + |
| 733 |
" node4 -> node7 [weight=1];\n" + |
| 734 |
" node8 [label=\"CALL\"];\n" + |
| 735 |
" node7 -> node8 [weight=1];\n" + |
| 736 |
" node9 [label=\"NAME\"];\n" + |
| 737 |
" node8 -> node9 [weight=1];\n" + |
| 738 |
" node7 -> RETURN " + |
| 739 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 740 |
" node4 -> node5 " + |
| 741 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 742 |
" node1 -> node4 " + |
| 743 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 744 |
" node0 -> RETURN " + |
| 745 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 746 |
"}\n"; |
| 747 |
testCfg(src, expected); |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
public void testSimpleFunction() { |
| 752 |
String src = "function f() { f() } f()"; |
| 753 |
String expected = "digraph AST {\n" + |
| 754 |
" node [color=lightblue2, style=filled];\n" + |
| 755 |
" node0 [label=\"SCRIPT\"];\n" + |
| 756 |
" node1 [label=\"FUNCTION\"];\n" + |
| 757 |
" node0 -> node1 [weight=1];\n" + |
| 758 |
" node2 [label=\"NAME\"];\n" + |
| 759 |
" node1 -> node2 [weight=1];\n" + |
| 760 |
" node3 [label=\"PARAM_LIST\"];\n" + |
| 761 |
" node1 -> node3 [weight=1];\n" + |
| 762 |
" node4 [label=\"BLOCK\"];\n" + |
| 763 |
" node1 -> node4 [weight=1];\n" + |
| 764 |
" node5 [label=\"EXPR_RESULT\"];\n" + |
| 765 |
" node4 -> node5 [weight=1];\n" + |
| 766 |
" node6 [label=\"CALL\"];\n" + |
| 767 |
" node5 -> node6 [weight=1];\n" + |
| 768 |
" node7 [label=\"NAME\"];\n" + |
| 769 |
" node6 -> node7 [weight=1];\n" + |
| 770 |
" node5 -> RETURN " + |
| 771 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 772 |
" node4 -> node5 " + |
| 773 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 774 |
" node1 -> node4 " + |
| 775 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 776 |
" node8 [label=\"EXPR_RESULT\"];\n" + |
| 777 |
" node0 -> node8 [weight=1];\n" + |
| 778 |
" node9 [label=\"CALL\"];\n" + |
| 779 |
" node8 -> node9 [weight=1];\n" + |
| 780 |
" node10 [label=\"NAME\"];\n" + |
| 781 |
" node9 -> node10 [weight=1];\n" + |
| 782 |
" node8 -> RETURN " + |
| 783 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 784 |
" node0 -> node8 " + |
| 785 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 786 |
"}\n"; |
| 787 |
testCfg(src, expected); |
| 788 |
} |
| 789 |
|
| 790 |
public void testSimpleCatch() { |
| 791 |
String src = "try{ throw x; x(); x['stuff']; x.x; x} catch (e) { e() }"; |
| 792 |
String expected = "digraph AST {\n" |
| 793 |
+ " node [color=lightblue2, style=filled];\n" |
| 794 |
+ " node0 [label=\"SCRIPT\"];\n" |
| 795 |
+ " node1 [label=\"TRY\"];\n" |
| 796 |
+ " node0 -> node1 [weight=1];\n" |
| 797 |
+ " node2 [label=\"BLOCK\"];\n" |
| 798 |
+ " node1 -> node2 [weight=1];\n" |
| 799 |
+ " node3 [label=\"THROW\"];\n" |
| 800 |
+ " node2 -> node3 [weight=1];\n" |
| 801 |
+ " node4 [label=\"NAME\"];\n" |
| 802 |
+ " node3 -> node4 [weight=1];\n" |
| 803 |
+ " node5 [label=\"BLOCK\"];\n" |
| 804 |
+ " node3 -> node5 [label=\"ON_EX\", " + |
| 805 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 806 |
+ " node6 [label=\"EXPR_RESULT\"];\n" |
| 807 |
+ " node2 -> node6 [weight=1];\n" |
| 808 |
+ " node7 [label=\"CALL\"];\n" |
| 809 |
+ " node6 -> node7 [weight=1];\n" |
| 810 |
+ " node8 [label=\"NAME\"];\n" |
| 811 |
+ " node7 -> node8 [weight=1];\n" |
| 812 |
+ " node9 [label=\"EXPR_RESULT\"];\n" |
| 813 |
+ " node6 -> node5 [label=\"ON_EX\", " + |
| 814 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 815 |
+ " node6 -> node9 [label=\"UNCOND\", " + |
| 816 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 817 |
+ " node2 -> node9 [weight=1];\n" |
| 818 |
+ " node10 [label=\"GETELEM\"];\n" |
| 819 |
+ " node9 -> node10 [weight=1];\n" |
| 820 |
+ " node11 [label=\"NAME\"];\n" |
| 821 |
+ " node10 -> node11 [weight=1];\n" |
| 822 |
+ " node12 [label=\"STRING\"];\n" |
| 823 |
+ " node10 -> node12 [weight=1];\n" |
| 824 |
+ " node13 [label=\"EXPR_RESULT\"];\n" |
| 825 |
+ " node9 -> node13 [label=\"UNCOND\", " + |
| 826 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 827 |
+ " node9 -> node5 [label=\"ON_EX\", " + |
| 828 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 829 |
+ " node2 -> node13 [weight=1];\n" |
| 830 |
+ " node14 [label=\"GETPROP\"];\n" |
| 831 |
+ " node13 -> node14 [weight=1];\n" |
| 832 |
+ " node15 [label=\"NAME\"];\n" |
| 833 |
+ " node14 -> node15 [weight=1];\n" |
| 834 |
+ " node16 [label=\"STRING\"];\n" |
| 835 |
+ " node14 -> node16 [weight=1];\n" |
| 836 |
+ " node17 [label=\"EXPR_RESULT\"];\n" |
| 837 |
+ " node13 -> node17 [label=\"UNCOND\", " + |
| 838 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 839 |
+ " node13 -> node5 [label=\"ON_EX\", " + |
| 840 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 841 |
+ " node2 -> node17 [weight=1];\n" |
| 842 |
+ " node18 [label=\"NAME\"];\n" |
| 843 |
+ " node17 -> node18 [weight=1];\n" |
| 844 |
+ " node17 -> RETURN [label=\"UNCOND\", " + |
| 845 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 846 |
+ " node2 -> node3 [label=\"UNCOND\", " + |
| 847 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 848 |
+ " node1 -> node5 [weight=1];\n" |
| 849 |
+ " node19 [label=\"CATCH\"];\n" |
| 850 |
+ " node5 -> node19 [weight=1];\n" |
| 851 |
+ " node20 [label=\"NAME\"];\n" |
| 852 |
+ " node19 -> node20 [weight=1];\n" |
| 853 |
+ " node21 [label=\"BLOCK\"];\n" |
| 854 |
+ " node19 -> node21 [weight=1];\n" |
| 855 |
+ " node22 [label=\"EXPR_RESULT\"];\n" |
| 856 |
+ " node21 -> node22 [weight=1];\n" |
| 857 |
+ " node23 [label=\"CALL\"];\n" |
| 858 |
+ " node22 -> node23 [weight=1];\n" |
| 859 |
+ " node24 [label=\"NAME\"];\n" |
| 860 |
+ " node23 -> node24 [weight=1];\n" |
| 861 |
+ " node22 -> RETURN [label=\"UNCOND\", " + |
| 862 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 863 |
+ " node21 -> node22 [label=\"UNCOND\", " + |
| 864 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 865 |
+ " node19 -> node21 [label=\"UNCOND\", " + |
| 866 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 867 |
+ " node5 -> node19 [label=\"UNCOND\", " + |
| 868 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 869 |
+ " node1 -> node2 [label=\"UNCOND\", " + |
| 870 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 871 |
+ " node0 -> node1 [label=\"UNCOND\", " + |
| 872 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 873 |
+ "}\n"; |
| 874 |
testCfg(src, expected); |
| 875 |
} |
| 876 |
|
| 877 |
public void testFunctionWithinTry() { |
| 878 |
|
| 879 |
String src = "try { function f() {throw 1;} } catch (e) { }"; |
| 880 |
String expected = "digraph AST {\n" |
| 881 |
+ " node [color=lightblue2, style=filled];\n" |
| 882 |
+ " node0 [label=\"SCRIPT\"];\n" |
| 883 |
+ " node1 [label=\"TRY\"];\n" |
| 884 |
+ " node0 -> node1 [weight=1];\n" |
| 885 |
+ " node2 [label=\"BLOCK\"];\n" |
| 886 |
+ " node1 -> node2 [weight=1];\n" |
| 887 |
+ " node3 [label=\"FUNCTION\"];\n" |
| 888 |
+ " node2 -> node3 [weight=1];\n" |
| 889 |
+ " node4 [label=\"NAME\"];\n" |
| 890 |
+ " node3 -> node4 [weight=1];\n" |
| 891 |
+ " node5 [label=\"PARAM_LIST\"];\n" |
| 892 |
+ " node3 -> node5 [weight=1];\n" |
| 893 |
+ " node6 [label=\"BLOCK\"];\n" |
| 894 |
+ " node3 -> node6 [weight=1];\n" |
| 895 |
+ " node7 [label=\"THROW\"];\n" |
| 896 |
+ " node6 -> node7 [weight=1];\n" |
| 897 |
+ " node8 [label=\"NUMBER\"];\n" |
| 898 |
+ " node7 -> node8 [weight=1];\n" |
| 899 |
+ " node6 -> node7 [label=\"UNCOND\", " + |
| 900 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 901 |
+ " node3 -> node6 [label=\"UNCOND\", " + |
| 902 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 903 |
+ " node2 -> RETURN [label=\"UNCOND\", " + |
| 904 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 905 |
+ " node9 [label=\"BLOCK\"];\n" |
| 906 |
+ " node1 -> node9 [weight=1];\n" |
| 907 |
+ " node10 [label=\"CATCH\"];\n" |
| 908 |
+ " node9 -> node10 [weight=1];\n" |
| 909 |
+ " node11 [label=\"NAME\"];\n" |
| 910 |
+ " node10 -> node11 [weight=1];\n" |
| 911 |
+ " node12 [label=\"BLOCK\"];\n" |
| 912 |
+ " node10 -> node12 [weight=1];\n" |
| 913 |
+ " node12 -> RETURN [label=\"UNCOND\", " + |
| 914 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 915 |
+ " node10 -> node12 [label=\"UNCOND\", " + |
| 916 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 917 |
+ " node9 -> node10 [label=\"UNCOND\", " + |
| 918 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 919 |
+ " node1 -> node2 [label=\"UNCOND\", " + |
| 920 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 921 |
+ " node0 -> node1 [label=\"UNCOND\", " + |
| 922 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 923 |
+ "}\n"; |
| 924 |
testCfg(src, expected); |
| 925 |
} |
| 926 |
|
| 927 |
public void testNestedCatch() { |
| 928 |
|
| 929 |
String src = "try{try{throw 1;}catch(e){throw 2}}catch(f){}"; |
| 930 |
String expected = "digraph AST {\n" |
| 931 |
+ " node [color=lightblue2, style=filled];\n" |
| 932 |
+ " node0 [label=\"SCRIPT\"];\n" |
| 933 |
+ " node1 [label=\"TRY\"];\n" |
| 934 |
+ " node0 -> node1 [weight=1];\n" |
| 935 |
+ " node2 [label=\"BLOCK\"];\n" |
| 936 |
+ " node1 -> node2 [weight=1];\n" |
| 937 |
+ " node3 [label=\"TRY\"];\n" |
| 938 |
+ " node2 -> node3 [weight=1];\n" |
| 939 |
+ " node4 [label=\"BLOCK\"];\n" |
| 940 |
+ " node3 -> node4 [weight=1];\n" |
| 941 |
+ " node5 [label=\"THROW\"];\n" |
| 942 |
+ " node4 -> node5 [weight=1];\n" |
| 943 |
+ " node6 [label=\"NUMBER\"];\n" |
| 944 |
+ " node5 -> node6 [weight=1];\n" |
| 945 |
+ " node7 [label=\"BLOCK\"];\n" |
| 946 |
+ " node5 -> node7 [label=\"ON_EX\", " + |
| 947 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 948 |
+ " node4 -> node5 [label=\"UNCOND\", " + |
| 949 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 950 |
+ " node3 -> node7 [weight=1];\n" |
| 951 |
+ " node8 [label=\"CATCH\"];\n" |
| 952 |
+ " node7 -> node8 [weight=1];\n" |
| 953 |
+ " node9 [label=\"NAME\"];\n" |
| 954 |
+ " node8 -> node9 [weight=1];\n" |
| 955 |
+ " node10 [label=\"BLOCK\"];\n" |
| 956 |
+ " node8 -> node10 [weight=1];\n" |
| 957 |
+ " node11 [label=\"THROW\"];\n" |
| 958 |
+ " node10 -> node11 [weight=1];\n" |
| 959 |
+ " node12 [label=\"NUMBER\"];\n" |
| 960 |
+ " node11 -> node12 [weight=1];\n" |
| 961 |
+ " node13 [label=\"BLOCK\"];\n" |
| 962 |
+ " node11 -> node13 [label=\"ON_EX\", " + |
| 963 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 964 |
+ " node10 -> node11 [label=\"UNCOND\", " + |
| 965 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 966 |
+ " node8 -> node10 [label=\"UNCOND\", " + |
| 967 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 968 |
+ " node7 -> node8 [label=\"UNCOND\", " + |
| 969 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 970 |
+ " node3 -> node4 [label=\"UNCOND\", " + |
| 971 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 972 |
+ " node2 -> node3 [label=\"UNCOND\", " + |
| 973 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 974 |
+ " node1 -> node13 [weight=1];\n" |
| 975 |
+ " node14 [label=\"CATCH\"];\n" |
| 976 |
+ " node13 -> node14 [weight=1];\n" |
| 977 |
+ " node15 [label=\"NAME\"];\n" |
| 978 |
+ " node14 -> node15 [weight=1];\n" |
| 979 |
+ " node16 [label=\"BLOCK\"];\n" |
| 980 |
+ " node14 -> node16 [weight=1];\n" |
| 981 |
+ " node16 -> RETURN [label=\"UNCOND\", " + |
| 982 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 983 |
+ " node14 -> node16 [label=\"UNCOND\", " + |
| 984 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 985 |
+ " node13 -> node14 [label=\"UNCOND\", " + |
| 986 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 987 |
+ " node1 -> node2 [label=\"UNCOND\", " + |
| 988 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 989 |
+ " node0 -> node1 [label=\"UNCOND\", " + |
| 990 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 991 |
+ "}\n"; |
| 992 |
testCfg(src, expected); |
| 993 |
} |
| 994 |
|
| 995 |
public void testSimpleFinally() { |
| 996 |
String src = "try{var x; foo()}finally{}"; |
| 997 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 998 |
assertDownEdge(cfg, Token.TRY, Token.BLOCK, Branch.UNCOND); |
| 999 |
assertDownEdge(cfg, Token.BLOCK, Token.VAR, Branch.UNCOND); |
| 1000 |
|
| 1001 |
assertCrossEdge(cfg, Token.EXPR_RESULT, Token.BLOCK, Branch.UNCOND); |
| 1002 |
|
| 1003 |
assertNoEdge(cfg, Token.BLOCK, Token.BLOCK); |
| 1004 |
} |
| 1005 |
|
| 1006 |
public void testSimpleCatchFinally() { |
| 1007 |
|
| 1008 |
String src = "try{ if(a){throw 1}else{a} } catch(e){a}finally{a}"; |
| 1009 |
String expected = "digraph AST {\n" |
| 1010 |
+ " node [color=lightblue2, style=filled];\n" |
| 1011 |
+ " node0 [label=\"SCRIPT\"];\n" |
| 1012 |
+ " node1 [label=\"TRY\"];\n" |
| 1013 |
+ " node0 -> node1 [weight=1];\n" |
| 1014 |
+ " node2 [label=\"BLOCK\"];\n" |
| 1015 |
+ " node1 -> node2 [weight=1];\n" |
| 1016 |
+ " node3 [label=\"IF\"];\n" |
| 1017 |
+ " node2 -> node3 [weight=1];\n" |
| 1018 |
+ " node4 [label=\"NAME\"];\n" |
| 1019 |
+ " node3 -> node4 [weight=1];\n" |
| 1020 |
+ " node5 [label=\"BLOCK\"];\n" |
| 1021 |
+ " node3 -> node5 [weight=1];\n" |
| 1022 |
+ " node6 [label=\"THROW\"];\n" |
| 1023 |
+ " node5 -> node6 [weight=1];\n" |
| 1024 |
+ " node7 [label=\"NUMBER\"];\n" |
| 1025 |
+ " node6 -> node7 [weight=1];\n" |
| 1026 |
+ " node8 [label=\"BLOCK\"];\n" |
| 1027 |
+ " node6 -> node8 [label=\"ON_EX\", " + |
| 1028 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1029 |
+ " node5 -> node6 [label=\"UNCOND\", " + |
| 1030 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1031 |
+ " node9 [label=\"BLOCK\"];\n" |
| 1032 |
+ " node3 -> node9 [weight=1];\n" |
| 1033 |
+ " node10 [label=\"EXPR_RESULT\"];\n" |
| 1034 |
+ " node9 -> node10 [weight=1];\n" |
| 1035 |
+ " node11 [label=\"NAME\"];\n" |
| 1036 |
+ " node10 -> node11 [weight=1];\n" |
| 1037 |
+ " node12 [label=\"BLOCK\"];\n" |
| 1038 |
+ " node10 -> node12 [label=\"UNCOND\", " + |
| 1039 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1040 |
+ " node9 -> node10 [label=\"UNCOND\", " + |
| 1041 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1042 |
+ " node3 -> node5 [label=\"ON_TRUE\", " + |
| 1043 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1044 |
+ " node3 -> node9 [label=\"ON_FALSE\", " + |
| 1045 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1046 |
+ " node2 -> node3 [label=\"UNCOND\", " + |
| 1047 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1048 |
+ " node1 -> node8 [weight=1];\n" |
| 1049 |
+ " node13 [label=\"CATCH\"];\n" |
| 1050 |
+ " node8 -> node13 [weight=1];\n" |
| 1051 |
+ " node14 [label=\"NAME\"];\n" |
| 1052 |
+ " node13 -> node14 [weight=1];\n" |
| 1053 |
+ " node15 [label=\"BLOCK\"];\n" |
| 1054 |
+ " node13 -> node15 [weight=1];\n" |
| 1055 |
+ " node16 [label=\"EXPR_RESULT\"];\n" |
| 1056 |
+ " node15 -> node16 [weight=1];\n" |
| 1057 |
+ " node17 [label=\"NAME\"];\n" |
| 1058 |
+ " node16 -> node17 [weight=1];\n" |
| 1059 |
+ " node16 -> node12 [label=\"UNCOND\", " + |
| 1060 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1061 |
+ " node15 -> node16 [label=\"UNCOND\", " + |
| 1062 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1063 |
+ " node13 -> node15 [label=\"UNCOND\", " + |
| 1064 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1065 |
+ " node8 -> node13 [label=\"UNCOND\", " + |
| 1066 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1067 |
+ " node1 -> node12 [weight=1];\n" |
| 1068 |
+ " node18 [label=\"EXPR_RESULT\"];\n" |
| 1069 |
+ " node12 -> node18 [weight=1];\n" |
| 1070 |
+ " node19 [label=\"NAME\"];\n" |
| 1071 |
+ " node18 -> node19 [weight=1];\n" |
| 1072 |
+ " node18 -> RETURN [label=\"UNCOND\", " + |
| 1073 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1074 |
+ " node12 -> node18 [label=\"UNCOND\", " + |
| 1075 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1076 |
+ " node1 -> node2 [label=\"UNCOND\", " + |
| 1077 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1078 |
+ " node0 -> node1 [label=\"UNCOND\", " + |
| 1079 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" |
| 1080 |
+ "}\n"; |
| 1081 |
testCfg(src, expected); |
| 1082 |
} |
| 1083 |
|
| 1084 |
public void testComplicatedFinally2() { |
| 1085 |
|
| 1086 |
String src = "while(1){try{" + |
| 1087 |
"if(a){a;continue;}else if(b){b;break;} else if(c) throw 1; else a}" + |
| 1088 |
"catch(e){}finally{c()}bar}foo"; |
| 1089 |
|
| 1090 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 1091 |
|
| 1092 |
assertCrossEdge(cfg, Token.CONTINUE, Token.BLOCK, Branch.UNCOND); |
| 1093 |
assertCrossEdge(cfg, Token.BREAK, Token.BLOCK, Branch.UNCOND); |
| 1094 |
assertCrossEdge(cfg, Token.THROW, Token.BLOCK, Branch.ON_EX); |
| 1095 |
} |
| 1096 |
|
| 1097 |
public void testDeepNestedBreakwithFinally() { |
| 1098 |
String src = "X:while(1){try{while(2){try{var a;break X;}" + |
| 1099 |
"finally{}}}finally{}}"; |
| 1100 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 1101 |
assertDownEdge(cfg, Token.WHILE, Token.BLOCK, Branch.ON_TRUE); |
| 1102 |
assertDownEdge(cfg, Token.BLOCK, Token.TRY, Branch.UNCOND); |
| 1103 |
assertDownEdge(cfg, Token.BLOCK, Token.VAR, Branch.UNCOND); |
| 1104 |
|
| 1105 |
assertCrossEdge(cfg, Token.BREAK, Token.BLOCK, Branch.UNCOND); |
| 1106 |
|
| 1107 |
assertCrossEdge(cfg, Token.BLOCK, Token.BLOCK, Branch.ON_EX); |
| 1108 |
assertCrossEdge(cfg, Token.WHILE, Token.BLOCK, Branch.ON_FALSE); |
| 1109 |
assertReturnEdge(cfg, Token.BLOCK); |
| 1110 |
} |
| 1111 |
|
| 1112 |
public void testDeepNestedFinally() { |
| 1113 |
String src = "try{try{try{throw 1}" + |
| 1114 |
"finally{1;var a}}finally{2;if(a);}}finally{3;a()}"; |
| 1115 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 1116 |
assertCrossEdge(cfg, Token.THROW, Token.BLOCK, Branch.ON_EX); |
| 1117 |
assertCrossEdge(cfg, Token.VAR, Token.BLOCK, Branch.UNCOND); |
| 1118 |
assertCrossEdge(cfg, Token.IF, Token.BLOCK, Branch.ON_EX); |
| 1119 |
} |
| 1120 |
|
| 1121 |
public void testReturn() { |
| 1122 |
String src = "function f() { return; }"; |
| 1123 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 1124 |
assertReturnEdge(cfg, Token.RETURN); |
| 1125 |
} |
| 1126 |
|
| 1127 |
public void testReturnInFinally() { |
| 1128 |
String src = "function f(x){ try{} finally {return x;} }"; |
| 1129 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 1130 |
assertReturnEdge(cfg, Token.RETURN); |
| 1131 |
} |
| 1132 |
|
| 1133 |
public void testReturnInFinally2() { |
| 1134 |
String src = "function f(x){" + |
| 1135 |
" try{ try{}finally{var dummy; return x;} } finally {} }"; |
| 1136 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 1137 |
assertCrossEdge(cfg, Token.VAR, Token.RETURN, Branch.UNCOND); |
| 1138 |
assertCrossEdge(cfg, Token.RETURN, Token.BLOCK, Branch.UNCOND); |
| 1139 |
assertReturnEdge(cfg, Token.BLOCK); |
| 1140 |
assertNoReturnEdge(cfg, Token.RETURN); |
| 1141 |
} |
| 1142 |
|
| 1143 |
public void testReturnInTry() { |
| 1144 |
String src = "function f(x){ try{x; return x()} finally {} var y;}"; |
| 1145 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 1146 |
assertCrossEdge(cfg, Token.EXPR_RESULT, Token.RETURN, Branch.UNCOND); |
| 1147 |
assertCrossEdge(cfg, Token.RETURN, Token.BLOCK, Branch.UNCOND); |
| 1148 |
assertCrossEdge(cfg, Token.BLOCK, Token.VAR, Branch.UNCOND); |
| 1149 |
assertReturnEdge(cfg, Token.VAR); |
| 1150 |
assertReturnEdge(cfg, Token.BLOCK); |
| 1151 |
assertNoReturnEdge(cfg, Token.RETURN); |
| 1152 |
} |
| 1153 |
|
| 1154 |
public void testOptionNotToTraverseFunctions() { |
| 1155 |
String src = "var x = 1; function f() { x = null; }"; |
| 1156 |
String expectedWhenNotTraversingFunctions = "digraph AST {\n" + |
| 1157 |
" node [color=lightblue2, style=filled];\n" + |
| 1158 |
" node0 [label=\"SCRIPT\"];\n" + |
| 1159 |
" node1 [label=\"VAR\"];\n" + |
| 1160 |
" node0 -> node1 [weight=1];\n" + |
| 1161 |
" node2 [label=\"NAME\"];\n" + |
| 1162 |
" node1 -> node2 [weight=1];\n" + |
| 1163 |
" node3 [label=\"NUMBER\"];\n" + |
| 1164 |
" node2 -> node3 [weight=1];\n" + |
| 1165 |
" node1 -> RETURN " + |
| 1166 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 1167 |
" node4 [label=\"FUNCTION\"];\n" + |
| 1168 |
" node0 -> node4 [weight=1];\n" + |
| 1169 |
" node5 [label=\"NAME\"];\n" + |
| 1170 |
" node4 -> node5 [weight=1];\n" + |
| 1171 |
" node6 [label=\"PARAM_LIST\"];\n" + |
| 1172 |
" node4 -> node6 [weight=1];\n" + |
| 1173 |
" node7 [label=\"BLOCK\"];\n" + |
| 1174 |
" node4 -> node7 [weight=1];\n" + |
| 1175 |
" node8 [label=\"EXPR_RESULT\"];\n" + |
| 1176 |
" node7 -> node8 [weight=1];\n" + |
| 1177 |
" node9 [label=\"ASSIGN\"];\n" + |
| 1178 |
" node8 -> node9 [weight=1];\n" + |
| 1179 |
" node10 [label=\"NAME\"];\n" + |
| 1180 |
" node9 -> node10 [weight=1];\n" + |
| 1181 |
" node11 [label=\"NULL\"];\n" + |
| 1182 |
" node9 -> node11 [weight=1];\n" + |
| 1183 |
" node0 -> node1 " + |
| 1184 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 1185 |
"}\n"; |
| 1186 |
String expected = "digraph AST {\n" + |
| 1187 |
" node [color=lightblue2, style=filled];\n" + |
| 1188 |
" node0 [label=\"SCRIPT\"];\n" + |
| 1189 |
" node1 [label=\"VAR\"];\n" + |
| 1190 |
" node0 -> node1 [weight=1];\n" + |
| 1191 |
" node2 [label=\"NAME\"];\n" + |
| 1192 |
" node1 -> node2 [weight=1];\n" + |
| 1193 |
" node3 [label=\"NUMBER\"];\n" + |
| 1194 |
" node2 -> node3 [weight=1];\n" + |
| 1195 |
" node1 -> RETURN " + |
| 1196 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 1197 |
" node4 [label=\"FUNCTION\"];\n" + |
| 1198 |
" node0 -> node4 [weight=1];\n" + |
| 1199 |
" node5 [label=\"NAME\"];\n" + |
| 1200 |
" node4 -> node5 [weight=1];\n" + |
| 1201 |
" node6 [label=\"PARAM_LIST\"];\n" + |
| 1202 |
" node4 -> node6 [weight=1];\n" + |
| 1203 |
" node7 [label=\"BLOCK\"];\n" + |
| 1204 |
" node4 -> node7 [weight=1];\n" + |
| 1205 |
" node8 [label=\"EXPR_RESULT\"];\n" + |
| 1206 |
" node7 -> node8 [weight=1];\n" + |
| 1207 |
" node9 [label=\"ASSIGN\"];\n" + |
| 1208 |
" node8 -> node9 [weight=1];\n" + |
| 1209 |
" node10 [label=\"NAME\"];\n" + |
| 1210 |
" node9 -> node10 [weight=1];\n" + |
| 1211 |
" node11 [label=\"NULL\"];\n" + |
| 1212 |
" node9 -> node11 [weight=1];\n" + |
| 1213 |
" node8 -> RETURN " + |
| 1214 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 1215 |
" node7 -> node8 " + |
| 1216 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 1217 |
" node4 -> node7 " + |
| 1218 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 1219 |
" node0 -> node1 " + |
| 1220 |
"[label=\"UNCOND\", fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 1221 |
"}\n"; |
| 1222 |
testCfg(src, expected); |
| 1223 |
testCfg(src, expectedWhenNotTraversingFunctions, false); |
| 1224 |
} |
| 1225 |
|
| 1226 |
public void testInstanceOf() { |
| 1227 |
String src = "try { x instanceof 'x' } catch (e) { }"; |
| 1228 |
ControlFlowGraph<Node> cfg = createCfg(src, true); |
| 1229 |
assertCrossEdge(cfg, Token.EXPR_RESULT, Token.BLOCK, Branch.ON_EX); |
| 1230 |
} |
| 1231 |
|
| 1232 |
public void testSynBlock() { |
| 1233 |
String src = "START(); var x; END(); var y;"; |
| 1234 |
ControlFlowGraph<Node> cfg = createCfg(src, true); |
| 1235 |
assertCrossEdge(cfg, Token.BLOCK, Token.EXPR_RESULT, Branch.SYN_BLOCK); |
| 1236 |
} |
| 1237 |
|
| 1238 |
public void testPartialTraversalOfScope() { |
| 1239 |
Compiler compiler = new Compiler(); |
| 1240 |
ControlFlowAnalysis cfa = new ControlFlowAnalysis(compiler, true, true); |
| 1241 |
|
| 1242 |
Node script1 = compiler.parseSyntheticCode("cfgtest", "var foo;"); |
| 1243 |
Node script2 = compiler.parseSyntheticCode("cfgtest2", "var bar;"); |
| 1244 |
|
| 1245 |
new Node(Token.BLOCK, script1, script2); |
| 1246 |
|
| 1247 |
cfa.process(null, script1); |
| 1248 |
ControlFlowGraph<Node> cfg = cfa.getCfg(); |
| 1249 |
|
| 1250 |
assertNotNull(cfg.getNode(script1)); |
| 1251 |
assertNull(cfg.getNode(script2)); |
| 1252 |
} |
| 1253 |
|
| 1254 |
public void testForLoopOrder() { |
| 1255 |
assertNodeOrder( |
| 1256 |
createCfg("for (var i = 0; i < 5; i++) { var x = 3; } if (true) {}"), |
| 1257 |
Lists.newArrayList( |
| 1258 |
Token.SCRIPT, Token.VAR, Token.FOR, Token.BLOCK, Token.VAR, |
| 1259 |
Token.INC , |
| 1260 |
Token.IF, Token.BLOCK)); |
| 1261 |
} |
| 1262 |
|
| 1263 |
public void testLabelledForInLoopOrder() { |
| 1264 |
assertNodeOrder( |
| 1265 |
createCfg("var i = 0; var y = {}; " + |
| 1266 |
"label: for (var x in y) { " + |
| 1267 |
" if (x) { break label; } else { i++ } x(); }"), |
| 1268 |
Lists.newArrayList( |
| 1269 |
Token.SCRIPT, Token.VAR, Token.VAR, Token.NAME, |
| 1270 |
Token.FOR, Token.BLOCK, |
| 1271 |
Token.IF, Token.BLOCK, Token.BREAK, |
| 1272 |
Token.BLOCK, Token.EXPR_RESULT, Token.EXPR_RESULT)); |
| 1273 |
} |
| 1274 |
|
| 1275 |
public void testLocalFunctionOrder() { |
| 1276 |
ControlFlowGraph<Node> cfg = |
| 1277 |
createCfg("function f() { while (x) { x++; } } var x = 3;"); |
| 1278 |
assertNodeOrder( |
| 1279 |
cfg, |
| 1280 |
Lists.newArrayList( |
| 1281 |
Token.SCRIPT, Token.VAR, |
| 1282 |
|
| 1283 |
Token.FUNCTION, Token.BLOCK, |
| 1284 |
Token.WHILE, Token.BLOCK, Token.EXPR_RESULT)); |
| 1285 |
} |
| 1286 |
|
| 1287 |
public void testDoWhileOrder() { |
| 1288 |
assertNodeOrder( |
| 1289 |
createCfg("do { var x = 3; } while (true); void x;"), |
| 1290 |
Lists.newArrayList( |
| 1291 |
Token.SCRIPT, Token.BLOCK, Token.VAR, Token.DO, Token.EXPR_RESULT)); |
| 1292 |
} |
| 1293 |
|
| 1294 |
public void testBreakInFinally1() { |
| 1295 |
String src = |
| 1296 |
"f = function() {\n" + |
| 1297 |
" var action;\n" + |
| 1298 |
" a: {\n" + |
| 1299 |
" var proto = null;\n" + |
| 1300 |
" try {\n" + |
| 1301 |
" proto = new Proto\n" + |
| 1302 |
" } finally {\n" + |
| 1303 |
" action = proto;\n" + |
| 1304 |
" break a\n" + |
| 1305 |
" }\n" + |
| 1306 |
" }\n" + |
| 1307 |
" alert(action)\n" + |
| 1308 |
"};"; |
| 1309 |
String expected = |
| 1310 |
"digraph AST {\n" + |
| 1311 |
" node [color=lightblue2, style=filled];\n" + |
| 1312 |
" node0 [label=\"SCRIPT\"];\n" + |
| 1313 |
" node1 [label=\"EXPR_RESULT\"];\n" + |
| 1314 |
" node0 -> node1 [weight=1];\n" + |
| 1315 |
" node2 [label=\"ASSIGN\"];\n" + |
| 1316 |
" node1 -> node2 [weight=1];\n" + |
| 1317 |
" node3 [label=\"NAME\"];\n" + |
| 1318 |
" node2 -> node3 [weight=1];\n" + |
| 1319 |
" node4 [label=\"FUNCTION\"];\n" + |
| 1320 |
" node2 -> node4 [weight=1];\n" + |
| 1321 |
" node5 [label=\"NAME\"];\n" + |
| 1322 |
" node4 -> node5 [weight=1];\n" + |
| 1323 |
" node6 [label=\"PARAM_LIST\"];\n" + |
| 1324 |
" node4 -> node6 [weight=1];\n" + |
| 1325 |
" node7 [label=\"BLOCK\"];\n" + |
| 1326 |
" node4 -> node7 [weight=1];\n" + |
| 1327 |
" node8 [label=\"VAR\"];\n" + |
| 1328 |
" node7 -> node8 [weight=1];\n" + |
| 1329 |
" node9 [label=\"NAME\"];\n" + |
| 1330 |
" node8 -> node9 [weight=1];\n" + |
| 1331 |
" node10 [label=\"LABEL\"];\n" + |
| 1332 |
" node7 -> node10 [weight=1];\n" + |
| 1333 |
" node11 [label=\"LABEL_NAME\"];\n" + |
| 1334 |
" node10 -> node11 [weight=1];\n" + |
| 1335 |
" node12 [label=\"BLOCK\"];\n" + |
| 1336 |
" node10 -> node12 [weight=1];\n" + |
| 1337 |
" node13 [label=\"VAR\"];\n" + |
| 1338 |
" node12 -> node13 [weight=1];\n" + |
| 1339 |
" node14 [label=\"NAME\"];\n" + |
| 1340 |
" node13 -> node14 [weight=1];\n" + |
| 1341 |
" node15 [label=\"NULL\"];\n" + |
| 1342 |
" node14 -> node15 [weight=1];\n" + |
| 1343 |
" node16 [label=\"TRY\"];\n" + |
| 1344 |
" node12 -> node16 [weight=1];\n" + |
| 1345 |
" node17 [label=\"BLOCK\"];\n" + |
| 1346 |
" node16 -> node17 [weight=1];\n" + |
| 1347 |
" node18 [label=\"EXPR_RESULT\"];\n" + |
| 1348 |
" node17 -> node18 [weight=1];\n" + |
| 1349 |
" node19 [label=\"ASSIGN\"];\n" + |
| 1350 |
" node18 -> node19 [weight=1];\n" + |
| 1351 |
" node20 [label=\"NAME\"];\n" + |
| 1352 |
" node19 -> node20 [weight=1];\n" + |
| 1353 |
" node21 [label=\"NEW\"];\n" + |
| 1354 |
" node19 -> node21 [weight=1];\n" + |
| 1355 |
" node22 [label=\"NAME\"];\n" + |
| 1356 |
" node21 -> node22 [weight=1];\n" + |
| 1357 |
" node23 [label=\"BLOCK\"];\n" + |
| 1358 |
" node16 -> node23 [weight=1];\n" + |
| 1359 |
" node24 [label=\"BLOCK\"];\n" + |
| 1360 |
" node16 -> node24 [weight=1];\n" + |
| 1361 |
" node25 [label=\"EXPR_RESULT\"];\n" + |
| 1362 |
" node24 -> node25 [weight=1];\n" + |
| 1363 |
" node26 [label=\"ASSIGN\"];\n" + |
| 1364 |
" node25 -> node26 [weight=1];\n" + |
| 1365 |
" node27 [label=\"NAME\"];\n" + |
| 1366 |
" node26 -> node27 [weight=1];\n" + |
| 1367 |
" node28 [label=\"NAME\"];\n" + |
| 1368 |
" node26 -> node28 [weight=1];\n" + |
| 1369 |
" node29 [label=\"BREAK\"];\n" + |
| 1370 |
" node24 -> node29 [weight=1];\n" + |
| 1371 |
" node30 [label=\"LABEL_NAME\"];\n" + |
| 1372 |
" node29 -> node30 [weight=1];\n" + |
| 1373 |
" node31 [label=\"EXPR_RESULT\"];\n" + |
| 1374 |
" node7 -> node31 [weight=1];\n" + |
| 1375 |
" node32 [label=\"CALL\"];\n" + |
| 1376 |
" node31 -> node32 [weight=1];\n" + |
| 1377 |
" node33 [label=\"NAME\"];\n" + |
| 1378 |
" node32 -> node33 [weight=1];\n" + |
| 1379 |
" node34 [label=\"NAME\"];\n" + |
| 1380 |
" node32 -> node34 [weight=1];\n" + |
| 1381 |
" node1 -> RETURN [label=\"UNCOND\", " + |
| 1382 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 1383 |
" node0 -> node1 [label=\"UNCOND\", " + |
| 1384 |
"fontcolor=\"red\", weight=0.01, color=\"red\"];\n" + |
| 1385 |
"}\n"; |
| 1386 |
testCfg(src, expected); |
| 1387 |
} |
| 1388 |
|
| 1389 |
public void testBreakInFinally2() { |
| 1390 |
String src = |
| 1391 |
"var action;\n" + |
| 1392 |
"a: {\n" + |
| 1393 |
" var proto = null;\n" + |
| 1394 |
" try {\n" + |
| 1395 |
" proto = new Proto\n" + |
| 1396 |
" } finally {\n" + |
| 1397 |
" action = proto;\n" + |
| 1398 |
" break a\n" + |
| 1399 |
" }\n" + |
| 1400 |
"}\n" + |
| 1401 |
"alert(action)\n"; |
| 1402 |
|
| 1403 |
ControlFlowGraph<Node> cfg = createCfg(src); |
| 1404 |
assertCrossEdge(cfg, Token.BREAK, Token.EXPR_RESULT, Branch.UNCOND); |
| 1405 |
assertNoEdge(cfg, Token.BREAK, Token.BLOCK); |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
private void assertNodeOrder(ControlFlowGraph<Node> cfg, |
| 1420 |
List<Integer> nodeTypes) { |
| 1421 |
List<DiGraphNode<Node, Branch>> cfgNodes = |
| 1422 |
Lists.newArrayList(cfg.getDirectedGraphNodes()); |
| 1423 |
Collections.sort(cfgNodes, cfg.getOptionalNodeComparator(true)); |
| 1424 |
|
| 1425 |
|
| 1426 |
Node implicitReturn = cfgNodes.remove(cfgNodes.size() - 1).getValue(); |
| 1427 |
assertNull(implicitReturn == null ? "null" : implicitReturn.toStringTree(), |
| 1428 |
implicitReturn); |
| 1429 |
|
| 1430 |
assertEquals("Wrong number of CFG nodes", |
| 1431 |
nodeTypes.size(), cfgNodes.size()); |
| 1432 |
for (int i = 0; i < cfgNodes.size(); i++) { |
| 1433 |
int expectedType = nodeTypes.get(i); |
| 1434 |
int actualType = cfgNodes.get(i).getValue().getType(); |
| 1435 |
assertEquals( |
| 1436 |
"node type mismatch at " + i + ".\n" + |
| 1437 |
"found : " + Token.name(actualType) + "\n" + |
| 1438 |
"required: " + Token.name(expectedType) + "\n", |
| 1439 |
expectedType, actualType); |
| 1440 |
} |
| 1441 |
} |
| 1442 |
} |