From 9392bf796afe4df8af0a4b7442ad87132ee6f747 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Wed, 6 Nov 2024 09:21:52 +0100 Subject: [PATCH] Rust: Include literals in the CFG for literal patterns --- .../internal/ControlFlowGraphImpl.qll | 6 ++++-- .../library-tests/controlflow/Cfg.expected | 21 ++++++++++++------- .../test/library-tests/variables/Cfg.expected | 15 ++++++++----- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/rust/ql/lib/codeql/rust/controlflow/internal/ControlFlowGraphImpl.qll b/rust/ql/lib/codeql/rust/controlflow/internal/ControlFlowGraphImpl.qll index 72a8098eb106..173bb8876bc8 100644 --- a/rust/ql/lib/codeql/rust/controlflow/internal/ControlFlowGraphImpl.qll +++ b/rust/ql/lib/codeql/rust/controlflow/internal/ControlFlowGraphImpl.qll @@ -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 { @@ -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() } diff --git a/rust/ql/test/library-tests/controlflow/Cfg.expected b/rust/ql/test/library-tests/controlflow/Cfg.expected index cdbf56592c9a..f2637a115c44 100644 --- a/rust/ql/test/library-tests/controlflow/Cfg.expected +++ b/rust/ql/test/library-tests/controlflow/Cfg.expected @@ -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 | @@ -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 | | @@ -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 | diff --git a/rust/ql/test/library-tests/variables/Cfg.expected b/rust/ql/test/library-tests/variables/Cfg.expected index f9bd23630192..90aac4b9627c 100644 --- a/rust/ql/test/library-tests/variables/Cfg.expected +++ b/rust/ql/test/library-tests/variables/Cfg.expected @@ -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" | | @@ -358,10 +359,12 @@ 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 | | @@ -369,10 +372,12 @@ edges | 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 | |