C++: add support for enums in HashCons

This commit is contained in:
Robert Marsh
2018-08-27 11:48:37 -07:00
parent 91da02bacf
commit e6314c5f35
3 changed files with 36 additions and 0 deletions

View File

@@ -43,6 +43,8 @@ import cpp
private cached newtype HCBase =
HC_IntLiteral(int val, Type t) { mk_IntLiteral(val,t,_) }
or
HC_EnumConstantAccess(EnumConstant val, Type t) { mk_EnumConstantAccess(val,t,_) }
or
HC_FloatLiteral(float val, Type t) { mk_FloatLiteral(val,t,_) }
or
HC_StringLiteral(string val, Type t) {mk_StringLiteral(val,t,_)}
@@ -179,6 +181,17 @@ private predicate mk_IntLiteral(int val, Type t, Expr e) {
t = e.getType().getUnspecifiedType()
}
private predicate analyzableEnumConstantAccess(EnumConstantAccess e) {
strictcount (e.getValue().toInt()) = 1 and
strictcount (e.getType().getUnspecifiedType()) = 1 and
e.getType().getUnspecifiedType() instanceof Enum
}
private predicate mk_EnumConstantAccess(EnumConstant val, Type t, Expr e) {
analyzableEnumConstantAccess(e) and
val = e.(EnumConstantAccess).getTarget() and
t = e.getType().getUnspecifiedType()
}
private predicate analyzableFloatLiteral(Literal e) {
strictcount (e.getValue().toFloat()) = 1 and
@@ -446,6 +459,10 @@ cached HC hashCons(Expr e) {
| mk_IntLiteral(val, t, e) and
result = HC_IntLiteral(val, t))
or
exists (EnumConstant val, Type t
| mk_EnumConstantAccess(val, t, e) and
result = HC_EnumConstantAccess(val, t))
or
exists (float val, Type t
| mk_FloatLiteral(val, t, e) and
result = HC_FloatLiteral(val, t))
@@ -521,6 +538,7 @@ cached HC hashCons(Expr e) {
*/
predicate analyzableExpr(Expr e, string kind) {
(analyzableIntLiteral(e) and kind = "IntLiteral") or
(analyzableEnumConstantAccess(e) and kind = "EnumConstantAccess") or
(analyzableFloatLiteral(e) and kind = "FloatLiteral") or
(analyzableStringLiteral(e) and kind = "StringLiteral") or
(analyzableNullptr(e) and kind = "Nullptr") or

View File

@@ -67,3 +67,5 @@
| test.cpp:150:3:150:5 | ... ++ | 150:c3-c5 150:c9-c11 151:c3-c5 151:c9-c11 152:c10-c12 |
| test.cpp:150:3:150:11 | ... + ... | 150:c3-c11 151:c3-c11 |
| test.cpp:156:14:156:20 | 0 | 156:c14-c20 156:c3-c9 157:c10-c16 |
| test.cpp:171:3:171:6 | (int)... | 171:c3-c6 172:c3-c6 |
| test.cpp:171:3:171:6 | e1x1 | 171:c3-c6 172:c3-c6 |

View File

@@ -156,3 +156,19 @@ void* test11() {
nullptr == nullptr;
return nullptr;
}
enum t1 {
e1x1 = 1,
e1x2 = 2
};
enum t2 {
e2x1 = 1,
e2x2 = 2
};
int test12() {
e1x1 == e2x1;
e1x1 == e2x2;
return e1x2;
}