Skip to content

Commit 5fef916

Browse files
authored
Merge pull request #171 from asger-semmle/js-for-loop-cfg
JavaScript: fix CFG for EnhancedForStmt
2 parents ec7beab + 0a4a5da commit 5fef916

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

javascript/ql/src/semmle/javascript/Stmt.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,10 @@ abstract class EnhancedForLoop extends LoopStmt {
607607
override Stmt getBody() {
608608
result = getChildStmt(2)
609609
}
610+
611+
override ControlFlowNode getFirstControlFlowNode() {
612+
result = getIteratorExpr().getFirstControlFlowNode()
613+
}
610614
}
611615

612616
/** A `for`-`in` loop. */

javascript/ql/test/library-tests/CFG/CFG.expected

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -644,15 +644,14 @@
644644
| forof | 1 | entry node of functio ... ;\\n} | 2 | {\\n f ... ;\\n} |
645645
| forof | 1 | f | 1 | functio ... ;\\n} |
646646
| forof | 1 | functio ... ;\\n} | 10 | exit node of <toplevel> |
647-
| forof | 2 | {\\n f ... ;\\n} | 3 | for (\\n ... )\\n ; |
648-
| forof | 3 | for (\\n ... )\\n ; | 7 | [] |
647+
| forof | 2 | {\\n f ... ;\\n} | 7 | [] |
648+
| forof | 3 | for (\\n ... )\\n ; | 4 | var\\n x |
649+
| forof | 3 | for (\\n ... )\\n ; | 9 | exit node of functio ... ;\\n} |
649650
| forof | 4 | var\\n x | 5 | x |
650651
| forof | 5 | x | 5 | x |
651652
| forof | 5 | x | 8 | ; |
652-
| forof | 7 | [] | 4 | var\\n x |
653-
| forof | 7 | [] | 9 | exit node of functio ... ;\\n} |
654-
| forof | 8 | ; | 4 | var\\n x |
655-
| forof | 8 | ; | 9 | exit node of functio ... ;\\n} |
653+
| forof | 7 | [] | 3 | for (\\n ... )\\n ; |
654+
| forof | 8 | ; | 3 | for (\\n ... )\\n ; |
656655
| globals | 1 | entry node of <toplevel> | 14 | g |
657656
| globals | 1 | var\\n x,\\n y; | 2 | x |
658657
| globals | 2 | x | 2 | x |
@@ -662,15 +661,14 @@
662661
| globals | 4 | {\\n var\\n z;\\n} | 5 | var\\n z; |
663662
| globals | 5 | var\\n z; | 6 | z |
664663
| globals | 6 | z | 6 | z |
665-
| globals | 6 | z | 8 | for (\\n ... [])\\n ; |
666-
| globals | 8 | for (\\n ... [])\\n ; | 11 | [] |
664+
| globals | 6 | z | 11 | [] |
665+
| globals | 8 | for (\\n ... [])\\n ; | 9 | var\\n a |
666+
| globals | 8 | for (\\n ... [])\\n ; | 13 | function\\n g()\\n{\\n} |
667667
| globals | 9 | var\\n a | 10 | a |
668668
| globals | 10 | a | 10 | a |
669669
| globals | 10 | a | 12 | ; |
670-
| globals | 11 | [] | 9 | var\\n a |
671-
| globals | 11 | [] | 13 | function\\n g()\\n{\\n} |
672-
| globals | 12 | ; | 9 | var\\n a |
673-
| globals | 12 | ; | 13 | function\\n g()\\n{\\n} |
670+
| globals | 11 | [] | 8 | for (\\n ... [])\\n ; |
671+
| globals | 12 | ; | 8 | for (\\n ... [])\\n ; |
674672
| globals | 13 | entry node of function\\n g()\\n{\\n} | 15 | {\\n} |
675673
| globals | 13 | function\\n g()\\n{\\n} | 17 | !\\nfunction\\n h()\\n{\\n}; |
676674
| globals | 14 | g | 1 | var\\n x,\\n y; |
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function f() {
2+
let y = false;
3+
for (const x of [1, 2, 3]) {
4+
if (x > 0) {
5+
y = true; // OK
6+
continue;
7+
}
8+
return;
9+
}
10+
if (!y) {
11+
console.log("Hello");
12+
}
13+
}

0 commit comments

Comments
 (0)