Skip to content

Commit 1479376

Browse files
committed
rename visit to visitWithSuccessors to avoid ambiguity
1 parent 4289875 commit 1479376

File tree

1 file changed

+49
-36
lines changed

1 file changed

+49
-36
lines changed

javascript/extractor/src/com/semmle/js/extractor/CFGExtractor.java

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ private Object visit(IStatementContainer nd) {
924924
}.find(ctxt.size() - 1);
925925
}
926926

927-
private Node visit(Node nd, Object trueSuccessors, Object falseSuccessors) {
927+
private Node visitWithSuccessors(Node nd, Object trueSuccessors, Object falseSuccessors) {
928928
if (nd == null) return null;
929929

930930
followingCache.put(nd, union(followingCache.get(nd), union(trueSuccessors, falseSuccessors)));
@@ -936,11 +936,19 @@ private Node visit(Node nd, Object trueSuccessors, Object falseSuccessors) {
936936
return First.of(nd);
937937
}
938938

939+
private Node visitWithSuccessors(Node nd, Object successors) {
940+
if (nd == null) return null;
941+
942+
followingCache.put(nd, union(followingCache.get(nd), successors));
943+
nd.accept(this, new SimpleSuccessorInfo(successors));
944+
return First.of(nd);
945+
}
946+
939947
private Object seq(Object... nodes) {
940948
Object fst = nodes[nodes.length - 1];
941949
for (int i = nodes.length - 2; i >= 0; --i) {
942950
for (Node node : createReversedIterable(nodes[i])) {
943-
Node ffst = visit(node, fst, null);
951+
Node ffst = visitWithSuccessors(node, fst);
944952
if (ffst != null) fst = ffst;
945953
}
946954
}
@@ -1212,7 +1220,7 @@ public Void visit(LabeledStatement nd, SuccessorInfo i) {
12121220
@Override
12131221
public Void visit(ExpressionStatement nd, SuccessorInfo i) {
12141222
writeSuccessors(nd, First.of(nd.getExpression()));
1215-
visit(nd.getExpression(), i.getAllSuccessors(), null);
1223+
visitWithSuccessors(nd.getExpression(), i.getAllSuccessors());
12161224
return null;
12171225
}
12181226

@@ -1227,20 +1235,20 @@ public Void visit(IfStatement nd, SuccessorInfo i) {
12271235
Expression test = nd.getTest();
12281236
writeSuccessors(nd, First.of(test));
12291237
Object following = i.getAllSuccessors();
1230-
visit(
1238+
visitWithSuccessors(
12311239
test,
12321240
First.of(nd.getConsequent()),
12331241
nd.hasAlternate() ? First.of(nd.getAlternate()) : following);
1234-
this.visit(nd.getConsequent(), following, null);
1235-
this.visit(nd.getAlternate(), following, null);
1242+
visitWithSuccessors(nd.getConsequent(), following);
1243+
visitWithSuccessors(nd.getAlternate(), following);
12361244
return null;
12371245
}
12381246

12391247
@Override
12401248
public Void visit(ConditionalExpression nd, SuccessorInfo i) {
12411249
Expression test = nd.getTest();
12421250
writeSuccessors(nd, First.of(test));
1243-
visit(test, First.of(nd.getConsequent()), First.of(nd.getAlternate()));
1251+
visitWithSuccessors(test, First.of(nd.getConsequent()), First.of(nd.getAlternate()));
12441252

12451253
nd.getConsequent().accept(this, i);
12461254
nd.getAlternate().accept(this, i);
@@ -1294,10 +1302,10 @@ public Void visit(SwitchStatement nd, SuccessorInfo i) {
12941302
}
12951303
}
12961304

1297-
if (nd.getCases().isEmpty()) this.visit(nd.getDiscriminant(), i.getAllSuccessors(), null);
1305+
if (nd.getCases().isEmpty()) visitWithSuccessors(nd.getDiscriminant(), i.getAllSuccessors());
12981306
else if (nd.getCases().size() > 1 && nd.getCases().get(0).isDefault())
1299-
this.visit(nd.getDiscriminant(), First.of(nd.getCases().get(1)), null);
1300-
else this.visit(nd.getDiscriminant(), First.of(nd.getCases().get(0)), null);
1307+
visitWithSuccessors(nd.getDiscriminant(), First.of(nd.getCases().get(1)));
1308+
else visitWithSuccessors(nd.getDiscriminant(), First.of(nd.getCases().get(0)));
13011309
this.seq(nd.getCases(), i.getAllSuccessors());
13021310
this.ctxt.pop();
13031311
return null;
@@ -1318,14 +1326,14 @@ public Void visit(SwitchCase nd, SuccessorInfo i) {
13181326

13191327
@Override
13201328
public Void visit(ReturnStatement nd, SuccessorInfo i) {
1321-
visit(nd.getArgument(), nd, null);
1329+
visitWithSuccessors(nd.getArgument(), nd);
13221330
writeSuccessors(nd, findTarget(JumpType.RETURN, null));
13231331
return null;
13241332
}
13251333

13261334
@Override
13271335
public Void visit(ThrowStatement nd, SuccessorInfo i) {
1328-
visit(nd.getArgument(), nd, null);
1336+
visitWithSuccessors(nd.getArgument(), nd);
13291337
writeSuccessors(nd, findTarget(JumpType.THROW, null));
13301338
return null;
13311339
}
@@ -1337,17 +1345,22 @@ public Void visit(TryStatement nd, SuccessorInfo i) {
13371345

13381346
ctxt.push(nd);
13391347
Object fst = nd.hasFinalizer() ? First.of(nd.getFinalizer()) : i.getAllSuccessors();
1340-
this.visit(nd.getBlock(), fst, null);
1348+
visitWithSuccessors(nd.getBlock(), fst);
13411349
ctxt.pop();
13421350

13431351
if (nd.hasFinalizer()) ctxt.push(new Finally(nd.getFinalizer().getLoc(), nd.getFinalizer()));
13441352

1345-
for (int j = 0, n = nd.getAllHandlers().size(); j < n; ++j)
1346-
visit(nd.getAllHandlers().get(j), fst, j + 1 < n ? nd.getAllHandlers().get(j + 1) : null);
1353+
for (int j = 0, n = nd.getAllHandlers().size(); j < n; ++j) {
1354+
if (j + 1 < n) {
1355+
visitWithSuccessors(nd.getAllHandlers().get(j), fst, nd.getAllHandlers().get(j + 1));
1356+
} else {
1357+
visitWithSuccessors(nd.getAllHandlers().get(j), fst);
1358+
}
1359+
}
13471360

13481361
if (nd.hasFinalizer()) {
13491362
ctxt.pop();
1350-
visit(nd.getFinalizer(), followingCache.get(nd.getFinalizer()), null);
1363+
visitWithSuccessors(nd.getFinalizer(), followingCache.get(nd.getFinalizer()));
13511364
}
13521365
return null;
13531366
}
@@ -1397,8 +1410,8 @@ public Void visit(WhileStatement nd, SuccessorInfo i) {
13971410
Node testStart = First.of(test);
13981411
writeSuccessors(nd, testStart);
13991412
ctxt.push(nd);
1400-
visit(nd.getBody(), testStart, null);
1401-
visit(test, First.of(nd.getBody()), i.getAllSuccessors());
1413+
visitWithSuccessors(nd.getBody(), testStart);
1414+
visitWithSuccessors(test, First.of(nd.getBody()), i.getAllSuccessors());
14021415
ctxt.pop();
14031416
return null;
14041417
}
@@ -1409,8 +1422,8 @@ public Void visit(DoWhileStatement nd, SuccessorInfo i) {
14091422
writeSuccessors(nd, body);
14101423
ctxt.push(nd);
14111424
Expression test = nd.getTest();
1412-
visit(nd.getBody(), First.of(test), null);
1413-
visit(test, body, i.getAllSuccessors());
1425+
visitWithSuccessors(nd.getBody(), First.of(test));
1426+
visitWithSuccessors(test, body, i.getAllSuccessors());
14141427
ctxt.pop();
14151428
return null;
14161429
}
@@ -1431,11 +1444,11 @@ public Void visit(ForStatement nd, SuccessorInfo i) {
14311444
writeSuccessors(nd, First.of(nd.hasInit() ? nd.getInit() : nd.hasTest() ? nd.getTest() : nd.getBody()));
14321445
ctxt.push(nd);
14331446
Node fst = First.of(nd.hasTest() ? nd.getTest() : nd.getBody());
1434-
visit(nd.getInit(), fst, null);
1447+
visitWithSuccessors(nd.getInit(), fst);
14351448

14361449
if (nd.hasTest()) {
14371450
Expression test = nd.getTest();
1438-
visit(test, First.of(nd.getBody()), i.getAllSuccessors());
1451+
visitWithSuccessors(test, First.of(nd.getBody()), i.getAllSuccessors());
14391452
}
14401453
seq(nd.getBody(), nd.getUpdate(), fst);
14411454
ctxt.pop();
@@ -1449,7 +1462,7 @@ public Void visit(SequenceExpression nd, SuccessorInfo i) {
14491462
int n = expressions.size() - 1;
14501463
expressions.get(n).accept(this, i);
14511464
Node next = First.of(expressions.get(n));
1452-
while (--n >= 0) next = visit(expressions.get(n), next, null);
1465+
while (--n >= 0) next = visitWithSuccessors(expressions.get(n), next);
14531466
return null;
14541467
}
14551468

@@ -1579,16 +1592,16 @@ public Void visit(AssignmentExpression nd, SuccessorInfo i) {
15791592
if ("&&=".equals(nd.getOperator()) || "||=".equals(nd.getOperator()) || "??=".equals(nd.getOperator())) {
15801593
if ("&&=".equals(nd.getOperator())) {
15811594
// from lhs to rhs on truthy. from lhs to false-branch on falsy.
1582-
visit(nd.getLeft(), First.of(nd.getRight()), i.getSuccessors(false));
1595+
visitWithSuccessors(nd.getLeft(), First.of(nd.getRight()), i.getSuccessors(false));
15831596
} else if ("||=".equals(nd.getOperator())) {
15841597
// from lhs to true-branch on truthy. from lhs to rhs on falsy.
1585-
visit(nd.getLeft(), i.getSuccessors(true), First.of(nd.getRight()));
1598+
visitWithSuccessors(nd.getLeft(), i.getSuccessors(true), First.of(nd.getRight()));
15861599
} else { // "??="
15871600
// the union of the above - truthyness is unknown.
1588-
visit(nd.getLeft(), union(First.of(nd.getRight()), i.getAllSuccessors()), null);
1601+
visitWithSuccessors(nd.getLeft(), union(First.of(nd.getRight()), i.getAllSuccessors()));
15891602
}
15901603

1591-
visit(nd.getRight(), First.of(nd), null); // from right to assignment.
1604+
visitWithSuccessors(nd.getRight(), First.of(nd)); // from right to assignment.
15921605

15931606
writeSuccessors(nd, i.getGuardedSuccessors(nd));
15941607
} else {
@@ -1612,7 +1625,7 @@ public Void visit(BinaryExpression nd, SuccessorInfo i) {
16121625
union(
16131626
First.of(nd.getRight()),
16141627
i.getAllSuccessors()); // short-circuiting happens with both truthy and falsy values
1615-
visit(nd.getLeft(), leftSucc, null);
1628+
visitWithSuccessors(nd.getLeft(), leftSucc);
16161629
nd.getRight().accept(this, i);
16171630
} else {
16181631
this.seq(nd.getLeft(), nd.getRight(), nd);
@@ -1634,36 +1647,36 @@ public Void visit(LogicalExpression nd, SuccessorInfo i) {
16341647
Expression left = nd.getLeft();
16351648
writeSuccessors(nd, First.of(left));
16361649
if ("&&".equals(nd.getOperator()))
1637-
visit(left, First.of(nd.getRight()), i.getSuccessors(false));
1638-
else visit(left, i.getSuccessors(true), First.of(nd.getRight()));
1650+
visitWithSuccessors(left, First.of(nd.getRight()), i.getSuccessors(false));
1651+
else visitWithSuccessors(left, i.getSuccessors(true), First.of(nd.getRight()));
16391652
nd.getRight().accept(this, i);
16401653
return null;
16411654
}
16421655

16431656
@Override
16441657
public Void visit(SpreadElement nd, SuccessorInfo i) {
1645-
visit(nd.getArgument(), nd, null);
1658+
visitWithSuccessors(nd.getArgument(), nd);
16461659
writeSuccessors(nd, i.getAllSuccessors());
16471660
return null;
16481661
}
16491662

16501663
@Override
16511664
public Void visit(UnaryExpression nd, SuccessorInfo i) {
1652-
visit(nd.getArgument(), nd, null);
1665+
visitWithSuccessors(nd.getArgument(), nd);
16531666
writeSuccessors(nd, i.getGuardedSuccessors(nd));
16541667
return null;
16551668
}
16561669

16571670
@Override
16581671
public Void visit(UpdateExpression nd, SuccessorInfo i) {
1659-
visit(nd.getArgument(), nd, null);
1672+
visitWithSuccessors(nd.getArgument(), nd);
16601673
writeSuccessors(nd, i.getGuardedSuccessors(nd));
16611674
return null;
16621675
}
16631676

16641677
@Override
16651678
public Void visit(YieldExpression nd, SuccessorInfo i) {
1666-
visit(nd.getArgument(), nd, null);
1679+
visitWithSuccessors(nd.getArgument(), nd);
16671680
// yield expressions may throw
16681681
writeSuccessors(nd, union(this.findTarget(JumpType.THROW, null), i.getGuardedSuccessors(nd)));
16691682
return null;
@@ -1759,7 +1772,7 @@ private Node visitComprehensionBlock(ComprehensionExpression nd, int i, Object f
17591772

17601773
private Node visitComprehensionFilter(ComprehensionExpression nd, Object follow) {
17611774
if (nd.hasFilter()) {
1762-
visit(nd.getFilter(), visitComprehensionBody(nd, follow), follow);
1775+
visitWithSuccessors(nd.getFilter(), visitComprehensionBody(nd, follow), follow);
17631776
return First.of(nd.getFilter());
17641777
} else {
17651778
return visitComprehensionBody(nd, follow);
@@ -1873,7 +1886,7 @@ public Void visit(JSXAttribute nd, SuccessorInfo c) {
18731886

18741887
@Override
18751888
public Void visit(JSXSpreadAttribute nd, SuccessorInfo c) {
1876-
visit(nd.getArgument(), nd, null);
1889+
visitWithSuccessors(nd.getArgument(), nd);
18771890
Label propkey = trapwriter.localID(nd, "JSXSpreadAttribute");
18781891
Label spreadkey = trapwriter.localID(nd);
18791892
trapwriter.addTuple("successor", spreadkey, propkey);

0 commit comments

Comments
 (0)