Skip to content

Commit 1acc111

Browse files
committed
C++: add getSyntheticDestructor to Expr and Stmt
1 parent 32a2ea1 commit 1acc111

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ class Expr extends StmtParent, @expr {
5858
/** Gets the parent of this expression, if any. */
5959
Element getParent() { exprparents(underlyingElement(this), _, unresolveElement(result)) }
6060

61+
/**
62+
* Gets the `n`th compiler-generated destructor call that is performed after this expression, in
63+
* order of destruction.
64+
*/
65+
DestructorCall getSyntheticDestructor(int n) {
66+
synthetic_destructor_call(this, max(int i | synthetic_destructor_call(this, i, _)) - n, result)
67+
}
68+
69+
/**
70+
* Gets a compiler-generated destructor call that is performed after this expression.
71+
*/
72+
DestructorCall getASyntheticDestructor() {
73+
synthetic_destructor_call(this, _, result)
74+
}
75+
6176
/** Gets the location of this expression. */
6277
override Location getLocation() {
6378
result = this.getExprLocationOverride()

cpp/ql/lib/semmle/code/cpp/stmts/Stmt.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ class Stmt extends StmtParent, @stmt {
5959
)
6060
}
6161

62+
/**
63+
* Gets the `n`th compiler-generated destructor call that is performed after this statement, in
64+
* order of destruction.
65+
*/
66+
DestructorCall getSyntheticDestructor(int n) {
67+
synthetic_destructor_call(this, max(int i | synthetic_destructor_call(this, i, _)) - n, result)
68+
}
69+
70+
DestructorCall getASyntheticDestructor() {
71+
synthetic_destructor_call(this, _, result)
72+
}
73+
6274
override Location getLocation() { stmts(underlyingElement(this), _, result) }
6375

6476
override string toString() { none() }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
exprDestructors
2+
stmtDestructors
3+
| destructors2.cpp:24:13:24:19 | return ... | 0 | destructors2.cpp:27:5:27:5 | call to ~Class2 | destructors2.cpp:27:5:27:5 | c |
4+
| destructors2.cpp:27:5:27:5 | return ... | 0 | destructors2.cpp:27:5:27:5 | call to ~Inner | destructors2.cpp:27:5:27:5 | inner |
5+
| destructors2.cpp:27:5:27:5 | return ... | 1 | destructors2.cpp:27:5:27:5 | call to ~Class2 | destructors2.cpp:27:5:27:5 | c |
6+
| destructors.cpp:10:5:26:5 | { ... } | 0 | destructors.cpp:26:5:26:5 | call to ~C | destructors.cpp:26:5:26:5 | c21 |
7+
| destructors.cpp:10:5:26:5 | { ... } | 1 | destructors.cpp:26:5:26:5 | call to ~C | destructors.cpp:26:5:26:5 | c20 |
8+
| destructors.cpp:12:9:14:9 | { ... } | 0 | destructors.cpp:14:9:14:9 | call to ~C | destructors.cpp:14:9:14:9 | c30 |
9+
| destructors.cpp:15:9:21:9 | { ... } | 0 | destructors.cpp:21:9:21:9 | call to ~C | destructors.cpp:21:9:21:9 | c33 |
10+
| destructors.cpp:15:9:21:9 | { ... } | 1 | destructors.cpp:21:9:21:9 | call to ~C | destructors.cpp:21:9:21:9 | c32 |
11+
| destructors.cpp:15:9:21:9 | { ... } | 2 | destructors.cpp:21:9:21:9 | call to ~C | destructors.cpp:21:9:21:9 | c31 |
12+
| destructors.cpp:17:21:17:29 | goto ... | 0 | destructors.cpp:21:9:21:9 | call to ~C | destructors.cpp:21:9:21:9 | c31 |
13+
| destructors.cpp:17:21:17:29 | goto ... | 1 | destructors.cpp:26:5:26:5 | call to ~C | destructors.cpp:26:5:26:5 | c20 |
14+
| destructors.cpp:19:21:19:27 | return ... | 0 | destructors.cpp:21:9:21:9 | call to ~C | destructors.cpp:21:9:21:9 | c32 |
15+
| destructors.cpp:19:21:19:27 | return ... | 1 | destructors.cpp:21:9:21:9 | call to ~C | destructors.cpp:21:9:21:9 | c31 |
16+
| destructors.cpp:19:21:19:27 | return ... | 2 | destructors.cpp:26:5:26:5 | call to ~C | destructors.cpp:26:5:26:5 | c20 |
17+
| destructors.cpp:19:21:19:27 | return ... | 3 | destructors.cpp:35:1:35:1 | call to ~C | destructors.cpp:35:1:35:1 | c10 |
18+
| destructors.cpp:22:9:24:9 | { ... } | 0 | destructors.cpp:24:9:24:9 | call to ~C | destructors.cpp:24:9:24:9 | c34 |
19+
| destructors.cpp:27:5:29:5 | { ... } | 0 | destructors.cpp:29:5:29:5 | call to ~C | destructors.cpp:29:5:29:5 | c22 |
20+
| destructors.cpp:30:5:33:5 | { ... } | 0 | destructors.cpp:33:5:33:5 | call to ~C | destructors.cpp:33:5:33:5 | c23 |
21+
| destructors.cpp:35:1:35:1 | return ... | 0 | destructors.cpp:35:1:35:1 | call to ~C | destructors.cpp:35:1:35:1 | c11 |
22+
| destructors.cpp:35:1:35:1 | return ... | 1 | destructors.cpp:35:1:35:1 | call to ~C | destructors.cpp:35:1:35:1 | c10 |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import cpp
2+
3+
query predicate exprDestructors(Expr e, int i, DestructorCall d, Expr destructed) {
4+
d = e.getSyntheticDestructor(i) and
5+
d.getQualifier() = destructed
6+
}
7+
8+
query predicate stmtDestructors(Stmt s, int i, DestructorCall d, Expr destructed) {
9+
d = s.getSyntheticDestructor(i) and
10+
d.getQualifier() = destructed
11+
}

0 commit comments

Comments
 (0)