Skip to content

Commit cff63fa

Browse files
committed
Ruby: CFG: make WhenExpr post-order
1 parent a9286e8 commit cff63fa

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

ruby/ql/lib/codeql/ruby/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -388,22 +388,23 @@ module Trees {
388388
first(this.getBranch(0), succ) and
389389
c instanceof SimpleCompletion
390390
or
391-
exists(int i, WhenTree branch | branch = this.getBranch(i) |
391+
exists(int i, WhenTree branch |
392+
branch = this.getBranch(i) and
392393
last(branch.getLastPattern(), pred, c) and
393-
first(this.getBranch(i + 1), succ) and
394394
c.(ConditionalCompletion).getValue() = false
395+
|
396+
first(this.getBranch(i + 1), succ)
397+
or
398+
not exists(this.getBranch(i + 1)) and succ = this
395399
)
396400
or
397401
succ = this and
398402
(
399403
last(this.getValue(), pred, c) and not exists(this.getABranch())
400404
or
401-
last(this.getABranch().(WhenExpr).getBody(), pred, c)
402-
or
403-
exists(int i, ControlFlowTree lastBranch |
404-
lastBranch = this.getBranch(i) and
405-
not exists(this.getBranch(i + 1)) and
406-
last(lastBranch, pred, c)
405+
exists(int i, ControlFlowTree branch |
406+
branch = this.getBranch(i) and
407+
last(branch, pred, c)
407408
)
408409
)
409410
}
@@ -1377,8 +1378,13 @@ module Trees {
13771378
final override ControlFlowTree getChildElement(int i) { result = this.getMethodName(i) }
13781379
}
13791380

1380-
private class WhenTree extends PreOrderTree, WhenExpr {
1381-
final override predicate propagatesAbnormal(AstNode child) { child = this.getAPattern() }
1381+
private class WhenTree extends PostOrderTree, WhenExpr {
1382+
final override predicate propagatesAbnormal(AstNode child) {
1383+
child = this.getAPattern() or
1384+
child = this.getBody()
1385+
}
1386+
1387+
final override predicate first(AstNode first) { first(this.getPattern(0), first) }
13821388

13831389
final Expr getLastPattern() {
13841390
exists(int i |
@@ -1387,18 +1393,7 @@ module Trees {
13871393
)
13881394
}
13891395

1390-
final override predicate last(AstNode last, Completion c) {
1391-
last(this.getLastPattern(), last, c) and
1392-
c.(ConditionalCompletion).getValue() = false
1393-
or
1394-
last(this.getBody(), last, c)
1395-
}
1396-
13971396
final override predicate succ(AstNode pred, AstNode succ, Completion c) {
1398-
pred = this and
1399-
first(this.getPattern(0), succ) and
1400-
c instanceof SimpleCompletion
1401-
or
14021397
exists(int i, Expr p, boolean b |
14031398
p = this.getPattern(i) and
14041399
last(p, pred, c) and
@@ -1410,6 +1405,15 @@ module Trees {
14101405
b = false and
14111406
first(this.getPattern(i + 1), succ)
14121407
)
1408+
or
1409+
last(this.getBody(), pred, c) and
1410+
succ = this and
1411+
c instanceof NormalCompletion
1412+
or
1413+
not exists(this.getBody()) and
1414+
last(this.getLastPattern(), pred, c) and
1415+
succ = this and
1416+
c.(ConditionalCompletion).getValue() = true
14131417
}
14141418
}
14151419
}

ruby/ql/test/library-tests/controlflow/graph/Cfg.expected

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -508,20 +508,20 @@ case.rb:
508508
#-----| -> exit if_in_case (normal)
509509

510510
# 2| call to x1
511-
#-----| -> when ...
511+
#-----| -> 1
512512

513513
# 2| self
514514
#-----| -> call to x1
515515

516516
# 3| when ...
517-
#-----| -> 1
517+
#-----| -> case ...
518518

519519
# 3| 1
520-
#-----| no-match -> when ...
520+
#-----| no-match -> 2
521521
#-----| match -> self
522522

523523
# 3| then ...
524-
#-----| -> case ...
524+
#-----| -> when ...
525525

526526
# 3| ( ... )
527527
#-----| -> then ...
@@ -549,14 +549,14 @@ case.rb:
549549
#-----| -> call to puts
550550

551551
# 4| when ...
552-
#-----| -> 2
552+
#-----| -> case ...
553553

554554
# 4| 2
555555
#-----| no-match -> case ...
556556
#-----| match -> self
557557

558558
# 4| then ...
559-
#-----| -> case ...
559+
#-----| -> when ...
560560

561561
# 4| call to puts
562562
#-----| -> then ...
@@ -1712,20 +1712,20 @@ cfg.rb:
17121712
#-----| -> call to puts
17131713

17141714
# 41| case ...
1715-
#-----| -> when ...
1715+
#-----| -> b
17161716

17171717
# 41| 10
1718-
#-----| -> when ...
1718+
#-----| -> 1
17191719

17201720
# 42| when ...
1721-
#-----| -> 1
1721+
#-----| -> case ...
17221722

17231723
# 42| 1
1724-
#-----| no-match -> when ...
1724+
#-----| no-match -> 2
17251725
#-----| match -> self
17261726

17271727
# 42| then ...
1728-
#-----| -> case ...
1728+
#-----| -> when ...
17291729

17301730
# 42| call to puts
17311731
#-----| -> then ...
@@ -1737,7 +1737,7 @@ cfg.rb:
17371737
#-----| -> call to puts
17381738

17391739
# 43| when ...
1740-
#-----| -> 2
1740+
#-----| -> case ...
17411741

17421742
# 43| 2
17431743
#-----| no-match -> 3
@@ -1752,7 +1752,7 @@ cfg.rb:
17521752
#-----| no-match -> self
17531753

17541754
# 43| then ...
1755-
#-----| -> case ...
1755+
#-----| -> when ...
17561756

17571757
# 43| call to puts
17581758
#-----| -> then ...
@@ -1779,10 +1779,10 @@ cfg.rb:
17791779
#-----| -> chained
17801780

17811781
# 48| when ...
1782-
#-----| -> b
1782+
#-----| -> case ...
17831783

17841784
# 48| ... == ...
1785-
#-----| false -> when ...
1785+
#-----| false -> b
17861786
#-----| true -> self
17871787

17881788
# 48| b
@@ -1792,7 +1792,7 @@ cfg.rb:
17921792
#-----| -> ... == ...
17931793

17941794
# 48| then ...
1795-
#-----| -> case ...
1795+
#-----| -> when ...
17961796

17971797
# 48| call to puts
17981798
#-----| -> then ...
@@ -1804,7 +1804,7 @@ cfg.rb:
18041804
#-----| -> call to puts
18051805

18061806
# 49| when ...
1807-
#-----| -> b
1807+
#-----| -> case ...
18081808

18091809
# 49| ... == ...
18101810
#-----| false -> b
@@ -1827,7 +1827,7 @@ cfg.rb:
18271827
#-----| -> ... > ...
18281828

18291829
# 49| then ...
1830-
#-----| -> case ...
1830+
#-----| -> when ...
18311831

18321832
# 49| call to puts
18331833
#-----| -> then ...

0 commit comments

Comments
 (0)