Skip to content

Commit 5da1425

Browse files
committed
Rust: Include names in patterns in the CFG
1 parent bf9563e commit 5da1425

File tree

3 files changed

+170
-45
lines changed

3 files changed

+170
-45
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,12 @@ module PatternTrees {
648648

649649
abstract class PostOrderPatTree extends StandardPatTree, StandardPostOrderTree { }
650650

651-
class IdentPatTree extends PostOrderPatTree, IdentPat {
652-
override Pat getPat(int i) { i = 0 and result = this.getPat() }
651+
class IdentPatTree extends StandardPostOrderTree, IdentPat {
652+
override AstNode getChildNode(int i) {
653+
i = 0 and result = this.getPat()
654+
or
655+
i = 1 and result = this.getName()
656+
}
653657

654658
override predicate last(AstNode node, Completion c) {
655659
super.last(node, c)
@@ -658,7 +662,11 @@ module PatternTrees {
658662
}
659663

660664
override predicate succ(AstNode pred, AstNode succ, Completion c) {
665+
// Edge from succesful subpattern to name
661666
super.succ(pred, succ, c) and c.(MatchCompletion).succeeded()
667+
or
668+
// Edge from name to the identifier pattern itself
669+
last(this.getName(), pred, c) and succ = this and completionIsNormal(c)
662670
}
663671
}
664672

rust/ql/test/library-tests/controlflow/BasicBlocks.expected

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -612,32 +612,42 @@ dominates
612612
| test.rs:396:13:396:13 | _ | test.rs:396:13:396:13 | _ |
613613
| test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern | test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern |
614614
| test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern | test.rs:401:9:404:9 | match 43 { ... } |
615-
| test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern | test.rs:402:13:402:21 | [match(true)] n @ ... |
615+
| test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern | test.rs:402:13:402:13 | n |
616616
| test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern | test.rs:402:17:402:17 | 1 |
617617
| test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern | test.rs:402:20:402:21 | 10 |
618+
| test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern | test.rs:402:26:402:26 | 2 |
618619
| test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern | test.rs:403:13:403:13 | _ |
619620
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:401:9:404:9 | match 43 { ... } |
620-
| test.rs:402:13:402:21 | [match(true)] n @ ... | test.rs:402:13:402:21 | [match(true)] n @ ... |
621-
| test.rs:402:17:402:17 | 1 | test.rs:402:13:402:21 | [match(true)] n @ ... |
621+
| test.rs:402:13:402:13 | n | test.rs:402:13:402:13 | n |
622+
| test.rs:402:13:402:13 | n | test.rs:402:26:402:26 | 2 |
623+
| test.rs:402:17:402:17 | 1 | test.rs:402:13:402:13 | n |
622624
| test.rs:402:17:402:17 | 1 | test.rs:402:17:402:17 | 1 |
623625
| test.rs:402:17:402:17 | 1 | test.rs:402:20:402:21 | 10 |
624-
| test.rs:402:20:402:21 | 10 | test.rs:402:13:402:21 | [match(true)] n @ ... |
626+
| test.rs:402:17:402:17 | 1 | test.rs:402:26:402:26 | 2 |
627+
| test.rs:402:20:402:21 | 10 | test.rs:402:13:402:13 | n |
625628
| test.rs:402:20:402:21 | 10 | test.rs:402:20:402:21 | 10 |
629+
| test.rs:402:20:402:21 | 10 | test.rs:402:26:402:26 | 2 |
630+
| test.rs:402:26:402:26 | 2 | test.rs:402:26:402:26 | 2 |
626631
| test.rs:403:13:403:13 | _ | test.rs:403:13:403:13 | _ |
627632
| test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref | test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref |
628633
| test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref | test.rs:409:9:412:9 | match a { ... } |
629-
| test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref | test.rs:410:13:410:29 | [match(true)] ref mut n @ ... |
634+
| test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref | test.rs:410:21:410:21 | n |
630635
| test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref | test.rs:410:25:410:25 | 1 |
631636
| test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref | test.rs:410:28:410:29 | 10 |
632-
| test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref | test.rs:411:13:411:21 | ref mut n |
637+
| test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref | test.rs:410:35:410:35 | n |
638+
| test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref | test.rs:411:21:411:21 | n |
633639
| test.rs:409:9:412:9 | match a { ... } | test.rs:409:9:412:9 | match a { ... } |
634-
| test.rs:410:13:410:29 | [match(true)] ref mut n @ ... | test.rs:410:13:410:29 | [match(true)] ref mut n @ ... |
635-
| test.rs:410:25:410:25 | 1 | test.rs:410:13:410:29 | [match(true)] ref mut n @ ... |
640+
| test.rs:410:21:410:21 | n | test.rs:410:21:410:21 | n |
641+
| test.rs:410:21:410:21 | n | test.rs:410:35:410:35 | n |
642+
| test.rs:410:25:410:25 | 1 | test.rs:410:21:410:21 | n |
636643
| test.rs:410:25:410:25 | 1 | test.rs:410:25:410:25 | 1 |
637644
| test.rs:410:25:410:25 | 1 | test.rs:410:28:410:29 | 10 |
638-
| test.rs:410:28:410:29 | 10 | test.rs:410:13:410:29 | [match(true)] ref mut n @ ... |
645+
| test.rs:410:25:410:25 | 1 | test.rs:410:35:410:35 | n |
646+
| test.rs:410:28:410:29 | 10 | test.rs:410:21:410:21 | n |
639647
| test.rs:410:28:410:29 | 10 | test.rs:410:28:410:29 | 10 |
640-
| test.rs:411:13:411:21 | ref mut n | test.rs:411:13:411:21 | ref mut n |
648+
| test.rs:410:28:410:29 | 10 | test.rs:410:35:410:35 | n |
649+
| test.rs:410:35:410:35 | n | test.rs:410:35:410:35 | n |
650+
| test.rs:411:21:411:21 | n | test.rs:411:21:411:21 | n |
641651
| test.rs:418:5:423:5 | enter fn test_infinite_loop | test.rs:418:5:423:5 | enter fn test_infinite_loop |
642652
| test.rs:418:5:423:5 | enter fn test_infinite_loop | test.rs:420:13:420:14 | TupleExpr |
643653
| test.rs:420:13:420:14 | TupleExpr | test.rs:420:13:420:14 | TupleExpr |
@@ -1223,25 +1233,29 @@ postDominance
12231233
| test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern | test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern |
12241234
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern |
12251235
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:401:9:404:9 | match 43 { ... } |
1226-
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:402:13:402:21 | [match(true)] n @ ... |
1236+
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:402:13:402:13 | n |
12271237
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:402:17:402:17 | 1 |
12281238
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:402:20:402:21 | 10 |
1239+
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:402:26:402:26 | 2 |
12291240
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:403:13:403:13 | _ |
1230-
| test.rs:402:13:402:21 | [match(true)] n @ ... | test.rs:402:13:402:21 | [match(true)] n @ ... |
1241+
| test.rs:402:13:402:13 | n | test.rs:402:13:402:13 | n |
12311242
| test.rs:402:17:402:17 | 1 | test.rs:402:17:402:17 | 1 |
12321243
| test.rs:402:20:402:21 | 10 | test.rs:402:20:402:21 | 10 |
1244+
| test.rs:402:26:402:26 | 2 | test.rs:402:26:402:26 | 2 |
12331245
| test.rs:403:13:403:13 | _ | test.rs:403:13:403:13 | _ |
12341246
| test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref | test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref |
12351247
| test.rs:409:9:412:9 | match a { ... } | test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref |
12361248
| test.rs:409:9:412:9 | match a { ... } | test.rs:409:9:412:9 | match a { ... } |
1237-
| test.rs:409:9:412:9 | match a { ... } | test.rs:410:13:410:29 | [match(true)] ref mut n @ ... |
1249+
| test.rs:409:9:412:9 | match a { ... } | test.rs:410:21:410:21 | n |
12381250
| test.rs:409:9:412:9 | match a { ... } | test.rs:410:25:410:25 | 1 |
12391251
| test.rs:409:9:412:9 | match a { ... } | test.rs:410:28:410:29 | 10 |
1240-
| test.rs:409:9:412:9 | match a { ... } | test.rs:411:13:411:21 | ref mut n |
1241-
| test.rs:410:13:410:29 | [match(true)] ref mut n @ ... | test.rs:410:13:410:29 | [match(true)] ref mut n @ ... |
1252+
| test.rs:409:9:412:9 | match a { ... } | test.rs:410:35:410:35 | n |
1253+
| test.rs:409:9:412:9 | match a { ... } | test.rs:411:21:411:21 | n |
1254+
| test.rs:410:21:410:21 | n | test.rs:410:21:410:21 | n |
12421255
| test.rs:410:25:410:25 | 1 | test.rs:410:25:410:25 | 1 |
12431256
| test.rs:410:28:410:29 | 10 | test.rs:410:28:410:29 | 10 |
1244-
| test.rs:411:13:411:21 | ref mut n | test.rs:411:13:411:21 | ref mut n |
1257+
| test.rs:410:35:410:35 | n | test.rs:410:35:410:35 | n |
1258+
| test.rs:411:21:411:21 | n | test.rs:411:21:411:21 | n |
12451259
| test.rs:418:5:423:5 | enter fn test_infinite_loop | test.rs:418:5:423:5 | enter fn test_infinite_loop |
12461260
| test.rs:420:13:420:14 | TupleExpr | test.rs:420:13:420:14 | TupleExpr |
12471261
| test.rs:427:5:429:5 | enter fn say_hello | test.rs:427:5:429:5 | enter fn say_hello |
@@ -1503,15 +1517,17 @@ immediateDominator
15031517
| test.rs:395:20:395:20 | 3 | test.rs:395:13:395:13 | 5 |
15041518
| test.rs:396:13:396:13 | _ | test.rs:395:13:395:15 | RangePat |
15051519
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern |
1506-
| test.rs:402:13:402:21 | [match(true)] n @ ... | test.rs:402:20:402:21 | 10 |
1520+
| test.rs:402:13:402:13 | n | test.rs:402:20:402:21 | 10 |
15071521
| test.rs:402:17:402:17 | 1 | test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern |
15081522
| test.rs:402:20:402:21 | 10 | test.rs:402:17:402:17 | 1 |
1523+
| test.rs:402:26:402:26 | 2 | test.rs:402:13:402:13 | n |
15091524
| test.rs:403:13:403:13 | _ | test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern |
15101525
| test.rs:409:9:412:9 | match a { ... } | test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref |
1511-
| test.rs:410:13:410:29 | [match(true)] ref mut n @ ... | test.rs:410:28:410:29 | 10 |
1526+
| test.rs:410:21:410:21 | n | test.rs:410:28:410:29 | 10 |
15121527
| test.rs:410:25:410:25 | 1 | test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref |
15131528
| test.rs:410:28:410:29 | 10 | test.rs:410:25:410:25 | 1 |
1514-
| test.rs:411:13:411:21 | ref mut n | test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref |
1529+
| test.rs:410:35:410:35 | n | test.rs:410:21:410:21 | n |
1530+
| test.rs:411:21:411:21 | n | test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref |
15151531
| test.rs:420:13:420:14 | TupleExpr | test.rs:418:5:423:5 | enter fn test_infinite_loop |
15161532
| test.rs:444:28:449:9 | exit { ... } (normal) | test.rs:444:28:449:9 | enter { ... } |
15171533
| test.rs:445:13:447:13 | if b {...} | test.rs:444:28:449:9 | enter { ... } |
@@ -2011,16 +2027,18 @@ joinBlockPredecessor
20112027
| test.rs:395:13:395:15 | RangePat | test.rs:394:16:394:16 | 2 | 0 |
20122028
| test.rs:396:13:396:13 | _ | test.rs:395:13:395:13 | 5 | 0 |
20132029
| test.rs:396:13:396:13 | _ | test.rs:395:13:395:15 | RangePat | 1 |
2014-
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:402:13:402:21 | [match(true)] n @ ... | 0 |
2030+
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:402:26:402:26 | 2 | 0 |
20152031
| test.rs:401:9:404:9 | match 43 { ... } | test.rs:403:13:403:13 | _ | 1 |
2016-
| test.rs:403:13:403:13 | _ | test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern | 2 |
2017-
| test.rs:403:13:403:13 | _ | test.rs:402:17:402:17 | 1 | 1 |
2018-
| test.rs:403:13:403:13 | _ | test.rs:402:20:402:21 | 10 | 0 |
2019-
| test.rs:409:9:412:9 | match a { ... } | test.rs:410:13:410:29 | [match(true)] ref mut n @ ... | 0 |
2020-
| test.rs:409:9:412:9 | match a { ... } | test.rs:411:13:411:21 | ref mut n | 1 |
2021-
| test.rs:411:13:411:21 | ref mut n | test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref | 2 |
2022-
| test.rs:411:13:411:21 | ref mut n | test.rs:410:25:410:25 | 1 | 1 |
2023-
| test.rs:411:13:411:21 | ref mut n | test.rs:410:28:410:29 | 10 | 0 |
2032+
| test.rs:403:13:403:13 | _ | test.rs:400:5:405:5 | enter fn identifier_pattern_with_subpattern | 3 |
2033+
| test.rs:403:13:403:13 | _ | test.rs:402:13:402:13 | n | 0 |
2034+
| test.rs:403:13:403:13 | _ | test.rs:402:17:402:17 | 1 | 2 |
2035+
| test.rs:403:13:403:13 | _ | test.rs:402:20:402:21 | 10 | 1 |
2036+
| test.rs:409:9:412:9 | match a { ... } | test.rs:410:35:410:35 | n | 0 |
2037+
| test.rs:409:9:412:9 | match a { ... } | test.rs:411:21:411:21 | n | 1 |
2038+
| test.rs:411:21:411:21 | n | test.rs:407:5:414:5 | enter fn identifier_pattern_with_ref | 3 |
2039+
| test.rs:411:21:411:21 | n | test.rs:410:21:410:21 | n | 0 |
2040+
| test.rs:411:21:411:21 | n | test.rs:410:25:410:25 | 1 | 2 |
2041+
| test.rs:411:21:411:21 | n | test.rs:410:28:410:29 | 10 | 1 |
20242042
| test.rs:420:13:420:14 | TupleExpr | test.rs:418:5:423:5 | enter fn test_infinite_loop | 1 |
20252043
| test.rs:420:13:420:14 | TupleExpr | test.rs:420:13:420:14 | TupleExpr | 0 |
20262044
| test.rs:444:28:449:9 | exit { ... } (normal) | test.rs:445:13:447:13 | if b {...} | 1 |

0 commit comments

Comments
 (0)