C++: first tests for HashCons

This commit is contained in:
Robert Marsh
2018-08-23 15:25:16 -07:00
parent 2d7109b8f5
commit 3c6a9c08a2
5 changed files with 155 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
| test.cpp:5:3:5:3 | x | 5:c3-c3 6:c3-c3 7:c7-c7 |
| test.cpp:5:7:5:8 | p0 | 5:c7-c8 6:c7-c8 |
| test.cpp:5:7:5:13 | ... + ... | 5:c7-c13 6:c7-c13 |
| test.cpp:5:12:5:13 | p1 | 5:c12-c13 6:c12-c13 |
| test.cpp:16:3:16:3 | x | 16:c3-c3 17:c3-c3 18:c7-c7 |
| test.cpp:16:7:16:8 | p0 | 16:c7-c8 17:c7-c8 |
| test.cpp:16:7:16:13 | ... + ... | 16:c7-c13 17:c7-c13 |
| test.cpp:16:7:16:24 | ... + ... | 16:c7-c24 17:c7-c24 |
| test.cpp:16:12:16:13 | p1 | 16:c12-c13 17:c12-c13 |
| test.cpp:16:17:16:24 | global01 | 16:c17-c24 17:c17-c24 |
| test.cpp:29:3:29:3 | x | 29:c3-c3 31:c3-c3 32:c7-c7 |
| test.cpp:29:7:29:8 | p0 | 29:c7-c8 31:c7-c8 |
| test.cpp:29:7:29:13 | ... + ... | 29:c7-c13 31:c7-c13 |
| test.cpp:29:7:29:24 | ... + ... | 29:c7-c24 31:c7-c24 |
| test.cpp:29:12:29:13 | p1 | 29:c12-c13 31:c12-c13 |
| test.cpp:29:17:29:24 | global02 | 29:c17-c24 31:c17-c24 |
| test.cpp:43:3:43:3 | x | 43:c3-c3 45:c3-c3 46:c7-c7 |
| test.cpp:43:7:43:8 | p0 | 43:c7-c8 45:c7-c8 |
| test.cpp:43:7:43:13 | ... + ... | 43:c7-c13 45:c7-c13 |
| test.cpp:43:7:43:24 | ... + ... | 43:c7-c24 45:c7-c24 |
| test.cpp:43:12:43:13 | p1 | 43:c12-c13 45:c12-c13 |
| test.cpp:43:17:43:24 | global03 | 43:c17-c24 45:c17-c24 |
| test.cpp:44:9:44:9 | 0 | 44:c9-c9 51:c25-c25 53:c18-c21 56:c39-c42 59:c17-c20 88:c12-c12 |
| test.cpp:53:10:53:13 | (int)... | 53:c10-c13 56:c21-c24 |
| test.cpp:53:10:53:13 | * ... | 53:c10-c13 56:c21-c24 |
| test.cpp:53:11:53:13 | str | 53:c11-c13 56:c22-c24 |
| test.cpp:53:18:53:21 | 0 | 53:c18-c21 56:c39-c42 59:c17-c20 |
| test.cpp:55:5:55:7 | ptr | 55:c5-c7 56:c14-c16 56:c32-c34 56:c47-c49 59:c10-c12 |
| test.cpp:56:13:56:16 | (int)... | 56:c13-c16 56:c31-c34 59:c9-c12 |
| test.cpp:56:13:56:16 | * ... | 56:c13-c16 56:c31-c34 59:c9-c12 |
| test.cpp:62:5:62:10 | result | 62:c5-c10 65:c10-c15 |
| test.cpp:79:7:79:7 | v | 79:c7-c7 80:c5-c5 |
| test.cpp:79:11:79:14 | vals | 79:c11-c14 79:c24-c27 |
| test.cpp:92:11:92:11 | x | 92:c11-c11 93:c10-c10 |
| test.cpp:97:3:97:3 | x | 97:c3-c3 98:c3-c3 |

View File

@@ -0,0 +1,12 @@
import cpp
import semmle.code.cpp.valuenumbering.HashCons
from HC h
where strictcount(h.getAnExpr()) > 1
select
h,
strictconcat(Location loc
| loc = h.getAnExpr().getLocation()
| loc.getStartLine() +
":c" + loc.getStartColumn() + "-c" + loc.getEndColumn()
, " ")

View File

@@ -0,0 +1,8 @@
import cpp
import semmle.code.cpp.valuenumbering.HashCons
// Every expression should have exactly one GVN.
// So this query should have zero results.
from Expr e
where count(hashCons(e)) != 1
select e, concat(HC h | h = hashCons(e) | h.getKind(), ", ")

View File

@@ -0,0 +1,100 @@
int test00(int p0, int p1) {
int x, y;
unsigned char b;
x = p0 + p1;
x = p0 + p1; // Same value as previous line. Should the assignment also be matched?
y = x;
}
int global01 = 1;
int test01(int p0, int p1) {
int x, y;
unsigned char b;
x = p0 + p1 + global01;
x = p0 + p1 + global01; // Same structure as previous line.
y = x; // x is the same as x above
}
int global02 = 2;
void change_global02(); // Just a declaration
int test02(int p0, int p1) {
int x, y;
unsigned char b;
x = p0 + p1 + global02;
change_global02();
x = p0 + p1 + global02; // same HashCons as above
y = x;
}
int global03 = 3;
void change_global03(); // Just a declaration
int test03(int p0, int p1, int* p2) {
int x, y;
unsigned char b;
x = p0 + p1 + global03;
*p2 = 0;
x = p0 + p1 + global03; // same HashCons as 43
y = x;
}
unsigned int my_strspn(const char *str, const char *chars) {
const char *ptr;
unsigned int result = 0;
while (*str != '\0') {
// check *str against chars
ptr = chars;
while ((*ptr != *str) && (*ptr != '\0')) {ptr++;}
// update
if (*ptr == '\0') { // ptr same as ptr on lines 53 and 56
break;
}
result++;
}
return result; // result same as result on line 62
}
int getAValue();
struct two_values {
signed short val1;
signed short val2;
};
void test04(two_values *vals)
{
signed short v = getAValue(); // should this match getAValue() on line 80?
if (v < vals->val1 + vals->val2) {
v = getAValue(); // should this match getAValue() on line 77?
}
}
void test05(int x, int y, void *p)
{
int v;
v = p != 0 ? x : y;
}
int regression_test00() {
int x = x = 10;
return x;
}
void test06(int x) {
x++;
x++; // x is matched but x++ is not matched?
}