Skip to content

Commit 8c74478

Browse files
authored
Merge pull request #18041 from paldepind/rust-cfg-self
Rust: Include `self` parameters in the CFG
2 parents 82ca369 + 93f6f04 commit 8c74478

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,12 @@ class CallableScopeTree extends StandardTree, PreOrderTree, PostOrderTree, Scope
7373
override predicate propagatesAbnormal(AstNode child) { none() }
7474

7575
override AstNode getChildNode(int i) {
76-
result = this.getParamList().getParam(i)
76+
i = 0 and
77+
result = this.getParamList().getSelfParam()
7778
or
78-
i = this.getParamList().getNumberOfParams() and
79+
result = this.getParamList().getParam(i - 1)
80+
or
81+
i = this.getParamList().getNumberOfParams() + 1 and
7982
result = this.getBody()
8083
}
8184
}
@@ -191,6 +194,10 @@ class NameTree extends LeafTree, Name { }
191194

192195
class NameRefTree extends LeafTree, NameRef { }
193196

197+
class SelfParamTree extends StandardPostOrderTree, SelfParam {
198+
override AstNode getChildNode(int i) { i = 0 and result = this.getName() }
199+
}
200+
194201
class TypeRefTree extends LeafTree instanceof TypeRef { }
195202

196203
/**

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,40 @@ edges
10441044
| test.rs:490:5:490:19 | ExprStmt | test.rs:490:5:490:10 | nested | |
10451045
| test.rs:490:12:490:17 | RefExpr | test.rs:490:5:490:18 | CallExpr | |
10461046
| test.rs:490:17:490:17 | x | test.rs:490:12:490:17 | RefExpr | |
1047+
| test.rs:502:5:504:5 | enter new | test.rs:502:12:502:12 | a | |
1048+
| test.rs:502:5:504:5 | exit new (normal) | test.rs:502:5:504:5 | exit new | |
1049+
| test.rs:502:12:502:12 | a | test.rs:502:12:502:17 | Param | match |
1050+
| test.rs:502:12:502:17 | Param | test.rs:503:23:503:23 | a | |
1051+
| test.rs:502:28:504:5 | BlockExpr | test.rs:502:5:504:5 | exit new (normal) | |
1052+
| test.rs:503:9:503:25 | RecordExpr | test.rs:502:28:504:5 | BlockExpr | |
1053+
| test.rs:503:23:503:23 | a | test.rs:503:9:503:25 | RecordExpr | |
1054+
| test.rs:506:5:508:5 | enter negated | test.rs:506:16:506:19 | self | |
1055+
| test.rs:506:5:508:5 | exit negated (normal) | test.rs:506:5:508:5 | exit negated | |
1056+
| test.rs:506:16:506:19 | SelfParam | test.rs:507:23:507:26 | self | |
1057+
| test.rs:506:16:506:19 | self | test.rs:506:16:506:19 | SelfParam | |
1058+
| test.rs:506:30:508:5 | BlockExpr | test.rs:506:5:508:5 | exit negated (normal) | |
1059+
| test.rs:507:9:507:30 | RecordExpr | test.rs:506:30:508:5 | BlockExpr | |
1060+
| test.rs:507:23:507:26 | self | test.rs:507:23:507:28 | FieldExpr | |
1061+
| test.rs:507:23:507:28 | FieldExpr | test.rs:507:9:507:30 | RecordExpr | |
1062+
| test.rs:510:5:512:5 | enter multifly_add | test.rs:510:26:510:29 | self | |
1063+
| test.rs:510:5:512:5 | exit multifly_add (normal) | test.rs:510:5:512:5 | exit multifly_add | |
1064+
| test.rs:510:21:510:29 | SelfParam | test.rs:510:32:510:32 | a | |
1065+
| test.rs:510:26:510:29 | self | test.rs:510:21:510:29 | SelfParam | |
1066+
| test.rs:510:32:510:32 | a | test.rs:510:32:510:37 | Param | match |
1067+
| test.rs:510:32:510:37 | Param | test.rs:510:40:510:40 | b | |
1068+
| test.rs:510:40:510:40 | b | test.rs:510:40:510:45 | Param | match |
1069+
| test.rs:510:40:510:45 | Param | test.rs:511:9:511:34 | ExprStmt | |
1070+
| test.rs:510:48:512:5 | BlockExpr | test.rs:510:5:512:5 | exit multifly_add (normal) | |
1071+
| test.rs:511:9:511:12 | self | test.rs:511:9:511:14 | FieldExpr | |
1072+
| test.rs:511:9:511:14 | FieldExpr | test.rs:511:19:511:22 | self | |
1073+
| test.rs:511:9:511:33 | ... = ... | test.rs:510:48:512:5 | BlockExpr | |
1074+
| test.rs:511:9:511:34 | ExprStmt | test.rs:511:9:511:12 | self | |
1075+
| test.rs:511:18:511:33 | ... + ... | test.rs:511:9:511:33 | ... = ... | |
1076+
| test.rs:511:19:511:22 | self | test.rs:511:19:511:24 | FieldExpr | |
1077+
| test.rs:511:19:511:24 | FieldExpr | test.rs:511:28:511:28 | a | |
1078+
| test.rs:511:19:511:28 | ... * ... | test.rs:511:33:511:33 | b | |
1079+
| test.rs:511:28:511:28 | a | test.rs:511:19:511:28 | ... * ... | |
1080+
| test.rs:511:33:511:33 | b | test.rs:511:18:511:33 | ... + ... | |
10471081
breakTarget
10481082
| test.rs:34:17:34:21 | BreakExpr | test.rs:28:9:40:9 | LoopExpr |
10491083
| test.rs:48:21:48:25 | BreakExpr | test.rs:46:13:53:13 | LoopExpr |

rust/ql/test/library-tests/controlflow/test.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,21 @@ fn test_nested_function2() {
493493
trait MyFrom<T> {
494494
fn my_from(x: T) -> Self;
495495
}
496+
497+
struct MyNumber {
498+
n: i64,
499+
}
500+
501+
impl MyNumber {
502+
fn new(a: i64) -> Self {
503+
MyNumber { n: a }
504+
}
505+
506+
fn negated(self) -> Self {
507+
MyNumber { n: self.n }
508+
}
509+
510+
fn multifly_add(&mut self, a: i64, b: i64) {
511+
self.n = (self.n * a) + b;
512+
}
513+
}

rust/ql/test/library-tests/variables/Cfg.expected

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,8 +1070,10 @@ edges
10701070
| variables.rs:472:9:472:20 | CallExpr | variables.rs:471:12:473:5 | BlockExpr | |
10711071
| variables.rs:472:9:472:21 | ExprStmt | variables.rs:472:9:472:17 | print_i64 | |
10721072
| variables.rs:472:19:472:19 | x | variables.rs:472:9:472:20 | CallExpr | |
1073-
| variables.rs:482:5:484:5 | enter my_get | variables.rs:483:9:483:24 | ExprStmt | |
1073+
| variables.rs:482:5:484:5 | enter my_get | variables.rs:482:20:482:23 | self | |
10741074
| variables.rs:482:5:484:5 | exit my_get (normal) | variables.rs:482:5:484:5 | exit my_get | |
1075+
| variables.rs:482:15:482:23 | SelfParam | variables.rs:483:9:483:24 | ExprStmt | |
1076+
| variables.rs:482:20:482:23 | self | variables.rs:482:15:482:23 | SelfParam | |
10751077
| variables.rs:483:9:483:23 | ReturnExpr | variables.rs:482:5:484:5 | exit my_get (normal) | return |
10761078
| variables.rs:483:9:483:24 | ExprStmt | variables.rs:483:16:483:19 | self | |
10771079
| variables.rs:483:16:483:19 | self | variables.rs:483:16:483:23 | FieldExpr | |
@@ -1131,8 +1133,10 @@ edges
11311133
| variables.rs:502:5:502:22 | ExprStmt | variables.rs:502:5:502:17 | print_i64_ref | |
11321134
| variables.rs:502:19:502:20 | RefExpr | variables.rs:502:5:502:21 | CallExpr | |
11331135
| variables.rs:502:20:502:20 | z | variables.rs:502:19:502:20 | RefExpr | |
1134-
| variables.rs:510:3:512:3 | enter bar | variables.rs:511:5:511:32 | ExprStmt | |
1136+
| variables.rs:510:3:512:3 | enter bar | variables.rs:510:15:510:18 | self | |
11351137
| variables.rs:510:3:512:3 | exit bar (normal) | variables.rs:510:3:512:3 | exit bar | |
1138+
| variables.rs:510:10:510:18 | SelfParam | variables.rs:511:5:511:32 | ExprStmt | |
1139+
| variables.rs:510:15:510:18 | self | variables.rs:510:10:510:18 | SelfParam | |
11361140
| variables.rs:510:21:512:3 | BlockExpr | variables.rs:510:3:512:3 | exit bar (normal) | |
11371141
| variables.rs:511:5:511:9 | * ... | variables.rs:511:29:511:29 | 3 | |
11381142
| variables.rs:511:5:511:31 | ... = ... | variables.rs:510:21:512:3 | BlockExpr | |

0 commit comments

Comments
 (0)