From f02d4a977d6dbc0d24de8bf8b05b47d1a4fb838c Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Thu, 11 Feb 2021 16:37:03 +0100 Subject: [PATCH 1/5] AST: some statement tests --- .../ast/control/ControlExpr.expected | 14 ++--- .../library-tests/ast/control/Loop.expected | 52 +++++++++---------- ql/test/library-tests/ast/control/loops.rb | 2 + 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/ql/test/library-tests/ast/control/ControlExpr.expected b/ql/test/library-tests/ast/control/ControlExpr.expected index 3dcbc7b969b..3273ce0c6a4 100644 --- a/ql/test/library-tests/ast/control/ControlExpr.expected +++ b/ql/test/library-tests/ast/control/ControlExpr.expected @@ -15,10 +15,10 @@ | loops.rb:9:1:12:3 | for ... in ... | ForExpr | | loops.rb:16:1:19:3 | for ... in ... | ForExpr | | loops.rb:22:1:25:3 | for ... in ... | ForExpr | -| loops.rb:28:1:31:3 | for ... in ... | ForExpr | -| loops.rb:34:1:37:3 | while ... | WhileExpr | -| loops.rb:40:1:43:3 | while ... | WhileExpr | -| loops.rb:46:1:46:19 | ... while ... | WhileModifierExpr | -| loops.rb:49:1:52:3 | until ... | UntilExpr | -| loops.rb:55:1:58:3 | until ... | UntilExpr | -| loops.rb:61:1:61:19 | ... until ... | UntilModifierExpr | +| loops.rb:28:1:32:3 | for ... in ... | ForExpr | +| loops.rb:35:1:39:3 | while ... | WhileExpr | +| loops.rb:42:1:45:3 | while ... | WhileExpr | +| loops.rb:48:1:48:19 | ... while ... | WhileModifierExpr | +| loops.rb:51:1:54:3 | until ... | UntilExpr | +| loops.rb:57:1:60:3 | until ... | UntilExpr | +| loops.rb:63:1:63:19 | ... until ... | UntilModifierExpr | diff --git a/ql/test/library-tests/ast/control/Loop.expected b/ql/test/library-tests/ast/control/Loop.expected index 81f479ae4e6..0da638e8b21 100644 --- a/ql/test/library-tests/ast/control/Loop.expected +++ b/ql/test/library-tests/ast/control/Loop.expected @@ -2,20 +2,20 @@ loops | loops.rb:9:1:12:3 | for ... in ... | ForExpr | loops.rb:9:15:12:3 | ...; ... | ExprSequence | | loops.rb:16:1:19:3 | for ... in ... | ForExpr | loops.rb:16:15:19:3 | ...; ... | ExprSequence | | loops.rb:22:1:25:3 | for ... in ... | ForExpr | loops.rb:22:35:25:3 | ...; ... | ExprSequence | -| loops.rb:28:1:31:3 | for ... in ... | ForExpr | loops.rb:28:37:31:3 | ...; ... | ExprSequence | -| loops.rb:34:1:37:3 | while ... | WhileExpr | loops.rb:34:12:37:3 | ...; ... | ExprSequence | -| loops.rb:40:1:43:3 | while ... | WhileExpr | loops.rb:40:13:43:3 | ...; ... | ExprSequence | -| loops.rb:46:1:46:19 | ... while ... | WhileModifierExpr | loops.rb:46:1:46:6 | ... += ... | AssignAddExpr | -| loops.rb:49:1:52:3 | until ... | UntilExpr | loops.rb:49:13:52:3 | ...; ... | ExprSequence | -| loops.rb:55:1:58:3 | until ... | UntilExpr | loops.rb:55:13:58:3 | ...; ... | ExprSequence | -| loops.rb:61:1:61:19 | ... until ... | UntilModifierExpr | loops.rb:61:1:61:6 | ... -= ... | AssignSubExpr | +| loops.rb:28:1:32:3 | for ... in ... | ForExpr | loops.rb:28:37:32:3 | ...; ... | ExprSequence | +| loops.rb:35:1:39:3 | while ... | WhileExpr | loops.rb:35:12:39:3 | ...; ... | ExprSequence | +| loops.rb:42:1:45:3 | while ... | WhileExpr | loops.rb:42:13:45:3 | ...; ... | ExprSequence | +| loops.rb:48:1:48:19 | ... while ... | WhileModifierExpr | loops.rb:48:1:48:6 | ... += ... | AssignAddExpr | +| loops.rb:51:1:54:3 | until ... | UntilExpr | loops.rb:51:13:54:3 | ...; ... | ExprSequence | +| loops.rb:57:1:60:3 | until ... | UntilExpr | loops.rb:57:13:60:3 | ...; ... | ExprSequence | +| loops.rb:63:1:63:19 | ... until ... | UntilModifierExpr | loops.rb:63:1:63:6 | ... -= ... | AssignSubExpr | conditionalLoops -| loops.rb:34:1:37:3 | while ... | WhileExpr | loops.rb:34:7:34:11 | ... < ... | loops.rb:34:12:37:3 | ...; ... | ExprSequence | -| loops.rb:40:1:43:3 | while ... | WhileExpr | loops.rb:40:7:40:11 | ... < ... | loops.rb:40:13:43:3 | ...; ... | ExprSequence | -| loops.rb:46:1:46:19 | ... while ... | WhileModifierExpr | loops.rb:46:14:46:19 | ... >= ... | loops.rb:46:1:46:6 | ... += ... | AssignAddExpr | -| loops.rb:49:1:52:3 | until ... | UntilExpr | loops.rb:49:7:49:12 | ... == ... | loops.rb:49:13:52:3 | ...; ... | ExprSequence | -| loops.rb:55:1:58:3 | until ... | UntilExpr | loops.rb:55:7:55:11 | ... > ... | loops.rb:55:13:58:3 | ...; ... | ExprSequence | -| loops.rb:61:1:61:19 | ... until ... | UntilModifierExpr | loops.rb:61:14:61:19 | ... == ... | loops.rb:61:1:61:6 | ... -= ... | AssignSubExpr | +| loops.rb:35:1:39:3 | while ... | WhileExpr | loops.rb:35:7:35:11 | ... < ... | loops.rb:35:12:39:3 | ...; ... | ExprSequence | +| loops.rb:42:1:45:3 | while ... | WhileExpr | loops.rb:42:7:42:11 | ... < ... | loops.rb:42:13:45:3 | ...; ... | ExprSequence | +| loops.rb:48:1:48:19 | ... while ... | WhileModifierExpr | loops.rb:48:14:48:19 | ... >= ... | loops.rb:48:1:48:6 | ... += ... | AssignAddExpr | +| loops.rb:51:1:54:3 | until ... | UntilExpr | loops.rb:51:7:51:12 | ... == ... | loops.rb:51:13:54:3 | ...; ... | ExprSequence | +| loops.rb:57:1:60:3 | until ... | UntilExpr | loops.rb:57:7:57:11 | ... > ... | loops.rb:57:13:60:3 | ...; ... | ExprSequence | +| loops.rb:63:1:63:19 | ... until ... | UntilModifierExpr | loops.rb:63:14:63:19 | ... == ... | loops.rb:63:1:63:6 | ... -= ... | AssignSubExpr | forExprs | loops.rb:9:1:12:3 | for ... in ... | loops.rb:9:5:9:5 | n | loops.rb:9:15:12:3 | ...; ... | 0 | loops.rb:10:5:10:12 | ... += ... | | loops.rb:9:1:12:3 | for ... in ... | loops.rb:9:5:9:5 | n | loops.rb:9:15:12:3 | ...; ... | 1 | loops.rb:11:5:11:11 | ... = ... | @@ -23,23 +23,23 @@ forExprs | loops.rb:16:1:19:3 | for ... in ... | loops.rb:16:5:16:5 | n | loops.rb:16:15:19:3 | ...; ... | 1 | loops.rb:18:5:18:12 | ... -= ... | | loops.rb:22:1:25:3 | for ... in ... | loops.rb:22:5:22:14 | (..., ...) | loops.rb:22:35:25:3 | ...; ... | 0 | loops.rb:23:3:23:14 | ... += ... | | loops.rb:22:1:25:3 | for ... in ... | loops.rb:22:5:22:14 | (..., ...) | loops.rb:22:35:25:3 | ...; ... | 1 | loops.rb:24:3:24:14 | ... *= ... | -| loops.rb:28:1:31:3 | for ... in ... | loops.rb:28:5:28:16 | (..., ...) | loops.rb:28:37:31:3 | ...; ... | 0 | loops.rb:29:3:29:14 | ... += ... | -| loops.rb:28:1:31:3 | for ... in ... | loops.rb:28:5:28:16 | (..., ...) | loops.rb:28:37:31:3 | ...; ... | 1 | loops.rb:30:3:30:14 | ... /= ... | +| loops.rb:28:1:32:3 | for ... in ... | loops.rb:28:5:28:16 | (..., ...) | loops.rb:28:37:32:3 | ...; ... | 0 | loops.rb:29:3:29:14 | ... += ... | +| loops.rb:28:1:32:3 | for ... in ... | loops.rb:28:5:28:16 | (..., ...) | loops.rb:28:37:32:3 | ...; ... | 1 | loops.rb:30:3:30:14 | ... /= ... | forExprsTuplePatterns | loops.rb:22:1:25:3 | for ... in ... | loops.rb:22:5:22:14 | (..., ...) | 0 | loops.rb:22:5:22:7 | key | | loops.rb:22:1:25:3 | for ... in ... | loops.rb:22:5:22:14 | (..., ...) | 1 | loops.rb:22:10:22:14 | value | -| loops.rb:28:1:31:3 | for ... in ... | loops.rb:28:5:28:16 | (..., ...) | 0 | loops.rb:28:5:28:16 | (..., ...) | +| loops.rb:28:1:32:3 | for ... in ... | loops.rb:28:5:28:16 | (..., ...) | 0 | loops.rb:28:5:28:16 | (..., ...) | whileExprs -| loops.rb:34:1:37:3 | while ... | loops.rb:34:7:34:11 | ... < ... | loops.rb:34:12:37:3 | ...; ... | 0 | loops.rb:35:3:35:8 | ... += ... | -| loops.rb:34:1:37:3 | while ... | loops.rb:34:7:34:11 | ... < ... | loops.rb:34:12:37:3 | ...; ... | 1 | loops.rb:36:3:36:8 | ... += ... | -| loops.rb:40:1:43:3 | while ... | loops.rb:40:7:40:11 | ... < ... | loops.rb:40:13:43:3 | ...; ... | 0 | loops.rb:41:3:41:8 | ... += ... | -| loops.rb:40:1:43:3 | while ... | loops.rb:40:7:40:11 | ... < ... | loops.rb:40:13:43:3 | ...; ... | 1 | loops.rb:42:3:42:8 | ... += ... | +| loops.rb:35:1:39:3 | while ... | loops.rb:35:7:35:11 | ... < ... | loops.rb:35:12:39:3 | ...; ... | 0 | loops.rb:36:3:36:8 | ... += ... | +| loops.rb:35:1:39:3 | while ... | loops.rb:35:7:35:11 | ... < ... | loops.rb:35:12:39:3 | ...; ... | 1 | loops.rb:37:3:37:8 | ... += ... | +| loops.rb:42:1:45:3 | while ... | loops.rb:42:7:42:11 | ... < ... | loops.rb:42:13:45:3 | ...; ... | 0 | loops.rb:43:3:43:8 | ... += ... | +| loops.rb:42:1:45:3 | while ... | loops.rb:42:7:42:11 | ... < ... | loops.rb:42:13:45:3 | ...; ... | 1 | loops.rb:44:3:44:8 | ... += ... | whileModifierExprs -| loops.rb:46:1:46:19 | ... while ... | loops.rb:46:14:46:19 | ... >= ... | loops.rb:46:1:46:6 | ... += ... | +| loops.rb:48:1:48:19 | ... while ... | loops.rb:48:14:48:19 | ... >= ... | loops.rb:48:1:48:6 | ... += ... | untilExprs -| loops.rb:49:1:52:3 | until ... | loops.rb:49:7:49:12 | ... == ... | loops.rb:49:13:52:3 | ...; ... | 0 | loops.rb:50:3:50:8 | ... += ... | -| loops.rb:49:1:52:3 | until ... | loops.rb:49:7:49:12 | ... == ... | loops.rb:49:13:52:3 | ...; ... | 1 | loops.rb:51:3:51:8 | ... -= ... | -| loops.rb:55:1:58:3 | until ... | loops.rb:55:7:55:11 | ... > ... | loops.rb:55:13:58:3 | ...; ... | 0 | loops.rb:56:3:56:8 | ... += ... | -| loops.rb:55:1:58:3 | until ... | loops.rb:55:7:55:11 | ... > ... | loops.rb:55:13:58:3 | ...; ... | 1 | loops.rb:57:3:57:8 | ... -= ... | +| loops.rb:51:1:54:3 | until ... | loops.rb:51:7:51:12 | ... == ... | loops.rb:51:13:54:3 | ...; ... | 0 | loops.rb:52:3:52:8 | ... += ... | +| loops.rb:51:1:54:3 | until ... | loops.rb:51:7:51:12 | ... == ... | loops.rb:51:13:54:3 | ...; ... | 1 | loops.rb:53:3:53:8 | ... -= ... | +| loops.rb:57:1:60:3 | until ... | loops.rb:57:7:57:11 | ... > ... | loops.rb:57:13:60:3 | ...; ... | 0 | loops.rb:58:3:58:8 | ... += ... | +| loops.rb:57:1:60:3 | until ... | loops.rb:57:7:57:11 | ... > ... | loops.rb:57:13:60:3 | ...; ... | 1 | loops.rb:59:3:59:8 | ... -= ... | untilModifierExprs -| loops.rb:61:1:61:19 | ... until ... | loops.rb:61:14:61:19 | ... == ... | loops.rb:61:1:61:6 | ... -= ... | +| loops.rb:63:1:63:19 | ... until ... | loops.rb:63:14:63:19 | ... == ... | loops.rb:63:1:63:6 | ... -= ... | diff --git a/ql/test/library-tests/ast/control/loops.rb b/ql/test/library-tests/ast/control/loops.rb index a400037caaa..e6bead6c401 100644 --- a/ql/test/library-tests/ast/control/loops.rb +++ b/ql/test/library-tests/ast/control/loops.rb @@ -28,12 +28,14 @@ end for (key, value) in {foo: 0, bar: 1} sum += value foo /= value + break end # While loop while x < y x += 1 z += 1 + next end # While loop with `do` keyword From fd6aeba9f596567c3b295a59a8a4ef0065a87922 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Thu, 11 Feb 2021 15:25:54 +0100 Subject: [PATCH 2/5] AST: make Expr extend Stmt --- ql/src/codeql_ruby/ast/Expr.qll | 17 +---------------- ql/src/codeql_ruby/ast/Statement.qll | 4 ++++ ql/src/codeql_ruby/ast/internal/Expr.qll | 6 ++---- 3 files changed, 7 insertions(+), 20 deletions(-) diff --git a/ql/src/codeql_ruby/ast/Expr.qll b/ql/src/codeql_ruby/ast/Expr.qll index 3440d9bd6b9..bad205bd99c 100644 --- a/ql/src/codeql_ruby/ast/Expr.qll +++ b/ql/src/codeql_ruby/ast/Expr.qll @@ -1,30 +1,15 @@ private import codeql_ruby.AST -private import codeql_ruby.CFG private import internal.Expr -private import internal.Variable -private import codeql_ruby.controlflow.internal.ControlFlowGraphImpl /** * An expression. * * This is the root QL class for all expressions. */ -class Expr extends AstNode { +class Expr extends Stmt { override Expr::Range range; Expr() { this = range } - - /** Gets a control-flow node for this expression, if any. */ - CfgNodes::AstCfgNode getAControlFlowNode() { result.getNode() = this } - - /** Gets the control-flow scope of this expression, if any. */ - CfgScope getCfgScope() { result = getCfgScope(this) } - - /** Gets the variable scope that this expression belongs to. */ - VariableScope getVariableScope() { result = enclosingScope(this) } - - /** Gets the enclosing callable, if any. */ - Callable getEnclosingCallable() { result = this.getCfgScope() } } /** diff --git a/ql/src/codeql_ruby/ast/Statement.qll b/ql/src/codeql_ruby/ast/Statement.qll index b4d7ff892db..d9c284a416d 100644 --- a/ql/src/codeql_ruby/ast/Statement.qll +++ b/ql/src/codeql_ruby/ast/Statement.qll @@ -1,6 +1,7 @@ private import codeql_ruby.AST private import codeql_ruby.CFG private import internal.Statement +private import internal.Variable private import codeql_ruby.controlflow.internal.ControlFlowGraphImpl /** @@ -17,6 +18,9 @@ class Stmt extends AstNode { /** Gets the control-flow scope of this statement, if any. */ CfgScope getCfgScope() { result = getCfgScope(this) } + /** Gets the variable scope that this statement belongs to. */ + VariableScope getVariableScope() { result = enclosingScope(this) } + /** Gets the enclosing callable, if any. */ Callable getEnclosingCallable() { result = this.getCfgScope() } } diff --git a/ql/src/codeql_ruby/ast/internal/Expr.qll b/ql/src/codeql_ruby/ast/internal/Expr.qll index 575af203d61..bed8763cf36 100644 --- a/ql/src/codeql_ruby/ast/internal/Expr.qll +++ b/ql/src/codeql_ruby/ast/internal/Expr.qll @@ -1,11 +1,9 @@ private import codeql_ruby.AST -private import codeql_ruby.ast.internal.AST -private import codeql_ruby.ast.internal.Pattern +private import codeql_ruby.ast.internal.Statement private import codeql_ruby.ast.internal.TreeSitter -private import codeql_ruby.ast.internal.Variable module Expr { - abstract class Range extends AstNode::Range { } + abstract class Range extends Stmt::Range { } } module Literal { From d42b6b651e16e50956755ccfe0f9a5a1699fc66e Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Thu, 11 Feb 2021 15:32:48 +0100 Subject: [PATCH 3/5] AST: rename ExprSequence to StmtSequence --- ql/src/codeql_ruby/ast/Control.qll | 26 ++++++------ ql/src/codeql_ruby/ast/Expr.qll | 32 +++++++------- ql/src/codeql_ruby/ast/Method.qll | 2 +- ql/src/codeql_ruby/ast/Module.qll | 6 +-- ql/src/codeql_ruby/ast/internal/Control.qll | 18 ++++---- ql/src/codeql_ruby/ast/internal/Expr.qll | 42 +++++++++---------- ql/src/codeql_ruby/ast/internal/Method.qll | 4 +- ql/src/codeql_ruby/controlflow/CfgNodes.qll | 17 ++++---- .../dataflow/internal/DataFlowPrivate.qll | 2 +- ql/test/library-tests/ast/control/CaseExpr.ql | 4 +- .../ast/control/ConditionalExpr.ql | 4 +- .../library-tests/ast/control/Loop.expected | 24 +++++------ ql/test/library-tests/ast/control/Loop.ql | 12 +++--- ql/test/library-tests/ast/modules/classes.ql | 2 +- ql/test/library-tests/ast/modules/modules.ql | 2 +- .../ast/modules/singleton_classes.ql | 2 +- 16 files changed, 98 insertions(+), 101 deletions(-) diff --git a/ql/src/codeql_ruby/ast/Control.qll b/ql/src/codeql_ruby/ast/Control.qll index 66110be14f3..6f5a7dbcded 100644 --- a/ql/src/codeql_ruby/ast/Control.qll +++ b/ql/src/codeql_ruby/ast/Control.qll @@ -57,11 +57,11 @@ class IfExpr extends ConditionalExpr { final predicate isElsif() { this instanceof @elsif } /** Gets the 'then' branch of this `if`/`elsif` expression. */ - final ExprSequence getThen() { result = range.getThen() } + final StmtSequence getThen() { result = range.getThen() } /** * Gets the `elsif`/`else` branch of this `if`/`elsif` expression, if any. In - * the following example, the result is an `ExprSequence` containing `b`. + * the following example, the result is a `StmtSequence` containing `b`. * ```rb * if foo * a @@ -77,7 +77,7 @@ class IfExpr extends ConditionalExpr { * ``` * There can be at most one result, since `elsif` branches nest. In the * following example, `ifExpr.getElse()` returns an `ElsifExpr`, and the - * `else` branch is nested inside that. To get the `ExprSequence` for the + * `else` branch is nested inside that. To get the `StmtSequence` for the * `else` branch, i.e. the one containing `c`, use * `getElse().(ElsifExpr).getElse()`. * ```rb @@ -108,7 +108,7 @@ class UnlessExpr extends ConditionalExpr, @unless { /** * Gets the 'then' branch of this `unless` expression. In the following - * example, the result is the `ExprSequence` containing `foo`. + * example, the result is the `StmtSequence` containing `foo`. * ```rb * unless a == b then * foo @@ -117,11 +117,11 @@ class UnlessExpr extends ConditionalExpr, @unless { * end * ``` */ - final ExprSequence getThen() { result = range.getThen() } + final StmtSequence getThen() { result = range.getThen() } /** * Gets the 'else' branch of this `unless` expression. In the following - * example, the result is the `ExprSequence` containing `bar`. + * example, the result is the `StmtSequence` containing `bar`. * ```rb * unless a == b then * foo @@ -130,7 +130,7 @@ class UnlessExpr extends ConditionalExpr, @unless { * end * ``` */ - final ExprSequence getElse() { result = range.getElse() } + final StmtSequence getElse() { result = range.getElse() } } /** @@ -220,8 +220,8 @@ class CaseExpr extends ControlExpr, @case__ { final Expr getValue() { result = range.getValue() } /** - * Gets the `n`th branch of this case expression, either a `WhenExpr` or an - * `ExprSequence`. + * Gets the `n`th branch of this case expression, either a `WhenExpr` or a + * `StmtSequence`. */ final Expr getBranch(int n) { result = range.getBranch(n) } @@ -235,7 +235,7 @@ class CaseExpr extends ControlExpr, @case__ { final WhenExpr getAWhenBranch() { result = getABranch() } /** Gets the `else` branch of this case expression, if any. */ - final ExprSequence getElseBranch() { result = getABranch() } + final StmtSequence getElseBranch() { result = getABranch() } /** * Gets the number of branches of this case expression. @@ -257,7 +257,7 @@ class WhenExpr extends Expr, @when { final override string getAPrimaryQlClass() { result = "WhenExpr" } /** Gets the body of this case-when expression. */ - final ExprSequence getBody() { result = range.getBody() } + final StmtSequence getBody() { result = range.getBody() } /** * Gets the `n`th pattern (or condition) in this case-when expression. In the @@ -320,7 +320,7 @@ class WhileExpr extends ConditionalLoop, @while { final override string getAPrimaryQlClass() { result = "WhileExpr" } /** Gets the body of this `while` loop. */ - final override ExprSequence getBody() { result = range.getBody() } + final override StmtSequence getBody() { result = range.getBody() } } /** @@ -338,7 +338,7 @@ class UntilExpr extends ConditionalLoop, @until { final override string getAPrimaryQlClass() { result = "UntilExpr" } /** Gets the body of this `until` loop. */ - final override ExprSequence getBody() { result = range.getBody() } + final override StmtSequence getBody() { result = range.getBody() } } /** diff --git a/ql/src/codeql_ruby/ast/Expr.qll b/ql/src/codeql_ruby/ast/Expr.qll index bad205bd99c..55ed61c0b0a 100644 --- a/ql/src/codeql_ruby/ast/Expr.qll +++ b/ql/src/codeql_ruby/ast/Expr.qll @@ -102,25 +102,25 @@ class SymbolLiteral extends Literal { } /** A sequence of expressions. */ -class ExprSequence extends Expr { - override ExprSequence::Range range; +class StmtSequence extends Expr { + override StmtSequence::Range range; - override string getAPrimaryQlClass() { result = "ExprSequence" } + override string getAPrimaryQlClass() { result = "StmtSequence" } - /** Gets the `n`th expression in this sequence. */ - final Expr getExpr(int n) { result = range.getExpr(n) } + /** Gets the `n`th statement in this sequence. */ + final Stmt getStmt(int n) { result = range.getStmt(n) } - /** Gets an expression in this sequence. */ - final Expr getAnExpr() { result = this.getExpr(_) } + /** Gets a statement in this sequence. */ + final Stmt getAStmt() { result = this.getStmt(_) } /** Gets the last expression in this sequence, if any. */ - final Expr getLastExpr() { result = this.getExpr(this.getNumberOfExpressions() - 1) } + final Expr getLastExpr() { result = this.getStmt(this.getNumberOfStatements() - 1) } - /** Gets the number of expressions in this sequence. */ - final int getNumberOfExpressions() { result = count(this.getAnExpr()) } + /** Gets the number of statements in this sequence. */ + final int getNumberOfStatements() { result = count(this.getAStmt()) } - /** Holds if this sequence has no expressions. */ - final predicate isEmpty() { this.getNumberOfExpressions() = 0 } + /** Holds if this sequence has no statements. */ + final predicate isEmpty() { this.getNumberOfStatements() = 0 } } /** @@ -128,14 +128,14 @@ class ExprSequence extends Expr { * or do-block. That is, any body that may also include rescue/ensure/else * statements. */ -class BodyStatement extends ExprSequence { +class BodyStatement extends StmtSequence { override BodyStatement::Range range; /** Gets the `else` block in this block, if any. */ - final ExprSequence getElse() { result = range.getElse() } + final StmtSequence getElse() { result = range.getElse() } /** Gets the `ensure` block in this block, if any. */ - final ExprSequence getEnsure() { result = range.getEnsure() } + final StmtSequence getEnsure() { result = range.getEnsure() } final predicate hasEnsure() { exists(this.getEnsure()) } } @@ -155,7 +155,7 @@ class BodyStatement extends ExprSequence { * () * ``` */ -class ParenthesizedExpr extends ExprSequence, @parenthesized_statements { +class ParenthesizedExpr extends StmtSequence, @parenthesized_statements { final override ParenthesizedExpr::Range range; final override string getAPrimaryQlClass() { result = "ParenthesizedExpr" } diff --git a/ql/src/codeql_ruby/ast/Method.qll b/ql/src/codeql_ruby/ast/Method.qll index 05245497fa5..28d4439f47d 100644 --- a/ql/src/codeql_ruby/ast/Method.qll +++ b/ql/src/codeql_ruby/ast/Method.qll @@ -63,7 +63,7 @@ class Lambda extends Callable, BodyStatement, @lambda { } /** A block. */ -class Block extends Callable, ExprSequence { +class Block extends Callable, StmtSequence { override Block::Range range; } diff --git a/ql/src/codeql_ruby/ast/Module.qll b/ql/src/codeql_ruby/ast/Module.qll index 8e03ee69c5b..96ee60d200a 100644 --- a/ql/src/codeql_ruby/ast/Module.qll +++ b/ql/src/codeql_ruby/ast/Module.qll @@ -9,19 +9,19 @@ class ModuleBase extends BodyStatement { override ModuleBase::Range range; /** Gets a method defined in this module/class. */ - Method getAMethod() { result = this.getAnExpr() } + Method getAMethod() { result = this.getAStmt() } /** Gets the method named `name` in this module/class, if any. */ Method getMethod(string name) { result = this.getAMethod() and result.getName() = name } /** Gets a class defined in this module/class. */ - Class getAClass() { result = this.getAnExpr() } + Class getAClass() { result = this.getAStmt() } /** Gets the class named `name` in this module/class, if any. */ Class getClass(string name) { result = this.getAClass() and result.getName() = name } /** Gets a module defined in this module/class. */ - Module getAModule() { result = this.getAnExpr() } + Module getAModule() { result = this.getAStmt() } /** Gets the module named `name` in this module/class, if any. */ Module getModule(string name) { result = this.getAModule() and result.getName() = name } diff --git a/ql/src/codeql_ruby/ast/internal/Control.qll b/ql/src/codeql_ruby/ast/internal/Control.qll index cfc98f5e3cb..518cb8e7505 100644 --- a/ql/src/codeql_ruby/ast/internal/Control.qll +++ b/ql/src/codeql_ruby/ast/internal/Control.qll @@ -17,7 +17,7 @@ module ConditionalExpr { module IfExpr { abstract class Range extends ConditionalExpr::Range { - abstract ExprSequence getThen(); + abstract StmtSequence getThen(); abstract Expr getElse(); @@ -31,7 +31,7 @@ module IfExpr { final override Expr getCondition() { result = generated.getCondition() } - final override ExprSequence getThen() { result = generated.getConsequence() } + final override StmtSequence getThen() { result = generated.getConsequence() } final override Expr getElse() { result = generated.getAlternative() } @@ -47,7 +47,7 @@ module IfExpr { final override Expr getCondition() { result = generated.getCondition() } - final override ExprSequence getThen() { result = generated.getConsequence() } + final override StmtSequence getThen() { result = generated.getConsequence() } final override Expr getElse() { result = generated.getAlternative() } @@ -65,9 +65,9 @@ module UnlessExpr { final override Expr getCondition() { result = generated.getCondition() } - final ExprSequence getThen() { result = generated.getConsequence() } + final StmtSequence getThen() { result = generated.getConsequence() } - final ExprSequence getElse() { result = generated.getAlternative() } + final StmtSequence getElse() { result = generated.getAlternative() } final override Expr getBranch(boolean cond) { cond = false and result = getThen() @@ -143,7 +143,7 @@ module WhenExpr { class Range extends Expr::Range, @when { final override Generated::When generated; - final ExprSequence getBody() { result = generated.getBody() } + final StmtSequence getBody() { result = generated.getBody() } final Expr getPattern(int n) { result = generated.getPattern(n).getChild() } @@ -167,7 +167,7 @@ module WhileExpr { class Range extends ConditionalLoop::Range, @while { final override Generated::While generated; - final override ExprSequence getBody() { result = generated.getBody() } + final override StmtSequence getBody() { result = generated.getBody() } final override Expr getCondition() { result = generated.getCondition() } @@ -179,7 +179,7 @@ module UntilExpr { class Range extends ConditionalLoop::Range, @until { final override Generated::Until generated; - final override ExprSequence getBody() { result = generated.getBody() } + final override StmtSequence getBody() { result = generated.getBody() } final override Expr getCondition() { result = generated.getCondition() } @@ -215,7 +215,7 @@ module ForExpr { class Range extends Loop::Range, @for { final override Generated::For generated; - final override ExprSequence getBody() { result = generated.getBody() } + final override StmtSequence getBody() { result = generated.getBody() } final Pattern getPattern() { result = generated.getPattern() } diff --git a/ql/src/codeql_ruby/ast/internal/Expr.qll b/ql/src/codeql_ruby/ast/internal/Expr.qll index bed8763cf36..c8ac64cf446 100644 --- a/ql/src/codeql_ruby/ast/internal/Expr.qll +++ b/ql/src/codeql_ruby/ast/internal/Expr.qll @@ -164,17 +164,17 @@ module SymbolLiteral { } } -module ExprSequence { +module StmtSequence { abstract class Range extends Expr::Range { - abstract Expr getExpr(int n); + abstract Stmt getStmt(int n); - int getNumberOfExpressions() { result = count(this.getExpr(_)) } + int getNumberOfStatements() { result = count(this.getStmt(_)) } override string toString() { - exists(int c | c = this.getNumberOfExpressions() | + exists(int c | c = this.getNumberOfStatements() | c = 0 and result = ";" or - c = 1 and result = this.getExpr(0).toString() + c = 1 and result = this.getStmt(0).toString() or c > 1 and result = "...; ..." ) @@ -183,8 +183,8 @@ module ExprSequence { } module BodyStatement { - abstract class Range extends ExprSequence::Range { - final override Expr getExpr(int n) { + abstract class Range extends StmtSequence::Range { + final override Stmt getStmt(int n) { result = rank[n + 1](Generated::AstNode node, int i | node = getChild(i) and @@ -196,59 +196,59 @@ module BodyStatement { ) } - final ExprSequence getElse() { result = unique(Generated::Else s | s = getChild(_)) } + final StmtSequence getElse() { result = unique(Generated::Else s | s = getChild(_)) } - final ExprSequence getEnsure() { result = unique(Generated::Ensure s | s = getChild(_)) } + final StmtSequence getEnsure() { result = unique(Generated::Ensure s | s = getChild(_)) } abstract Generated::AstNode getChild(int i); } } module ParenthesizedExpr { - class Range extends ExprSequence::Range, @parenthesized_statements { + class Range extends StmtSequence::Range, @parenthesized_statements { final override Generated::ParenthesizedStatements generated; - final override Expr getExpr(int n) { result = generated.getChild(n) } + final override Stmt getStmt(int n) { result = generated.getChild(n) } final override string toString() { - exists(int c | c = this.getNumberOfExpressions() | + exists(int c | c = this.getNumberOfStatements() | c = 0 and result = "()" or - c > 0 and result = "(" + ExprSequence::Range.super.toString() + ")" + c > 0 and result = "(" + StmtSequence::Range.super.toString() + ")" ) } } } module ThenExpr { - class Range extends ExprSequence::Range, @then { + class Range extends StmtSequence::Range, @then { final override Generated::Then generated; - final override Expr getExpr(int n) { result = generated.getChild(n) } + final override Stmt getStmt(int n) { result = generated.getChild(n) } } } module ElseExpr { - class Range extends ExprSequence::Range, @else { + class Range extends StmtSequence::Range, @else { final override Generated::Else generated; - final override Expr getExpr(int n) { result = generated.getChild(n) } + final override Stmt getStmt(int n) { result = generated.getChild(n) } } } module DoExpr { - class Range extends ExprSequence::Range, @do { + class Range extends StmtSequence::Range, @do { final override Generated::Do generated; - final override Expr getExpr(int n) { result = generated.getChild(n) } + final override Stmt getStmt(int n) { result = generated.getChild(n) } } } module Ensure { - class Range extends ExprSequence::Range, @ensure { + class Range extends StmtSequence::Range, @ensure { final override Generated::Ensure generated; - final override Expr getExpr(int n) { result = generated.getChild(n) } + final override Stmt getStmt(int n) { result = generated.getChild(n) } final override string toString() { result = "ensure ... end" } } diff --git a/ql/src/codeql_ruby/ast/internal/Method.qll b/ql/src/codeql_ruby/ast/internal/Method.qll index 331cf90f987..ae3462af950 100644 --- a/ql/src/codeql_ruby/ast/internal/Method.qll +++ b/ql/src/codeql_ruby/ast/internal/Method.qll @@ -65,7 +65,7 @@ module Lambda { } module Block { - abstract class Range extends Callable::Range, ExprSequence::Range { + abstract class Range extends Callable::Range, StmtSequence::Range { Range() { not generated.getParent() instanceof Generated::Lambda } } } @@ -92,7 +92,7 @@ module BraceBlock { result = generated.getParameters().getChild(n) } - final override Expr getExpr(int i) { result = generated.getChild(i) } + final override Stmt getStmt(int i) { result = generated.getChild(i) } final override string toString() { result = "{ ... }" } } diff --git a/ql/src/codeql_ruby/controlflow/CfgNodes.qll b/ql/src/codeql_ruby/controlflow/CfgNodes.qll index 4ab22541942..42466e3df20 100644 --- a/ql/src/codeql_ruby/controlflow/CfgNodes.qll +++ b/ql/src/codeql_ruby/controlflow/CfgNodes.qll @@ -292,21 +292,18 @@ module ExprNodes { final ExprCfgNode getBranch(boolean cond) { e.hasCfgChild(e.getBranch(cond), this, result) } } - private class ExprSequenceChildMapping extends ExprChildMapping, ExprSequence { - override predicate relevantChild(Expr e) { e = this.getAnExpr() } + private class StmtSequenceChildMapping extends ExprChildMapping, StmtSequence { + override predicate relevantChild(Expr e) { e = this.getLastExpr() } } - /** A control-flow node that wraps an `ExprSequence` AST expression. */ - class ExprSequenceCfgNode extends ExprCfgNode { - override ExprSequenceChildMapping e; + /** A control-flow node that wraps a `StmtSequence` AST expression. */ + class StmtSequenceCfgNode extends ExprCfgNode { + override StmtSequenceChildMapping e; - final override ExprSequence getExpr() { result = ExprCfgNode.super.getExpr() } + final override StmtSequence getExpr() { result = ExprCfgNode.super.getExpr() } /** Gets the last expression in this sequence, if any. */ final ExprCfgNode getLastExpr() { e.hasCfgChild(e.getLastExpr(), this, result) } - - /** Gets the 'n'th expression of this expression sequence. */ - final ExprCfgNode getExpr(int n) { e.hasCfgChild(e.getExpr(n), this, result) } } private class ForExprChildMapping extends ExprChildMapping, ForExpr { @@ -324,7 +321,7 @@ module ExprNodes { } /** A control-flow node that wraps a `ParenthesizedExpr` AST expression. */ - class ParenthesizedExprCfgNode extends ExprSequenceCfgNode { + class ParenthesizedExprCfgNode extends StmtSequenceCfgNode { ParenthesizedExprCfgNode() { this.getExpr() instanceof ParenthesizedExpr } } diff --git a/ql/src/codeql_ruby/dataflow/internal/DataFlowPrivate.qll b/ql/src/codeql_ruby/dataflow/internal/DataFlowPrivate.qll index 38669d32736..222fa8ac7bb 100644 --- a/ql/src/codeql_ruby/dataflow/internal/DataFlowPrivate.qll +++ b/ql/src/codeql_ruby/dataflow/internal/DataFlowPrivate.qll @@ -126,7 +126,7 @@ private module Cached { or nodeFrom.asExpr() = nodeTo.asExpr().(CfgNodes::ExprNodes::AssignExprCfgNode).getRhs() or - nodeFrom.asExpr() = nodeTo.asExpr().(CfgNodes::ExprNodes::ExprSequenceCfgNode).getLastExpr() + nodeFrom.asExpr() = nodeTo.asExpr().(CfgNodes::ExprNodes::StmtSequenceCfgNode).getLastExpr() or nodeFrom.asExpr() = nodeTo.asExpr().(CfgNodes::ExprNodes::ConditionalExprCfgNode).getBranch(_) or diff --git a/ql/test/library-tests/ast/control/CaseExpr.ql b/ql/test/library-tests/ast/control/CaseExpr.ql index 297b6b3577c..eaef9359f55 100644 --- a/ql/test/library-tests/ast/control/CaseExpr.ql +++ b/ql/test/library-tests/ast/control/CaseExpr.ql @@ -4,13 +4,13 @@ query predicate caseValues(CaseExpr c, Expr value) { value = c.getValue() } query predicate caseNoValues(CaseExpr c) { not exists(c.getValue()) } -query predicate caseElseBranches(CaseExpr c, ExprSequence elseBranch) { +query predicate caseElseBranches(CaseExpr c, StmtSequence elseBranch) { elseBranch = c.getElseBranch() } query predicate caseNoElseBranches(CaseExpr c) { not exists(c.getElseBranch()) } -query predicate caseWhenBranches(CaseExpr c, WhenExpr when, int pIndex, Expr p, ExprSequence body) { +query predicate caseWhenBranches(CaseExpr c, WhenExpr when, int pIndex, Expr p, StmtSequence body) { when = c.getAWhenBranch() and p = when.getPattern(pIndex) and body = when.getBody() diff --git a/ql/test/library-tests/ast/control/ConditionalExpr.ql b/ql/test/library-tests/ast/control/ConditionalExpr.ql index 48c1e2630c9..6e209b2d3fc 100644 --- a/ql/test/library-tests/ast/control/ConditionalExpr.ql +++ b/ql/test/library-tests/ast/control/ConditionalExpr.ql @@ -9,7 +9,7 @@ query predicate conditionalExprs( } query predicate ifExprs( - IfExpr e, string pClass, Expr cond, ExprSequence thenExpr, string elseStr, boolean isElsif + IfExpr e, string pClass, Expr cond, StmtSequence thenExpr, string elseStr, boolean isElsif ) { pClass = e.getAPrimaryQlClass() and cond = e.getCondition() and @@ -19,7 +19,7 @@ query predicate ifExprs( } query predicate unlessExprs( - UnlessExpr e, string pClass, Expr cond, ExprSequence thenExpr, string elseStr + UnlessExpr e, string pClass, Expr cond, StmtSequence thenExpr, string elseStr ) { pClass = e.getAPrimaryQlClass() and cond = e.getCondition() and diff --git a/ql/test/library-tests/ast/control/Loop.expected b/ql/test/library-tests/ast/control/Loop.expected index 0da638e8b21..0c32647e0be 100644 --- a/ql/test/library-tests/ast/control/Loop.expected +++ b/ql/test/library-tests/ast/control/Loop.expected @@ -1,20 +1,20 @@ loops -| loops.rb:9:1:12:3 | for ... in ... | ForExpr | loops.rb:9:15:12:3 | ...; ... | ExprSequence | -| loops.rb:16:1:19:3 | for ... in ... | ForExpr | loops.rb:16:15:19:3 | ...; ... | ExprSequence | -| loops.rb:22:1:25:3 | for ... in ... | ForExpr | loops.rb:22:35:25:3 | ...; ... | ExprSequence | -| loops.rb:28:1:32:3 | for ... in ... | ForExpr | loops.rb:28:37:32:3 | ...; ... | ExprSequence | -| loops.rb:35:1:39:3 | while ... | WhileExpr | loops.rb:35:12:39:3 | ...; ... | ExprSequence | -| loops.rb:42:1:45:3 | while ... | WhileExpr | loops.rb:42:13:45:3 | ...; ... | ExprSequence | +| loops.rb:9:1:12:3 | for ... in ... | ForExpr | loops.rb:9:15:12:3 | ...; ... | StmtSequence | +| loops.rb:16:1:19:3 | for ... in ... | ForExpr | loops.rb:16:15:19:3 | ...; ... | StmtSequence | +| loops.rb:22:1:25:3 | for ... in ... | ForExpr | loops.rb:22:35:25:3 | ...; ... | StmtSequence | +| loops.rb:28:1:32:3 | for ... in ... | ForExpr | loops.rb:28:37:32:3 | ...; ... | StmtSequence | +| loops.rb:35:1:39:3 | while ... | WhileExpr | loops.rb:35:12:39:3 | ...; ... | StmtSequence | +| loops.rb:42:1:45:3 | while ... | WhileExpr | loops.rb:42:13:45:3 | ...; ... | StmtSequence | | loops.rb:48:1:48:19 | ... while ... | WhileModifierExpr | loops.rb:48:1:48:6 | ... += ... | AssignAddExpr | -| loops.rb:51:1:54:3 | until ... | UntilExpr | loops.rb:51:13:54:3 | ...; ... | ExprSequence | -| loops.rb:57:1:60:3 | until ... | UntilExpr | loops.rb:57:13:60:3 | ...; ... | ExprSequence | +| loops.rb:51:1:54:3 | until ... | UntilExpr | loops.rb:51:13:54:3 | ...; ... | StmtSequence | +| loops.rb:57:1:60:3 | until ... | UntilExpr | loops.rb:57:13:60:3 | ...; ... | StmtSequence | | loops.rb:63:1:63:19 | ... until ... | UntilModifierExpr | loops.rb:63:1:63:6 | ... -= ... | AssignSubExpr | conditionalLoops -| loops.rb:35:1:39:3 | while ... | WhileExpr | loops.rb:35:7:35:11 | ... < ... | loops.rb:35:12:39:3 | ...; ... | ExprSequence | -| loops.rb:42:1:45:3 | while ... | WhileExpr | loops.rb:42:7:42:11 | ... < ... | loops.rb:42:13:45:3 | ...; ... | ExprSequence | +| loops.rb:35:1:39:3 | while ... | WhileExpr | loops.rb:35:7:35:11 | ... < ... | loops.rb:35:12:39:3 | ...; ... | StmtSequence | +| loops.rb:42:1:45:3 | while ... | WhileExpr | loops.rb:42:7:42:11 | ... < ... | loops.rb:42:13:45:3 | ...; ... | StmtSequence | | loops.rb:48:1:48:19 | ... while ... | WhileModifierExpr | loops.rb:48:14:48:19 | ... >= ... | loops.rb:48:1:48:6 | ... += ... | AssignAddExpr | -| loops.rb:51:1:54:3 | until ... | UntilExpr | loops.rb:51:7:51:12 | ... == ... | loops.rb:51:13:54:3 | ...; ... | ExprSequence | -| loops.rb:57:1:60:3 | until ... | UntilExpr | loops.rb:57:7:57:11 | ... > ... | loops.rb:57:13:60:3 | ...; ... | ExprSequence | +| loops.rb:51:1:54:3 | until ... | UntilExpr | loops.rb:51:7:51:12 | ... == ... | loops.rb:51:13:54:3 | ...; ... | StmtSequence | +| loops.rb:57:1:60:3 | until ... | UntilExpr | loops.rb:57:7:57:11 | ... > ... | loops.rb:57:13:60:3 | ...; ... | StmtSequence | | loops.rb:63:1:63:19 | ... until ... | UntilModifierExpr | loops.rb:63:14:63:19 | ... == ... | loops.rb:63:1:63:6 | ... -= ... | AssignSubExpr | forExprs | loops.rb:9:1:12:3 | for ... in ... | loops.rb:9:5:9:5 | n | loops.rb:9:15:12:3 | ...; ... | 0 | loops.rb:10:5:10:12 | ... += ... | diff --git a/ql/test/library-tests/ast/control/Loop.ql b/ql/test/library-tests/ast/control/Loop.ql index e8b2b30b107..6bae94fe352 100644 --- a/ql/test/library-tests/ast/control/Loop.ql +++ b/ql/test/library-tests/ast/control/Loop.ql @@ -13,10 +13,10 @@ query predicate conditionalLoops( cond = l.getCondition() } -query predicate forExprs(ForExpr f, Pattern p, ExprSequence body, int i, Expr bodyChild) { +query predicate forExprs(ForExpr f, Pattern p, StmtSequence body, int i, Expr bodyChild) { p = f.getPattern() and body = f.getBody() and - bodyChild = body.getExpr(i) + bodyChild = body.getStmt(i) } query predicate forExprsTuplePatterns(ForExpr f, TuplePattern tp, int i, Pattern cp) { @@ -24,10 +24,10 @@ query predicate forExprsTuplePatterns(ForExpr f, TuplePattern tp, int i, Pattern cp = tp.getElement(i) } -query predicate whileExprs(WhileExpr e, Expr cond, ExprSequence body, int i, Expr bodyChild) { +query predicate whileExprs(WhileExpr e, Expr cond, StmtSequence body, int i, Expr bodyChild) { cond = e.getCondition() and body = e.getBody() and - bodyChild = body.getExpr(i) + bodyChild = body.getStmt(i) } query predicate whileModifierExprs(WhileModifierExpr e, Expr cond, Expr body) { @@ -35,10 +35,10 @@ query predicate whileModifierExprs(WhileModifierExpr e, Expr cond, Expr body) { body = e.getBody() } -query predicate untilExprs(UntilExpr e, Expr cond, ExprSequence body, int i, Expr bodyChild) { +query predicate untilExprs(UntilExpr e, Expr cond, StmtSequence body, int i, Expr bodyChild) { cond = e.getCondition() and body = e.getBody() and - bodyChild = body.getExpr(i) + bodyChild = body.getStmt(i) } query predicate untilModifierExprs(UntilModifierExpr e, Expr cond, Expr body) { diff --git a/ql/test/library-tests/ast/modules/classes.ql b/ql/test/library-tests/ast/modules/classes.ql index 7293e193768..52d6ed21e51 100644 --- a/ql/test/library-tests/ast/modules/classes.ql +++ b/ql/test/library-tests/ast/modules/classes.ql @@ -9,7 +9,7 @@ query predicate classesWithNameScopeExprs(Class c, Expr se) { se = c.getScopeExp query predicate classesWithGlobalNameScopeExprs(Class c) { c.hasGlobalScope() } query predicate exprsInClasses(Class c, int i, Expr e, string eClass) { - e = c.getExpr(i) and eClass = e.getAPrimaryQlClass() + e = c.getStmt(i) and eClass = e.getAPrimaryQlClass() } query predicate methodsInClasses(Class c, Method m, string name) { m = c.getMethod(name) } diff --git a/ql/test/library-tests/ast/modules/modules.ql b/ql/test/library-tests/ast/modules/modules.ql index 8f7d7a952c5..cce336c8b5c 100644 --- a/ql/test/library-tests/ast/modules/modules.ql +++ b/ql/test/library-tests/ast/modules/modules.ql @@ -9,7 +9,7 @@ query predicate modulesWithScopeExprs(Module m, Expr se) { se = m.getScopeExpr() query predicate modulesWithGlobalNameScopeExprs(Module m) { m.hasGlobalScope() } query predicate exprsInModules(Module m, int i, Expr e, string eClass) { - e = m.getExpr(i) and eClass = e.getAPrimaryQlClass() + e = m.getStmt(i) and eClass = e.getAPrimaryQlClass() } query predicate methodsInModules(Module mod, Method method, string name) { diff --git a/ql/test/library-tests/ast/modules/singleton_classes.ql b/ql/test/library-tests/ast/modules/singleton_classes.ql index 83e0da59ce2..d02dd8cb68c 100644 --- a/ql/test/library-tests/ast/modules/singleton_classes.ql +++ b/ql/test/library-tests/ast/modules/singleton_classes.ql @@ -5,7 +5,7 @@ query predicate singletonClasses(SingletonClass sc, string pClass, Expr value) { } query predicate exprsInSingletonClasses(SingletonClass sc, int i, Expr e, string eClass) { - e = sc.getExpr(i) and eClass = e.getAPrimaryQlClass() + e = sc.getStmt(i) and eClass = e.getAPrimaryQlClass() } query predicate methodsInSingletonClasses(SingletonClass sc, Method m) { m = sc.getAMethod() } From c4e2c87d8238c403f503855392668ffc4a4724f7 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Thu, 11 Feb 2021 16:37:03 +0100 Subject: [PATCH 4/5] AST: some statement tests --- ql/test/library-tests/ast/control/Loop.expected | 2 ++ ql/test/library-tests/ast/control/Loop.ql | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ql/test/library-tests/ast/control/Loop.expected b/ql/test/library-tests/ast/control/Loop.expected index 0c32647e0be..f93e73f5ee8 100644 --- a/ql/test/library-tests/ast/control/Loop.expected +++ b/ql/test/library-tests/ast/control/Loop.expected @@ -25,6 +25,7 @@ forExprs | loops.rb:22:1:25:3 | for ... in ... | loops.rb:22:5:22:14 | (..., ...) | loops.rb:22:35:25:3 | ...; ... | 1 | loops.rb:24:3:24:14 | ... *= ... | | loops.rb:28:1:32:3 | for ... in ... | loops.rb:28:5:28:16 | (..., ...) | loops.rb:28:37:32:3 | ...; ... | 0 | loops.rb:29:3:29:14 | ... += ... | | loops.rb:28:1:32:3 | for ... in ... | loops.rb:28:5:28:16 | (..., ...) | loops.rb:28:37:32:3 | ...; ... | 1 | loops.rb:30:3:30:14 | ... /= ... | +| loops.rb:28:1:32:3 | for ... in ... | loops.rb:28:5:28:16 | (..., ...) | loops.rb:28:37:32:3 | ...; ... | 2 | loops.rb:31:3:31:7 | break | forExprsTuplePatterns | loops.rb:22:1:25:3 | for ... in ... | loops.rb:22:5:22:14 | (..., ...) | 0 | loops.rb:22:5:22:7 | key | | loops.rb:22:1:25:3 | for ... in ... | loops.rb:22:5:22:14 | (..., ...) | 1 | loops.rb:22:10:22:14 | value | @@ -32,6 +33,7 @@ forExprsTuplePatterns whileExprs | loops.rb:35:1:39:3 | while ... | loops.rb:35:7:35:11 | ... < ... | loops.rb:35:12:39:3 | ...; ... | 0 | loops.rb:36:3:36:8 | ... += ... | | loops.rb:35:1:39:3 | while ... | loops.rb:35:7:35:11 | ... < ... | loops.rb:35:12:39:3 | ...; ... | 1 | loops.rb:37:3:37:8 | ... += ... | +| loops.rb:35:1:39:3 | while ... | loops.rb:35:7:35:11 | ... < ... | loops.rb:35:12:39:3 | ...; ... | 2 | loops.rb:38:3:38:6 | next | | loops.rb:42:1:45:3 | while ... | loops.rb:42:7:42:11 | ... < ... | loops.rb:42:13:45:3 | ...; ... | 0 | loops.rb:43:3:43:8 | ... += ... | | loops.rb:42:1:45:3 | while ... | loops.rb:42:7:42:11 | ... < ... | loops.rb:42:13:45:3 | ...; ... | 1 | loops.rb:44:3:44:8 | ... += ... | whileModifierExprs diff --git a/ql/test/library-tests/ast/control/Loop.ql b/ql/test/library-tests/ast/control/Loop.ql index 6bae94fe352..d5275b46e33 100644 --- a/ql/test/library-tests/ast/control/Loop.ql +++ b/ql/test/library-tests/ast/control/Loop.ql @@ -13,7 +13,7 @@ query predicate conditionalLoops( cond = l.getCondition() } -query predicate forExprs(ForExpr f, Pattern p, StmtSequence body, int i, Expr bodyChild) { +query predicate forExprs(ForExpr f, Pattern p, StmtSequence body, int i, Stmt bodyChild) { p = f.getPattern() and body = f.getBody() and bodyChild = body.getStmt(i) @@ -24,7 +24,7 @@ query predicate forExprsTuplePatterns(ForExpr f, TuplePattern tp, int i, Pattern cp = tp.getElement(i) } -query predicate whileExprs(WhileExpr e, Expr cond, StmtSequence body, int i, Expr bodyChild) { +query predicate whileExprs(WhileExpr e, Expr cond, StmtSequence body, int i, Stmt bodyChild) { cond = e.getCondition() and body = e.getBody() and bodyChild = body.getStmt(i) @@ -35,7 +35,7 @@ query predicate whileModifierExprs(WhileModifierExpr e, Expr cond, Expr body) { body = e.getBody() } -query predicate untilExprs(UntilExpr e, Expr cond, StmtSequence body, int i, Expr bodyChild) { +query predicate untilExprs(UntilExpr e, Expr cond, StmtSequence body, int i, Stmt bodyChild) { cond = e.getCondition() and body = e.getBody() and bodyChild = body.getStmt(i) From f9e9dc230463bbdde3b38797cccf01823d07b63e Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Thu, 11 Feb 2021 17:53:28 +0100 Subject: [PATCH 5/5] Address comment Co-authored-by: Nick Rolfe --- ql/src/codeql_ruby/ast/Expr.qll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ql/src/codeql_ruby/ast/Expr.qll b/ql/src/codeql_ruby/ast/Expr.qll index 55ed61c0b0a..22956ca6dbf 100644 --- a/ql/src/codeql_ruby/ast/Expr.qll +++ b/ql/src/codeql_ruby/ast/Expr.qll @@ -124,7 +124,7 @@ class StmtSequence extends Expr { } /** - * A sequence of expressions representing the body of a method, class, module, + * A sequence of statements representing the body of a method, class, module, * or do-block. That is, any body that may also include rescue/ensure/else * statements. */