Skip to content

Commit bf7614a

Browse files
committed
C++: Move Expr location workaround to Expr.qll
This workaround from `DataFlowUtil.qll` should be useful for any query that selects an `Expr`. In particular, it's useful for IR data flow. This commit does not include test changes.
1 parent 1096e5d commit bf7614a

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -114,33 +114,12 @@ class ExprNode extends Node, TExprNode {
114114

115115
override string toString() { result = expr.toString() }
116116

117-
override Location getLocation() {
118-
result = getExprLocationOverride(expr)
119-
or
120-
not exists(getExprLocationOverride(expr)) and
121-
result = expr.getLocation()
122-
}
117+
override Location getLocation() { result = expr.getLocation() }
123118

124119
/** Gets the expression corresponding to this node. */
125120
Expr getExpr() { result = expr }
126121
}
127122

128-
/**
129-
* Gets a location for `e` that's more accurate than `e.getLocation()`, if any.
130-
*/
131-
private Location getExprLocationOverride(Expr e) {
132-
// Base case: the parent has a better location than `e`.
133-
e.getLocation() instanceof UnknownExprLocation and
134-
result = e.getParent().getLocation() and
135-
not result instanceof UnknownLocation
136-
or
137-
// Recursive case: the parent has a location override that's better than what
138-
// `e` has.
139-
e.getLocation() instanceof UnknownExprLocation and
140-
result = getExprLocationOverride(e.getParent()) and
141-
not result instanceof UnknownLocation
142-
}
143-
144123
abstract class ParameterNode extends Node, TNode {
145124
/**
146125
* Holds if this node is the parameter of `c` at the specified (zero-based)

cpp/ql/src/semmle/code/cpp/exprs/Expr.qll

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,32 @@ class Expr extends StmtParent, @expr {
5353
Element getParent() { exprparents(underlyingElement(this), _, unresolveElement(result)) }
5454

5555
/** Gets the location of this expression. */
56-
override Location getLocation() { exprs(underlyingElement(this), _, result) }
56+
override Location getLocation() {
57+
result = this.getExprLocationOverride()
58+
or
59+
not exists(this.getExprLocationOverride()) and
60+
result = this.getDbLocation()
61+
}
62+
63+
/**
64+
* Gets a location for this expression that's more accurate than
65+
* `getDbLocation()`, if any.
66+
*/
67+
private Location getExprLocationOverride() {
68+
// Base case: the parent has a better location than `this`.
69+
this.getDbLocation() instanceof UnknownExprLocation and
70+
result = this.getParent().(Expr).getDbLocation() and
71+
not result instanceof UnknownLocation
72+
or
73+
// Recursive case: the parent has a location override that's better than
74+
// what `this` has.
75+
this.getDbLocation() instanceof UnknownExprLocation and
76+
result = this.getParent().(Expr).getExprLocationOverride() and
77+
not result instanceof UnknownLocation
78+
}
79+
80+
/** Gets the location of this expressions, raw from the database. */
81+
private Location getDbLocation() { exprs(underlyingElement(this), _, result) }
5782

5883
/** Holds if this is an auxiliary expression generated by the compiler. */
5984
predicate isCompilerGenerated() {

0 commit comments

Comments
 (0)