Skip to content

Commit 7ff732d

Browse files
committed
C++: Use OO dispatch for getType and getFunction
1 parent 972d008 commit 7ff732d

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowUtil.qll

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@ private newtype TNode =
2424
*/
2525
class Node extends TNode {
2626
/** Gets the function to which this node belongs. */
27-
Function getFunction() {
28-
result = this.asExpr().getEnclosingFunction()
29-
or
30-
result = this.asParameter().getFunction()
31-
or
32-
result = this.asUninitialized().getFunction()
33-
or
34-
result = this.asDefiningArgument().getEnclosingFunction()
35-
}
27+
Function getFunction() { none() } // overridden in subclasses
3628

3729
/**
3830
* INTERNAL: Do not use. Alternative name for `getFunction`.
@@ -42,13 +34,7 @@ class Node extends TNode {
4234
}
4335

4436
/** Gets the type of this node. */
45-
Type getType() {
46-
result = this.asExpr().getType()
47-
or
48-
result = asVariable(this).getType()
49-
or
50-
result = this.asDefiningVariableAccess().getType()
51-
}
37+
Type getType() { none() } // overridden in subclasses
5238

5339
/** Gets the expression corresponding to this node, if any. */
5440
Expr asExpr() { result = this.(ExprNode).getExpr() }
@@ -90,6 +76,8 @@ class Node extends TNode {
9076
class ExprNode extends Node, TExprNode {
9177
Expr expr;
9278
ExprNode() { this = TExprNode(expr) }
79+
override Function getFunction() { result = expr.getEnclosingFunction() }
80+
override Type getType() { result = expr.getType() }
9381
override string toString() { result = expr.toString() }
9482
override Location getLocation() { result = expr.getLocation() }
9583
/** Gets the expression corresponding to this node. */
@@ -103,6 +91,8 @@ class ExprNode extends Node, TExprNode {
10391
class ParameterNode extends Node, TParameterNode {
10492
Parameter param;
10593
ParameterNode() { this = TParameterNode(param) }
94+
override Function getFunction() { result = param.getFunction() }
95+
override Type getType() { result = param.getType() }
10696
override string toString() { result = param.toString() }
10797
override Location getLocation() { result = param.getLocation() }
10898
/** Gets the parameter corresponding to this node. */
@@ -131,6 +121,8 @@ class DefinitionByReferenceNode extends Node, TDefinitionByReferenceNode {
131121
Expr argument;
132122

133123
DefinitionByReferenceNode() { this = TDefinitionByReferenceNode(va, argument) }
124+
override Function getFunction() { result = va.getEnclosingFunction() }
125+
override Type getType() { result = va.getType() }
134126
override string toString() { result = "ref arg " + argument.toString() }
135127
override Location getLocation() { result = argument.getLocation() }
136128
/** Gets the argument corresponding to this node. */
@@ -146,6 +138,8 @@ class DefinitionByReferenceNode extends Node, TDefinitionByReferenceNode {
146138
class UninitializedNode extends Node, TUninitializedNode {
147139
LocalVariable v;
148140
UninitializedNode() { this = TUninitializedNode(v) }
141+
override Function getFunction() { result = v.getFunction() }
142+
override Type getType() { result = v.getType() }
149143
override string toString() { result = v.toString() }
150144
override Location getLocation() { result = v.getLocation() }
151145
/** Gets the uninitialized local variable corresponding to this node. */
@@ -206,12 +200,6 @@ UninitializedNode uninitializedNode(LocalVariable v) {
206200
result.getLocalVariable() = v
207201
}
208202

209-
private Variable asVariable(Node node) {
210-
result = node.asParameter()
211-
or
212-
result = node.asUninitialized()
213-
}
214-
215203
/**
216204
* Holds if data flows from `nodeFrom` to `nodeTo` in exactly one local
217205
* (intra-procedural) step.
@@ -225,7 +213,9 @@ predicate localFlowStep(Node nodeFrom, Node nodeTo) {
225213
(
226214
exprToVarStep(nodeFrom.asExpr(), var)
227215
or
228-
varSourceBaseCase(var, asVariable(nodeFrom))
216+
varSourceBaseCase(var, nodeFrom.asParameter())
217+
or
218+
varSourceBaseCase(var, nodeFrom.asUninitialized())
229219
or
230220
var.definedByReference(nodeFrom.asDefiningArgument())
231221
) and

0 commit comments

Comments
 (0)