Replace ConditionalExpr::get{Then,Else} with getBranch(boolean cond).

This commit is contained in:
Nick Rolfe
2021-01-07 17:32:41 +00:00
parent f4abe7f4a1
commit 36c7d3fe5b
4 changed files with 131 additions and 75 deletions

View File

@@ -31,11 +31,11 @@ class ConditionalExpr extends Expr {
*/
final Expr getCondition() { result = range.getCondition() }
/** Gets the 'then' branch of this conditional expression. */
Expr getThen() { result = range.getThen() }
/** Gets the 'else' branch of this conditional expression, if any. */
Expr getElse() { result = range.getElse() }
/**
* Gets the branch of this conditional expression that is taken when the
* condition evaluates to `cond`, if any.
*/
Expr getBranch(boolean cond) { result = range.getBranch(cond) }
}
/**
@@ -46,7 +46,7 @@ class IfOrElsifExpr extends ConditionalExpr {
override IfOrElsifExpr::Range range;
/** Gets the 'then' branch of this `if`/`elsif` expression. */
final override ExprSequence getThen() { result = range.getThen() }
final ExprSequence getThen() { result = range.getThen() }
/**
* Gets the `elsif`/`else` branch of this `if`/`elsif` expression, if any. In
@@ -79,7 +79,7 @@ class IfOrElsifExpr extends ConditionalExpr {
* end
* ```
*/
final override Expr getElse() { result = range.getElse() }
final Expr getElse() { result = range.getElse() }
}
/**
@@ -108,7 +108,7 @@ class IfExpr extends IfOrElsifExpr, @if {
* end
* ```
*/
class ElsifExpr extends ConditionalExpr {
class ElsifExpr extends IfOrElsifExpr, @elsif {
final override ElsifExpr::Range range;
final override string getAPrimaryQlClass() { result = "ElsifExpr" }
@@ -130,11 +130,36 @@ class UnlessExpr extends ConditionalExpr, @unless {
final override string getAPrimaryQlClass() { result = "UnlessExpr" }
final override string toString() { result = "unless ..." }
/**
* Gets the 'then' branch of this `unless` expression. In the following
* example, the result is the `ExprSequence` containing `foo`.
* ```rb
* unless a == b then
* foo
* else
* bar
* end
* ```
*/
final ExprSequence getThen() { result = range.getThen() }
/**
* Gets the 'else' branch of this `unless` expression. In the following
* example, the result is the `ExprSequence` containing `bar`.
* ```rb
* unless a == b then
* foo
* else
* bar
* end
* ```
*/
final ExprSequence getElse() { result = range.getElse() }
}
/**
* An expression modified using `if`. In the following example, `getCondition`
* returns the `Expr` for `bar`, and `getThen` returns the `Expr` for `foo`.
* An expression modified using `if`.
* ```rb
* foo if bar
* ```
@@ -147,19 +172,20 @@ class IfModifierExpr extends ConditionalExpr, @if_modifier {
final override string toString() { result = "... if ..." }
/**
* Does not hold, since `if`-modified expressions cannot have `else`
* branches.
* Gets the expression that is conditionally evaluated. In the following
* example, the result is the `Expr` for `foo`.
* ```rb
* foo if bar
* ```
*/
final override Expr getElse() { none() }
final Expr getExpr() { result = range.getExpr() }
}
/**
* An expression modified using `unless`. For example, in:
* An expression modified using `unless`.
* ```rb
* y /= x unless x == 0
* ```
* `getCondition` returns the `x == 0` expression, and `getThen` returns the
* `y /= x` expression.
*/
class UnlessModifierExpr extends ConditionalExpr, @unless_modifier {
final override UnlessModifierExpr::Range range;
@@ -169,10 +195,13 @@ class UnlessModifierExpr extends ConditionalExpr, @unless_modifier {
final override string toString() { result = "... unless ..." }
/**
* Does not hold, since `unless`-modified expressions cannot have `else`
* branches.
* Gets the expression that is conditionally evaluated. In the following
* example, the result is the `Expr` for `foo`.
* ```rb
* foo unless bar
* ```
*/
final override Expr getElse() { none() }
final Expr getExpr() { result = range.getExpr() }
}
/**
@@ -187,6 +216,12 @@ class TernaryIfExpr extends ConditionalExpr, @conditional {
final override string getAPrimaryQlClass() { result = "TernaryIfExpr" }
final override string toString() { result = "... ? ... : ..." }
/** Gets the 'then' branch of this ternary if expression. */
final Expr getThen() { result = range.getThen() }
/** Gets the 'else' branch of this ternary if expression. */
final Expr getElse() { result = range.getElse() }
}
class CaseExpr extends ControlExpr, @case__ {

View File

@@ -11,14 +11,16 @@ module ConditionalExpr {
abstract class Range extends ControlExpr::Range {
abstract Expr getCondition();
abstract Expr getThen();
abstract Expr getElse();
abstract Expr getBranch(boolean cond);
}
}
module IfOrElsifExpr {
abstract class Range extends ConditionalExpr::Range { }
abstract class Range extends ConditionalExpr::Range {
abstract Expr getThen();
abstract Expr getElse();
}
}
module IfExpr {
@@ -30,6 +32,12 @@ module IfExpr {
final override ExprSequence getThen() { result = generated.getConsequence() }
final override Expr getElse() { result = generated.getAlternative() }
final override Expr getBranch(boolean cond) {
cond = true and result = getThen()
or
cond = false and result = getElse()
}
}
}
@@ -42,6 +50,12 @@ module ElsifExpr {
final override ExprSequence getThen() { result = generated.getConsequence() }
final override Expr getElse() { result = generated.getAlternative() }
final override Expr getBranch(boolean cond) {
cond = true and result = getThen()
or
cond = false and result = getElse()
}
}
}
@@ -51,9 +65,15 @@ module UnlessExpr {
final override Expr getCondition() { result = generated.getCondition() }
final override ExprSequence getThen() { result = generated.getConsequence() }
final ExprSequence getThen() { result = generated.getConsequence() }
final override ExprSequence getElse() { result = generated.getAlternative() }
final ExprSequence getElse() { result = generated.getAlternative() }
final override Expr getBranch(boolean cond) {
cond = false and result = getThen()
or
cond = true and result = getElse()
}
}
}
@@ -63,9 +83,9 @@ module IfModifierExpr {
final override Expr getCondition() { result = generated.getCondition() }
final override Expr getThen() { result = generated.getBody() }
final Expr getExpr() { result = generated.getBody() }
final override Expr getElse() { none() }
final override Expr getBranch(boolean cond) { cond = true and result = getExpr() }
}
}
@@ -75,9 +95,9 @@ module UnlessModifierExpr {
final override Expr getCondition() { result = generated.getCondition() }
final override Expr getThen() { result = generated.getBody() }
final Expr getExpr() { result = generated.getBody() }
final override Expr getElse() { none() }
final override Expr getBranch(boolean cond) { cond = false and result = getExpr() }
}
}
@@ -87,9 +107,15 @@ module TernaryIfExpr {
final override Expr getCondition() { result = generated.getCondition() }
final override Expr getThen() { result = generated.getConsequence() }
final Expr getThen() { result = generated.getConsequence() }
final override Expr getElse() { result = generated.getAlternative() }
final Expr getElse() { result = generated.getAlternative() }
final override Expr getBranch(boolean cond) {
cond = true and result = getThen()
or
cond = false and result = getElse()
}
}
}

View File

@@ -1,17 +1,23 @@
conditionalExprsWithElse
| conditionals.rb:15:1:19:3 | if ... | conditionals.rb:15:4:15:9 | ... == ... | conditionals.rb:15:10:16:5 | ...; ... | conditionals.rb:17:1:18:5 | ...; ... | IfExpr |
| conditionals.rb:22:1:30:3 | if ... | conditionals.rb:22:4:22:9 | ... == ... | conditionals.rb:22:11:23:5 | ...; ... | conditionals.rb:24:1:29:5 | elsif ... | IfExpr |
| conditionals.rb:24:1:29:5 | elsif ... | conditionals.rb:24:7:24:12 | ... == ... | conditionals.rb:24:14:25:5 | ...; ... | conditionals.rb:26:1:29:5 | elsif ... | ElsifExpr |
| conditionals.rb:26:1:29:5 | elsif ... | conditionals.rb:26:7:26:12 | ... == ... | conditionals.rb:26:14:27:5 | ...; ... | conditionals.rb:28:1:29:5 | ...; ... | ElsifExpr |
| conditionals.rb:33:1:37:3 | if ... | conditionals.rb:33:4:33:9 | ... == ... | conditionals.rb:33:10:34:5 | ...; ... | conditionals.rb:35:1:36:5 | elsif ... | IfExpr |
| conditionals.rb:45:1:49:3 | unless ... | conditionals.rb:45:8:45:13 | ... == ... | conditionals.rb:45:14:46:5 | ...; ... | conditionals.rb:47:1:48:5 | ...; ... | UnlessExpr |
| conditionals.rb:58:5:58:25 | ... ? ... : ... | conditionals.rb:58:5:58:9 | ... > ... | conditionals.rb:58:13:58:17 | ... + ... | conditionals.rb:58:21:58:25 | ... - ... | TernaryIfExpr |
conditionalExprsWithoutElse
| conditionals.rb:10:1:12:3 | if ... | conditionals.rb:10:4:10:8 | ... > ... | conditionals.rb:10:10:11:5 | ...; ... | IfExpr |
| conditionals.rb:35:1:36:5 | elsif ... | conditionals.rb:35:7:35:12 | ... == ... | conditionals.rb:35:13:36:5 | ...; ... | ElsifExpr |
| conditionals.rb:40:1:42:3 | unless ... | conditionals.rb:40:8:40:12 | ... > ... | conditionals.rb:40:14:41:5 | ...; ... | UnlessExpr |
| conditionals.rb:52:1:52:14 | ... if ... | conditionals.rb:52:10:52:14 | ... > ... | conditionals.rb:52:1:52:5 | ... = ... | IfModifierExpr |
| conditionals.rb:55:1:55:18 | ... unless ... | conditionals.rb:55:14:55:18 | ... < ... | conditionals.rb:55:1:55:5 | ... = ... | UnlessModifierExpr |
conditionalExprs
| conditionals.rb:10:1:12:3 | if ... | IfExpr | conditionals.rb:10:4:10:8 | ... > ... | conditionals.rb:10:10:11:5 | ...; ... | true |
| conditionals.rb:15:1:19:3 | if ... | IfExpr | conditionals.rb:15:4:15:9 | ... == ... | conditionals.rb:15:10:16:5 | ...; ... | true |
| conditionals.rb:15:1:19:3 | if ... | IfExpr | conditionals.rb:15:4:15:9 | ... == ... | conditionals.rb:17:1:18:5 | ...; ... | false |
| conditionals.rb:22:1:30:3 | if ... | IfExpr | conditionals.rb:22:4:22:9 | ... == ... | conditionals.rb:22:11:23:5 | ...; ... | true |
| conditionals.rb:22:1:30:3 | if ... | IfExpr | conditionals.rb:22:4:22:9 | ... == ... | conditionals.rb:24:1:29:5 | elsif ... | false |
| conditionals.rb:24:1:29:5 | elsif ... | ElsifExpr | conditionals.rb:24:7:24:12 | ... == ... | conditionals.rb:24:14:25:5 | ...; ... | true |
| conditionals.rb:24:1:29:5 | elsif ... | ElsifExpr | conditionals.rb:24:7:24:12 | ... == ... | conditionals.rb:26:1:29:5 | elsif ... | false |
| conditionals.rb:26:1:29:5 | elsif ... | ElsifExpr | conditionals.rb:26:7:26:12 | ... == ... | conditionals.rb:26:14:27:5 | ...; ... | true |
| conditionals.rb:26:1:29:5 | elsif ... | ElsifExpr | conditionals.rb:26:7:26:12 | ... == ... | conditionals.rb:28:1:29:5 | ...; ... | false |
| conditionals.rb:33:1:37:3 | if ... | IfExpr | conditionals.rb:33:4:33:9 | ... == ... | conditionals.rb:33:10:34:5 | ...; ... | true |
| conditionals.rb:33:1:37:3 | if ... | IfExpr | conditionals.rb:33:4:33:9 | ... == ... | conditionals.rb:35:1:36:5 | elsif ... | false |
| conditionals.rb:35:1:36:5 | elsif ... | ElsifExpr | conditionals.rb:35:7:35:12 | ... == ... | conditionals.rb:35:13:36:5 | ...; ... | true |
| conditionals.rb:40:1:42:3 | unless ... | UnlessExpr | conditionals.rb:40:8:40:12 | ... > ... | conditionals.rb:40:14:41:5 | ...; ... | false |
| conditionals.rb:45:1:49:3 | unless ... | UnlessExpr | conditionals.rb:45:8:45:13 | ... == ... | conditionals.rb:45:14:46:5 | ...; ... | false |
| conditionals.rb:45:1:49:3 | unless ... | UnlessExpr | conditionals.rb:45:8:45:13 | ... == ... | conditionals.rb:47:1:48:5 | ...; ... | true |
| conditionals.rb:52:1:52:14 | ... if ... | IfModifierExpr | conditionals.rb:52:10:52:14 | ... > ... | conditionals.rb:52:1:52:5 | ... = ... | true |
| conditionals.rb:55:1:55:18 | ... unless ... | UnlessModifierExpr | conditionals.rb:55:14:55:18 | ... < ... | conditionals.rb:55:1:55:5 | ... = ... | false |
| conditionals.rb:58:5:58:25 | ... ? ... : ... | TernaryIfExpr | conditionals.rb:58:5:58:9 | ... > ... | conditionals.rb:58:13:58:17 | ... + ... | true |
| conditionals.rb:58:5:58:25 | ... ? ... : ... | TernaryIfExpr | conditionals.rb:58:5:58:9 | ... > ... | conditionals.rb:58:21:58:25 | ... - ... | false |
ifOrElsifExprs
| conditionals.rb:10:1:12:3 | if ... | IfExpr | conditionals.rb:10:4:10:8 | ... > ... | conditionals.rb:10:10:11:5 | ...; ... | (none) |
| conditionals.rb:15:1:19:3 | if ... | IfExpr | conditionals.rb:15:4:15:9 | ... == ... | conditionals.rb:15:10:16:5 | ...; ... | ...; ... |
@@ -24,8 +30,8 @@ unlessExprs
| conditionals.rb:40:1:42:3 | unless ... | UnlessExpr | conditionals.rb:40:8:40:12 | ... > ... | conditionals.rb:40:14:41:5 | ...; ... | (none) |
| conditionals.rb:45:1:49:3 | unless ... | UnlessExpr | conditionals.rb:45:8:45:13 | ... == ... | conditionals.rb:45:14:46:5 | ...; ... | ...; ... |
ifModifierExprs
| conditionals.rb:52:1:52:14 | ... if ... | IfModifierExpr | conditionals.rb:52:10:52:14 | ... > ... | conditionals.rb:52:1:52:5 | ... = ... | (none) |
| conditionals.rb:52:1:52:14 | ... if ... | IfModifierExpr | conditionals.rb:52:10:52:14 | ... > ... | conditionals.rb:52:1:52:5 | ... = ... |
unlessModifierExprs
| conditionals.rb:55:1:55:18 | ... unless ... | UnlessModifierExpr | conditionals.rb:55:14:55:18 | ... < ... | conditionals.rb:55:1:55:5 | ... = ... | (none) |
| conditionals.rb:55:1:55:18 | ... unless ... | UnlessModifierExpr | conditionals.rb:55:14:55:18 | ... < ... | conditionals.rb:55:1:55:5 | ... = ... |
ternaryIfExprs
| conditionals.rb:58:5:58:25 | ... ? ... : ... | TernaryIfExpr | conditionals.rb:58:5:58:9 | ... > ... | conditionals.rb:58:13:58:17 | ... + ... | conditionals.rb:58:21:58:25 | ... - ... |

View File

@@ -1,52 +1,41 @@
import ruby
query predicate conditionalExprsWithElse(
ConditionalExpr e, Expr cond, Expr thenExpr, Expr elseExpr, string pClass
query predicate conditionalExprs(
ConditionalExpr e, string pClass, Expr cond, Expr branch, boolean branchCond
) {
pClass = e.getAPrimaryQlClass() and
cond = e.getCondition() and
thenExpr = e.getThen() and
elseExpr = e.getElse()
branch = e.getBranch(branchCond)
}
query predicate conditionalExprsWithoutElse(
ConditionalExpr e, Expr cond, Expr thenExpr, string pClass
query predicate ifOrElsifExprs(
IfOrElsifExpr e, string pClass, Expr cond, ExprSequence thenExpr, string elseStr
) {
pClass = e.getAPrimaryQlClass() and
cond = e.getCondition() and
thenExpr = e.getThen() and
not exists(e.getElse())
}
predicate helper(ConditionalExpr e, string pClass, Expr cond, Expr thenExpr, string elseStr) {
pClass = e.getAPrimaryQlClass() and
cond = e.getCondition() and
thenExpr = e.getThen() and
if exists(e.getElse()) then elseStr = e.getElse().toString() else elseStr = "(none)"
}
query predicate ifOrElsifExprs(
IfOrElsifExpr e, string pClass, Expr cond, ExprSequence thenExpr, string elseStr
) {
helper(e, pClass, cond, thenExpr, elseStr)
}
query predicate unlessExprs(
UnlessExpr e, string pClass, Expr cond, ExprSequence thenExpr, string elseStr
) {
helper(e, pClass, cond, thenExpr, elseStr)
pClass = e.getAPrimaryQlClass() and
cond = e.getCondition() and
thenExpr = e.getThen() and
if exists(e.getElse()) then elseStr = e.getElse().toString() else elseStr = "(none)"
}
query predicate ifModifierExprs(
IfModifierExpr e, string pClass, Expr cond, Expr thenExpr, string elseStr
) {
helper(e, pClass, cond, thenExpr, elseStr)
query predicate ifModifierExprs(IfModifierExpr e, string pClass, Expr cond, Expr expr) {
pClass = e.getAPrimaryQlClass() and
cond = e.getCondition() and
expr = e.getExpr()
}
query predicate unlessModifierExprs(
UnlessModifierExpr e, string pClass, Expr cond, Expr thenExpr, string elseStr
) {
helper(e, pClass, cond, thenExpr, elseStr)
query predicate unlessModifierExprs(UnlessModifierExpr e, string pClass, Expr cond, Expr expr) {
pClass = e.getAPrimaryQlClass() and
cond = e.getCondition() and
expr = e.getExpr()
}
query predicate ternaryIfExprs(