Skip to content

Commit ae4f6ed

Browse files
authored
Merge pull request github#3204 from jbj/Expr-location-workaround
C++: Move Expr location workaround to Expr.qll
2 parents a305d39 + 7d4d435 commit ae4f6ed

File tree

33 files changed

+1028
-1057
lines changed

33 files changed

+1028
-1057
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
@@ -116,33 +116,12 @@ class ExprNode extends Node, TExprNode {
116116

117117
override string toString() { result = expr.toString() }
118118

119-
override Location getLocation() {
120-
result = getExprLocationOverride(expr)
121-
or
122-
not exists(getExprLocationOverride(expr)) and
123-
result = expr.getLocation()
124-
}
119+
override Location getLocation() { result = expr.getLocation() }
125120

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

130-
/**
131-
* Gets a location for `e` that's more accurate than `e.getLocation()`, if any.
132-
*/
133-
private Location getExprLocationOverride(Expr e) {
134-
// Base case: the parent has a better location than `e`.
135-
e.getLocation() instanceof UnknownExprLocation and
136-
result = e.getParent().getLocation() and
137-
not result instanceof UnknownLocation
138-
or
139-
// Recursive case: the parent has a location override that's better than what
140-
// `e` has.
141-
e.getLocation() instanceof UnknownExprLocation and
142-
result = getExprLocationOverride(e.getParent()) and
143-
not result instanceof UnknownLocation
144-
}
145-
146125
abstract class ParameterNode extends Node, TNode {
147126
/**
148127
* 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() {

cpp/ql/test/examples/expressions/PrintAST.expected

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -430,28 +430,28 @@ DynamicCast.cpp:
430430
#-----| Conversion = [BaseClassConversion] base class conversion
431431
#-----| Type = [PointerType] Base *
432432
#-----| ValueCategory = prvalue
433-
#-----| expr: [ThisExpr] this
434-
#-----| Type = [PointerType] Derived *
435-
#-----| ValueCategory = prvalue(load)
433+
# 4| expr: [ThisExpr] this
434+
# 4| Type = [PointerType] Derived *
435+
# 4| ValueCategory = prvalue(load)
436436
#-----| 0: [ReferenceToExpr] (reference to)
437437
#-----| Type = [LValueReferenceType] const Base &
438438
#-----| ValueCategory = prvalue
439-
#-----| expr: [PointerDereferenceExpr] * ...
440-
#-----| Type = [SpecifiedType] const Base
441-
#-----| ValueCategory = lvalue
439+
# 4| expr: [PointerDereferenceExpr] * ...
440+
# 4| Type = [SpecifiedType] const Base
441+
# 4| ValueCategory = lvalue
442442
#-----| 0: [CStyleCast] (const Base *)...
443443
#-----| Conversion = [BaseClassConversion] base class conversion
444444
#-----| Type = [PointerType] const Base *
445445
#-----| ValueCategory = prvalue
446-
#-----| expr: [AddressOfExpr] & ...
447-
#-----| Type = [PointerType] const Derived *
448-
#-----| ValueCategory = prvalue
446+
# 4| expr: [AddressOfExpr] & ...
447+
# 4| Type = [PointerType] const Derived *
448+
# 4| ValueCategory = prvalue
449449
#-----| 0: [ReferenceDereferenceExpr] (reference dereference)
450450
#-----| Type = [SpecifiedType] const Derived
451451
#-----| ValueCategory = lvalue
452-
#-----| expr: [VariableAccess] p#0
453-
#-----| Type = [LValueReferenceType] const Derived &
454-
#-----| ValueCategory = prvalue(load)
452+
# 4| expr: [VariableAccess] p#0
453+
# 4| Type = [LValueReferenceType] const Derived &
454+
# 4| ValueCategory = prvalue(load)
455455
#-----| 1: [ReturnStmt] return ...
456456
#-----| 0: [ReferenceToExpr] (reference to)
457457
#-----| Type = [LValueReferenceType] Derived &
@@ -1248,9 +1248,9 @@ union_etc.cpp:
12481248
# 6| 0: [PointerFieldAccess] x
12491249
# 6| Type = [IntType] int
12501250
# 6| ValueCategory = lvalue
1251-
#-----| -1: [ThisExpr] this
1252-
#-----| Type = [PointerType] S *
1253-
#-----| ValueCategory = prvalue(load)
1251+
# 6| -1: [ThisExpr] this
1252+
# 6| Type = [PointerType] S *
1253+
# 6| ValueCategory = prvalue(load)
12541254
# 6| 1: [VariableAccess] val
12551255
# 6| Type = [IntType] int
12561256
# 6| ValueCategory = prvalue(load)
@@ -1431,9 +1431,9 @@ union_etc.cpp:
14311431
# 33| 0: [PointerFieldAccess] q
14321432
# 33| Type = [IntType] int
14331433
# 33| ValueCategory = lvalue
1434-
#-----| -1: [ThisExpr] this
1435-
#-----| Type = [PointerType] T *
1436-
#-----| ValueCategory = prvalue(load)
1434+
# 33| -1: [ThisExpr] this
1435+
# 33| Type = [PointerType] T *
1436+
# 33| ValueCategory = prvalue(load)
14371437
# 33| 1: [VariableAccess] val
14381438
# 33| Type = [IntType] int
14391439
# 33| ValueCategory = prvalue(load)

cpp/ql/test/library-tests/blocks/cpp/exprs.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@
7979
| blocks.cpp:57:12:57:32 | call to expression |
8080
| blocks.cpp:57:14:57:30 | ^ { ... } |
8181
| blocks.cpp:57:23:57:26 | four |
82-
| file://:0:0:0:0 | this |
82+
| blocks.cpp:57:23:57:26 | this |
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
| edg.c:12:14:12:51 | (int)... | 0 | 0 |
22
| edg.c:12:14:12:51 | __builtin_offsetof | 1 | 1 |
3+
| edg.c:12:14:12:51 | mystruct | 0 | 0 |
4+
| edg.c:12:49:12:50 | 0 | 0 | 0 |
5+
| edg.c:12:49:12:50 | * ... | 0 | 0 |
36
| edg.c:12:49:12:50 | f2 | 0 | 0 |
47
| edg.c:13:14:13:45 | 0 | 0 | 0 |
58
| edg.c:13:14:13:45 | & ... | 0 | 0 |
@@ -10,6 +13,3 @@
1013
| edg.c:13:14:13:45 | (size_t)... | 0 | 0 |
1114
| edg.c:13:14:13:45 | __INTADDR__ | 1 | 1 |
1215
| edg.c:13:43:13:44 | f2 | 0 | 0 |
13-
| file://:0:0:0:0 | 0 | 0 | 0 |
14-
| file://:0:0:0:0 | * ... | 0 | 0 |
15-
| file://:0:0:0:0 | mystruct | 0 | 0 |

0 commit comments

Comments
 (0)