Skip to content

Commit 799ec23

Browse files
hvitvedhmac
authored andcommitted
Ruby: Generalize ExprChildMapping logic to AstNodes
1 parent 322f835 commit 799ec23

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

ruby/ql/lib/codeql/ruby/controlflow/CfgNodes.qll

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class StringComponentCfgNode extends AstCfgNode {
132132
string getValueText() { result = this.getNode().(StringComponent).getValueText() }
133133
}
134134

135-
private Expr desugar(Expr n) {
135+
private AstNode desugar(AstNode n) {
136136
result = n.getDesugared()
137137
or
138138
not exists(n.getDesugared()) and
@@ -147,10 +147,10 @@ abstract private class ExprChildMapping extends Expr {
147147
* Holds if `child` is a (possibly nested) child of this expression
148148
* for which we would like to find a matching CFG child.
149149
*/
150-
abstract predicate relevantChild(Expr child);
150+
abstract predicate relevantChild(AstNode child);
151151

152152
pragma[nomagic]
153-
private predicate reachesBasicBlock(Expr child, CfgNode cfn, BasicBlock bb) {
153+
private predicate reachesBasicBlock(AstNode child, CfgNode cfn, BasicBlock bb) {
154154
this.relevantChild(child) and
155155
cfn = this.getAControlFlowNode() and
156156
bb.getANode() = cfn
@@ -170,16 +170,16 @@ abstract private class ExprChildMapping extends Expr {
170170
* The path never escapes the syntactic scope of this expression.
171171
*/
172172
cached
173-
predicate hasCfgChild(Expr child, CfgNode cfn, CfgNode cfnChild) {
173+
predicate hasCfgChild(AstNode child, CfgNode cfn, CfgNode cfnChild) {
174174
this.reachesBasicBlock(child, cfn, cfnChild.getBasicBlock()) and
175-
cfnChild = desugar(child).getAControlFlowNode()
175+
cfnChild.getNode() = desugar(child)
176176
}
177177
}
178178

179179
/** Provides classes for control-flow nodes that wrap AST expressions. */
180180
module ExprNodes {
181181
private class LiteralChildMapping extends ExprChildMapping, Literal {
182-
override predicate relevantChild(Expr e) { none() }
182+
override predicate relevantChild(AstNode n) { none() }
183183
}
184184

185185
/** A control-flow node that wraps an `ArrayLiteral` AST expression. */
@@ -192,7 +192,7 @@ module ExprNodes {
192192
}
193193

194194
private class AssignExprChildMapping extends ExprChildMapping, AssignExpr {
195-
override predicate relevantChild(Expr e) { e = this.getAnOperand() }
195+
override predicate relevantChild(AstNode n) { n = this.getAnOperand() }
196196
}
197197

198198
/** A control-flow node that wraps an `AssignExpr` AST expression. */
@@ -209,7 +209,7 @@ module ExprNodes {
209209
}
210210

211211
private class OperationExprChildMapping extends ExprChildMapping, Operation {
212-
override predicate relevantChild(Expr e) { e = this.getAnOperand() }
212+
override predicate relevantChild(AstNode n) { n = this.getAnOperand() }
213213
}
214214

215215
/** A control-flow node that wraps an `Operation` AST expression. */
@@ -292,7 +292,7 @@ module ExprNodes {
292292
}
293293

294294
private class BlockArgumentChildMapping extends ExprChildMapping, BlockArgument {
295-
override predicate relevantChild(Expr e) { e = this.getValue() }
295+
override predicate relevantChild(AstNode n) { n = this.getValue() }
296296
}
297297

298298
/** A control-flow node that wraps a `BlockArgument` AST expression. */
@@ -306,8 +306,8 @@ module ExprNodes {
306306
}
307307

308308
private class CallExprChildMapping extends ExprChildMapping, Call {
309-
override predicate relevantChild(Expr e) {
310-
e = [this.getAnArgument(), this.(MethodCall).getReceiver(), this.(MethodCall).getBlock()]
309+
override predicate relevantChild(AstNode n) {
310+
n = [this.getAnArgument(), this.(MethodCall).getReceiver(), this.(MethodCall).getBlock()]
311311
}
312312
}
313313

@@ -340,7 +340,7 @@ module ExprNodes {
340340
}
341341

342342
private class CaseExprChildMapping extends ExprChildMapping, CaseExpr {
343-
override predicate relevantChild(Expr e) { e = this.getValue() }
343+
override predicate relevantChild(AstNode e) { e = this.getValue() }
344344
}
345345

346346
/** A control-flow node that wraps a `MethodCall` AST expression. */
@@ -361,7 +361,7 @@ module ExprNodes {
361361
}
362362

363363
private class ConditionalExprChildMapping extends ExprChildMapping, ConditionalExpr {
364-
override predicate relevantChild(Expr e) { e = this.getCondition() or e = this.getBranch(_) }
364+
override predicate relevantChild(AstNode n) { n = [this.getCondition(), this.getBranch(_)] }
365365
}
366366

367367
/** A control-flow node that wraps a `ConditionalExpr` AST expression. */
@@ -381,7 +381,7 @@ module ExprNodes {
381381
}
382382

383383
private class ConstantAccessChildMapping extends ExprChildMapping, ConstantAccess {
384-
override predicate relevantChild(Expr e) { e = this.getScopeExpr() }
384+
override predicate relevantChild(AstNode n) { n = this.getScopeExpr() }
385385
}
386386

387387
/** A control-flow node that wraps a `ConditionalExpr` AST expression. */
@@ -397,7 +397,7 @@ module ExprNodes {
397397
}
398398

399399
private class StmtSequenceChildMapping extends ExprChildMapping, StmtSequence {
400-
override predicate relevantChild(Expr e) { e = this.getLastStmt() }
400+
override predicate relevantChild(AstNode n) { n = this.getLastStmt() }
401401
}
402402

403403
/** A control-flow node that wraps a `StmtSequence` AST expression. */
@@ -411,7 +411,7 @@ module ExprNodes {
411411
}
412412

413413
private class ForExprChildMapping extends ExprChildMapping, ForExpr {
414-
override predicate relevantChild(Expr e) { e = this.getValue() }
414+
override predicate relevantChild(AstNode n) { n = this.getValue() }
415415
}
416416

417417
/** A control-flow node that wraps a `ForExpr` AST expression. */
@@ -430,7 +430,7 @@ module ExprNodes {
430430
}
431431

432432
private class PairChildMapping extends ExprChildMapping, Pair {
433-
override predicate relevantChild(Expr e) { e = this.getKey() or e = this.getValue() }
433+
override predicate relevantChild(AstNode n) { n = [this.getKey(), this.getValue()] }
434434
}
435435

436436
/** A control-flow node that wraps a `Pair` AST expression. */
@@ -475,7 +475,7 @@ module ExprNodes {
475475
}
476476

477477
private class StringlikeLiteralChildMapping extends ExprChildMapping, StringlikeLiteral {
478-
override predicate relevantChild(Expr e) { e = this.getComponent(_) }
478+
override predicate relevantChild(AstNode n) { n = this.getComponent(_) }
479479
}
480480

481481
/** A control-flow node that wraps a `StringlikeLiteral` AST expression. */

0 commit comments

Comments
 (0)