Skip to content

Commit 7644d60

Browse files
committed
Revert "Ruby: CFG: make WhenExpr post-order"
This reverts commit cff63fa.
1 parent 46144fe commit 7644d60

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

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

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -388,24 +388,23 @@ module Trees {
388388
first(this.getBranch(0), succ) and
389389
c instanceof SimpleCompletion
390390
or
391-
exists(int i, WhenTree branch |
392-
branch = this.getBranch(i) and
391+
exists(int i, WhenTree branch | branch = this.getBranch(i) |
393392
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
399395
)
400396
or
401397
succ = this and
402398
c instanceof NormalCompletion and
403399
(
404400
last(this.getValue(), pred, c) and not exists(this.getABranch())
405401
or
406-
exists(int i, ControlFlowTree branch |
407-
branch = this.getBranch(i) and
408-
last(branch, pred, c)
402+
last(this.getABranch().(WhenExpr).getBody(), pred, c)
403+
or
404+
exists(int i, ControlFlowTree lastBranch |
405+
lastBranch = this.getBranch(i) and
406+
not exists(this.getBranch(i + 1)) and
407+
last(lastBranch, pred, c)
409408
)
410409
)
411410
}
@@ -1371,13 +1370,8 @@ module Trees {
13711370
final override ControlFlowTree getChildElement(int i) { result = this.getMethodName(i) }
13721371
}
13731372

1374-
private class WhenTree extends PostOrderTree, WhenExpr {
1375-
final override predicate propagatesAbnormal(AstNode child) {
1376-
child = this.getAPattern() or
1377-
child = this.getBody()
1378-
}
1379-
1380-
final override predicate first(AstNode first) { first(this.getPattern(0), first) }
1373+
private class WhenTree extends PreOrderTree, WhenExpr {
1374+
final override predicate propagatesAbnormal(AstNode child) { child = this.getAPattern() }
13811375

13821376
final Expr getLastPattern() {
13831377
exists(int i |
@@ -1386,7 +1380,18 @@ module Trees {
13861380
)
13871381
}
13881382

1383+
final override predicate last(AstNode last, Completion c) {
1384+
last(this.getLastPattern(), last, c) and
1385+
c.(ConditionalCompletion).getValue() = false
1386+
or
1387+
last(this.getBody(), last, c)
1388+
}
1389+
13891390
final override predicate succ(AstNode pred, AstNode succ, Completion c) {
1391+
pred = this and
1392+
first(this.getPattern(0), succ) and
1393+
c instanceof SimpleCompletion
1394+
or
13901395
exists(int i, Expr p, boolean b |
13911396
p = this.getPattern(i) and
13921397
last(p, pred, c) and
@@ -1398,15 +1403,6 @@ module Trees {
13981403
b = false and
13991404
first(this.getPattern(i + 1), succ)
14001405
)
1401-
or
1402-
last(this.getBody(), pred, c) and
1403-
succ = this and
1404-
c instanceof NormalCompletion
1405-
or
1406-
not exists(this.getBody()) and
1407-
last(this.getLastPattern(), pred, c) and
1408-
succ = this and
1409-
c.(ConditionalCompletion).getValue() = true
14101406
}
14111407
}
14121408
}

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-
#-----| -> 1
511+
#-----| -> when ...
512512

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)