Skip to content

Commit

Permalink
Merge pull request #17915 from hvitved/rust/pat-literal-cfg
Browse files Browse the repository at this point in the history
Rust: Include literals in the CFG for literal patterns
  • Loading branch information
hvitved authored Nov 6, 2024
2 parents 6054855 + 9392bf7 commit f8058e4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ module ExprTrees {
* Provides `ControlFlowTree`s for patterns.
*
* Since patterns destruct values, they are modeled in pre-order, except for
* `OrPat`s and `IdentPat`s.
* `LiteralPat`s, `OrPat`s, and `IdentPat`s.
*/
module PatternTrees {
abstract class StandardPatTree extends StandardTree {
Expand Down Expand Up @@ -665,7 +665,9 @@ module PatternTrees {

class RestPatTree extends LeafTree, RestPat { }

class LiteralPatTree extends LeafTree, LiteralPat { }
class LiteralPatTree extends StandardPostOrderTree, LiteralPat {
override AstNode getChildNode(int i) { i = 0 and result = this.getLiteral() }
}

class MacroPatTree extends PreOrderPatTree, MacroPat {
override Pat getPat(int i) { i = 0 and result = this.getMacroCall().getExpanded() }
Expand Down
21 changes: 14 additions & 7 deletions rust/ql/test/library-tests/controlflow/Cfg.expected
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ edges
| test.rs:153:9:160:9 | IfExpr | test.rs:152:44:161:5 | BlockExpr | |
| test.rs:153:13:156:9 | [boolean(false)] MatchExpr | test.rs:159:13:159:13 | 0 | false |
| test.rs:153:13:156:9 | [boolean(true)] MatchExpr | test.rs:157:13:157:13 | 1 | true |
| test.rs:153:19:153:19 | a | test.rs:154:13:154:13 | LiteralPat | |
| test.rs:153:19:153:19 | a | test.rs:154:13:154:13 | 0 | |
| test.rs:154:13:154:13 | 0 | test.rs:154:13:154:13 | LiteralPat | |
| test.rs:154:13:154:13 | LiteralPat | test.rs:154:18:154:21 | true | match |
| test.rs:154:13:154:13 | LiteralPat | test.rs:155:13:155:13 | WildcardPat | no-match |
| test.rs:154:18:154:21 | true | test.rs:153:13:156:9 | [boolean(true)] MatchExpr | true |
Expand Down Expand Up @@ -590,13 +591,15 @@ edges
| test.rs:274:9:277:9 | MatchExpr | test.rs:273:71:278:5 | BlockExpr | |
| test.rs:274:15:274:15 | b | test.rs:274:15:274:16 | TryExpr | |
| test.rs:274:15:274:16 | TryExpr | test.rs:273:5:278:5 | exit test_question_mark_operator_2 (normal) | return |
| test.rs:274:15:274:16 | TryExpr | test.rs:275:13:275:16 | LiteralPat | match |
| test.rs:274:15:274:16 | TryExpr | test.rs:275:13:275:16 | true | match |
| test.rs:275:13:275:16 | LiteralPat | test.rs:275:21:275:24 | PathExpr | match |
| test.rs:275:13:275:16 | LiteralPat | test.rs:276:13:276:17 | LiteralPat | no-match |
| test.rs:275:13:275:16 | LiteralPat | test.rs:276:13:276:17 | false | no-match |
| test.rs:275:13:275:16 | true | test.rs:275:13:275:16 | LiteralPat | |
| test.rs:275:21:275:24 | PathExpr | test.rs:275:26:275:30 | false | |
| test.rs:275:21:275:31 | CallExpr | test.rs:274:9:277:9 | MatchExpr | |
| test.rs:275:26:275:30 | false | test.rs:275:21:275:31 | CallExpr | |
| test.rs:276:13:276:17 | LiteralPat | test.rs:276:22:276:25 | PathExpr | match |
| test.rs:276:13:276:17 | false | test.rs:276:13:276:17 | LiteralPat | |
| test.rs:276:22:276:25 | PathExpr | test.rs:276:27:276:30 | true | |
| test.rs:276:22:276:31 | CallExpr | test.rs:274:9:277:9 | MatchExpr | |
| test.rs:276:27:276:30 | true | test.rs:276:22:276:31 | CallExpr | |
Expand Down Expand Up @@ -735,21 +738,25 @@ edges
| test.rs:348:31:355:5 | BlockExpr | test.rs:348:5:355:5 | exit range_pattern (normal) | |
| test.rs:349:9:354:9 | MatchExpr | test.rs:348:31:355:5 | BlockExpr | |
| test.rs:349:15:349:16 | 42 | test.rs:350:13:350:15 | RangePat | |
| test.rs:350:13:350:15 | RangePat | test.rs:350:15:350:15 | LiteralPat | match |
| test.rs:350:13:350:15 | RangePat | test.rs:350:15:350:15 | 0 | match |
| test.rs:350:13:350:15 | RangePat | test.rs:351:13:351:16 | RangePat | no-match |
| test.rs:350:15:350:15 | 0 | test.rs:350:15:350:15 | LiteralPat | |
| test.rs:350:15:350:15 | LiteralPat | test.rs:350:20:350:20 | 1 | match |
| test.rs:350:15:350:15 | LiteralPat | test.rs:351:13:351:16 | RangePat | no-match |
| test.rs:350:20:350:20 | 1 | test.rs:349:9:354:9 | MatchExpr | |
| test.rs:351:13:351:13 | LiteralPat | test.rs:351:16:351:16 | LiteralPat | match |
| test.rs:351:13:351:13 | 1 | test.rs:351:13:351:13 | LiteralPat | |
| test.rs:351:13:351:13 | LiteralPat | test.rs:351:16:351:16 | 2 | match |
| test.rs:351:13:351:13 | LiteralPat | test.rs:352:13:352:15 | RangePat | no-match |
| test.rs:351:13:351:16 | RangePat | test.rs:351:13:351:13 | LiteralPat | match |
| test.rs:351:13:351:16 | RangePat | test.rs:351:13:351:13 | 1 | match |
| test.rs:351:13:351:16 | RangePat | test.rs:352:13:352:15 | RangePat | no-match |
| test.rs:351:16:351:16 | 2 | test.rs:351:16:351:16 | LiteralPat | |
| test.rs:351:16:351:16 | LiteralPat | test.rs:351:21:351:21 | 2 | match |
| test.rs:351:16:351:16 | LiteralPat | test.rs:352:13:352:15 | RangePat | no-match |
| test.rs:351:21:351:21 | 2 | test.rs:349:9:354:9 | MatchExpr | |
| test.rs:352:13:352:13 | 5 | test.rs:352:13:352:13 | LiteralPat | |
| test.rs:352:13:352:13 | LiteralPat | test.rs:352:20:352:20 | 3 | match |
| test.rs:352:13:352:13 | LiteralPat | test.rs:353:13:353:14 | RestPat | no-match |
| test.rs:352:13:352:15 | RangePat | test.rs:352:13:352:13 | LiteralPat | match |
| test.rs:352:13:352:15 | RangePat | test.rs:352:13:352:13 | 5 | match |
| test.rs:352:13:352:15 | RangePat | test.rs:353:13:353:14 | RestPat | no-match |
| test.rs:352:20:352:20 | 3 | test.rs:349:9:354:9 | MatchExpr | |
| test.rs:353:13:353:14 | RestPat | test.rs:353:19:353:19 | 4 | match |
Expand Down
15 changes: 10 additions & 5 deletions rust/ql/test/library-tests/variables/Cfg.expected
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,9 @@ edges
| variables.rs:114:5:122:5 | ExprStmt | variables.rs:114:11:114:12 | x6 | |
| variables.rs:114:5:122:5 | MatchExpr | variables.rs:124:5:124:18 | ExprStmt | |
| variables.rs:114:11:114:12 | x6 | variables.rs:115:9:115:16 | TupleStructPat | |
| variables.rs:115:9:115:16 | TupleStructPat | variables.rs:115:14:115:15 | LiteralPat | match |
| variables.rs:115:9:115:16 | TupleStructPat | variables.rs:115:14:115:15 | 50 | match |
| variables.rs:115:9:115:16 | TupleStructPat | variables.rs:116:9:116:16 | TupleStructPat | no-match |
| variables.rs:115:14:115:15 | 50 | variables.rs:115:14:115:15 | LiteralPat | |
| variables.rs:115:14:115:15 | LiteralPat | variables.rs:115:21:115:29 | PathExpr | match |
| variables.rs:115:14:115:15 | LiteralPat | variables.rs:116:9:116:16 | TupleStructPat | no-match |
| variables.rs:115:21:115:29 | PathExpr | variables.rs:115:31:115:38 | "Got 50" | |
Expand Down Expand Up @@ -358,21 +359,25 @@ edges
| variables.rs:172:9:174:9 | RecordPat | variables.rs:173:31:173:35 | RangePat | match |
| variables.rs:172:9:174:9 | RecordPat | variables.rs:175:9:175:38 | RecordPat | no-match |
| variables.rs:173:17:173:35 | [match(true)] id_variable | variables.rs:174:14:174:22 | PathExpr | match |
| variables.rs:173:31:173:31 | LiteralPat | variables.rs:173:35:173:35 | LiteralPat | match |
| variables.rs:173:31:173:31 | 3 | variables.rs:173:31:173:31 | LiteralPat | |
| variables.rs:173:31:173:31 | LiteralPat | variables.rs:173:35:173:35 | 7 | match |
| variables.rs:173:31:173:31 | LiteralPat | variables.rs:175:9:175:38 | RecordPat | no-match |
| variables.rs:173:31:173:35 | RangePat | variables.rs:173:31:173:31 | LiteralPat | match |
| variables.rs:173:31:173:35 | RangePat | variables.rs:173:31:173:31 | 3 | match |
| variables.rs:173:31:173:35 | RangePat | variables.rs:175:9:175:38 | RecordPat | no-match |
| variables.rs:173:35:173:35 | 7 | variables.rs:173:35:173:35 | LiteralPat | |
| variables.rs:173:35:173:35 | LiteralPat | variables.rs:173:17:173:35 | [match(true)] id_variable | match |
| variables.rs:173:35:173:35 | LiteralPat | variables.rs:175:9:175:38 | RecordPat | no-match |
| variables.rs:174:14:174:22 | PathExpr | variables.rs:174:24:174:34 | id_variable | |
| variables.rs:174:14:174:35 | CallExpr | variables.rs:171:5:180:5 | MatchExpr | |
| variables.rs:174:24:174:34 | id_variable | variables.rs:174:14:174:35 | CallExpr | |
| variables.rs:175:9:175:38 | RecordPat | variables.rs:175:30:175:36 | RangePat | match |
| variables.rs:175:9:175:38 | RecordPat | variables.rs:178:9:178:29 | RecordPat | no-match |
| variables.rs:175:30:175:31 | LiteralPat | variables.rs:175:35:175:36 | LiteralPat | match |
| variables.rs:175:30:175:31 | 10 | variables.rs:175:30:175:31 | LiteralPat | |
| variables.rs:175:30:175:31 | LiteralPat | variables.rs:175:35:175:36 | 12 | match |
| variables.rs:175:30:175:31 | LiteralPat | variables.rs:178:9:178:29 | RecordPat | no-match |
| variables.rs:175:30:175:36 | RangePat | variables.rs:175:30:175:31 | LiteralPat | match |
| variables.rs:175:30:175:36 | RangePat | variables.rs:175:30:175:31 | 10 | match |
| variables.rs:175:30:175:36 | RangePat | variables.rs:178:9:178:29 | RecordPat | no-match |
| variables.rs:175:35:175:36 | 12 | variables.rs:175:35:175:36 | LiteralPat | |
| variables.rs:175:35:175:36 | LiteralPat | variables.rs:176:22:176:51 | MacroStmts | match |
| variables.rs:175:35:175:36 | LiteralPat | variables.rs:178:9:178:29 | RecordPat | no-match |
| variables.rs:175:43:177:9 | BlockExpr | variables.rs:171:5:180:5 | MatchExpr | |
Expand Down

0 comments on commit f8058e4

Please sign in to comment.