Skip to content

Commit 7ea4582

Browse files
committed
C++: HashCons for throw
1 parent d37db46 commit 7ea4582

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

cpp/ql/src/semmle/code/cpp/valuenumbering/HashCons.qll

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ private cached newtype HCBase =
119119
or
120120
HC_DeleteArrayExpr(HashCons child) {mk_DeleteArrayExpr(child, _)}
121121
or
122+
HC_ThrowExpr(HashCons child) {mk_ThrowExpr(child, _)}
123+
or
124+
HC_ReThrowExpr()
125+
or
122126
// Any expression that is not handled by the cases above is
123127
// given a unique number based on the expression itself.
124128
HC_Unanalyzable(Expr e) { not analyzableExpr(e,_) }
@@ -228,6 +232,8 @@ class HashCons extends HCBase {
228232
if this instanceof HC_ClassAggregateLiteral then result = "ClassAggreagateLiteral" else
229233
if this instanceof HC_DeleteExpr then result = "DeleteExpr" else
230234
if this instanceof HC_DeleteArrayExpr then result = "DeleteArrayExpr" else
235+
if this instanceof HC_ThrowExpr then result = "ThrowExpr" else
236+
if this instanceof HC_ReThrowExpr then result = "ReThrowExpr" else
231237
result = "error"
232238
}
233239

@@ -828,6 +834,23 @@ private predicate mk_ArrayAggregateLiteral(Type t, HC_Array hca, ArrayAggregateL
828834
)
829835
}
830836

837+
private predicate analyzableThrowExpr(ThrowExpr te) {
838+
strictcount(te.getExpr().getFullyConverted()) = 1
839+
}
840+
841+
private predicate mk_ThrowExpr(HashCons hc, ThrowExpr te) {
842+
analyzableThrowExpr(te) and
843+
hc.getAnExpr() = te.getExpr().getFullyConverted()
844+
}
845+
846+
private predicate analyzableReThrowExpr(ReThrowExpr rte) {
847+
any()
848+
}
849+
850+
private predicate mk_ReThrowExpr(ReThrowExpr te) {
851+
any()
852+
}
853+
831854
/** Gets the hash-cons of expression `e`. */
832855
cached HashCons hashCons(Expr e) {
833856
exists (int val, Type t
@@ -946,6 +969,16 @@ cached HashCons hashCons(Expr e) {
946969
result = HC_DeleteArrayExpr(child)
947970
)
948971
or
972+
exists(HashCons child
973+
| mk_ThrowExpr(child, e) and
974+
result = HC_ThrowExpr(child)
975+
)
976+
or
977+
(
978+
mk_ReThrowExpr(e) and
979+
result = HC_ReThrowExpr()
980+
)
981+
or
949982
(
950983
mk_Nullptr(e) and
951984
result = HC_Nullptr()
@@ -987,5 +1020,7 @@ predicate analyzableExpr(Expr e, string kind) {
9871020
(analyzableClassAggregateLiteral(e) and kind = "ClassAggregateLiteral") or
9881021
(analyzableArrayAggregateLiteral(e) and kind = "ArrayAggregateLiteral") or
9891022
(analyzableDeleteExpr(e) and kind = "DeleteExpr") or
990-
(analyzableDeleteArrayExpr(e) and kind = "DeleteArrayExpr")
1023+
(analyzableDeleteArrayExpr(e) and kind = "DeleteArrayExpr") or
1024+
(analyzableThrowExpr(e) and kind = "ThrowExpr") or
1025+
(analyzableReThrowExpr(e) and kind = "ReThrowExpr")
9911026
}

cpp/ql/test/library-tests/valuenumbering/HashCons/HashCons.expected

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
| test.cpp:92:11:92:11 | x | 92:c11-c11 93:c10-c10 |
3939
| test.cpp:97:3:97:3 | x | 97:c3-c3 98:c3-c3 |
4040
| test.cpp:97:3:97:5 | ... ++ | 97:c3-c5 98:c3-c5 |
41-
| test.cpp:103:10:103:11 | 1 | 103:c10-c11 104:c7-c7 107:c7-c7 108:c7-c7 10:c16-c16 179:c21-c21 247:c11-c11 248:c11-c11 271:c28-c28 272:c28-c28 274:c19-c19 274:c22-c22 288:c5-c5 292:c5-c5 297:c5-c5 |
41+
| test.cpp:103:10:103:11 | 1 | 103:c10-c11 104:c7-c7 107:c7-c7 108:c7-c7 10:c16-c16 179:c21-c21 247:c11-c11 248:c11-c11 271:c28-c28 272:c28-c28 274:c19-c19 274:c22-c22 288:c5-c5 292:c5-c5 297:c5-c5 302:c9-c9 303:c9-c9 |
4242
| test.cpp:104:3:104:3 | x | 104:c3-c3 105:c3-c3 106:c3-c3 107:c3-c3 108:c3-c3 |
43-
| test.cpp:105:7:105:7 | 2 | 105:c7-c7 106:c7-c7 107:c11-c11 108:c11-c11 21:c16-c16 249:c11-c11 271:c24-c24 271:c31-c31 272:c24-c24 272:c31-c31 273:c24-c24 274:c15-c15 275:c15-c15 275:c19-c19 275:c22-c22 277:c15-c15 278:c15-c15 289:c5-c5 293:c5-c5 296:c5-c5 |
43+
| test.cpp:105:7:105:7 | 2 | 105:c7-c7 106:c7-c7 107:c11-c11 108:c11-c11 21:c16-c16 249:c11-c11 271:c24-c24 271:c31-c31 272:c24-c24 272:c31-c31 273:c24-c24 274:c15-c15 275:c15-c15 275:c19-c19 275:c22-c22 277:c15-c15 278:c15-c15 289:c5-c5 293:c5-c5 296:c5-c5 304:c9-c9 305:c9-c9 |
4444
| test.cpp:107:7:107:11 | ... + ... | 107:c7-c11 108:c7-c11 |
4545
| test.cpp:110:15:110:17 | 1 | 110:c15-c17 111:c9-c11 |
4646
| test.cpp:110:15:110:17 | (char *)... | 110:c15-c17 111:c9-c11 |
@@ -104,3 +104,6 @@
104104
| test.cpp:277:3:277:19 | new[] | 277:c3-c19 278:c3-c19 |
105105
| test.cpp:277:3:277:19 | {...} | 277:c3-c19 278:c3-c19 |
106106
| test.cpp:287:15:290:3 | {...} | 287:c15-c3 291:c15-c3 |
107+
| test.cpp:302:3:302:9 | throw ... | 302:c3-c9 303:c3-c9 |
108+
| test.cpp:304:3:304:9 | throw ... | 304:c3-c9 305:c3-c9 |
109+
| test.cpp:306:3:306:7 | re-throw exception | 306:c3-c7 307:c3-c7 |

cpp/ql/test/library-tests/valuenumbering/HashCons/test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,12 @@ void test17() {
297297
1
298298
};
299299
}
300+
301+
void test18() {
302+
throw 1;
303+
throw 1;
304+
throw 2;
305+
throw 2;
306+
throw;
307+
throw;
308+
}

0 commit comments

Comments
 (0)