Skip to content

Commit 3c6a9c0

Browse files
author
Robert Marsh
committed
C++: first tests for HashCons
1 parent 2d7109b commit 3c6a9c0

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
| test.cpp:5:3:5:3 | x | 5:c3-c3 6:c3-c3 7:c7-c7 |
2+
| test.cpp:5:7:5:8 | p0 | 5:c7-c8 6:c7-c8 |
3+
| test.cpp:5:7:5:13 | ... + ... | 5:c7-c13 6:c7-c13 |
4+
| test.cpp:5:12:5:13 | p1 | 5:c12-c13 6:c12-c13 |
5+
| test.cpp:16:3:16:3 | x | 16:c3-c3 17:c3-c3 18:c7-c7 |
6+
| test.cpp:16:7:16:8 | p0 | 16:c7-c8 17:c7-c8 |
7+
| test.cpp:16:7:16:13 | ... + ... | 16:c7-c13 17:c7-c13 |
8+
| test.cpp:16:7:16:24 | ... + ... | 16:c7-c24 17:c7-c24 |
9+
| test.cpp:16:12:16:13 | p1 | 16:c12-c13 17:c12-c13 |
10+
| test.cpp:16:17:16:24 | global01 | 16:c17-c24 17:c17-c24 |
11+
| test.cpp:29:3:29:3 | x | 29:c3-c3 31:c3-c3 32:c7-c7 |
12+
| test.cpp:29:7:29:8 | p0 | 29:c7-c8 31:c7-c8 |
13+
| test.cpp:29:7:29:13 | ... + ... | 29:c7-c13 31:c7-c13 |
14+
| test.cpp:29:7:29:24 | ... + ... | 29:c7-c24 31:c7-c24 |
15+
| test.cpp:29:12:29:13 | p1 | 29:c12-c13 31:c12-c13 |
16+
| test.cpp:29:17:29:24 | global02 | 29:c17-c24 31:c17-c24 |
17+
| test.cpp:43:3:43:3 | x | 43:c3-c3 45:c3-c3 46:c7-c7 |
18+
| test.cpp:43:7:43:8 | p0 | 43:c7-c8 45:c7-c8 |
19+
| test.cpp:43:7:43:13 | ... + ... | 43:c7-c13 45:c7-c13 |
20+
| test.cpp:43:7:43:24 | ... + ... | 43:c7-c24 45:c7-c24 |
21+
| test.cpp:43:12:43:13 | p1 | 43:c12-c13 45:c12-c13 |
22+
| test.cpp:43:17:43:24 | global03 | 43:c17-c24 45:c17-c24 |
23+
| 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 |
24+
| test.cpp:53:10:53:13 | (int)... | 53:c10-c13 56:c21-c24 |
25+
| test.cpp:53:10:53:13 | * ... | 53:c10-c13 56:c21-c24 |
26+
| test.cpp:53:11:53:13 | str | 53:c11-c13 56:c22-c24 |
27+
| test.cpp:53:18:53:21 | 0 | 53:c18-c21 56:c39-c42 59:c17-c20 |
28+
| test.cpp:55:5:55:7 | ptr | 55:c5-c7 56:c14-c16 56:c32-c34 56:c47-c49 59:c10-c12 |
29+
| test.cpp:56:13:56:16 | (int)... | 56:c13-c16 56:c31-c34 59:c9-c12 |
30+
| test.cpp:56:13:56:16 | * ... | 56:c13-c16 56:c31-c34 59:c9-c12 |
31+
| test.cpp:62:5:62:10 | result | 62:c5-c10 65:c10-c15 |
32+
| test.cpp:79:7:79:7 | v | 79:c7-c7 80:c5-c5 |
33+
| test.cpp:79:11:79:14 | vals | 79:c11-c14 79:c24-c27 |
34+
| test.cpp:92:11:92:11 | x | 92:c11-c11 93:c10-c10 |
35+
| test.cpp:97:3:97:3 | x | 97:c3-c3 98:c3-c3 |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import cpp
2+
import semmle.code.cpp.valuenumbering.HashCons
3+
4+
from HC h
5+
where strictcount(h.getAnExpr()) > 1
6+
select
7+
h,
8+
strictconcat(Location loc
9+
| loc = h.getAnExpr().getLocation()
10+
| loc.getStartLine() +
11+
":c" + loc.getStartColumn() + "-c" + loc.getEndColumn()
12+
, " ")

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

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import cpp
2+
import semmle.code.cpp.valuenumbering.HashCons
3+
4+
// Every expression should have exactly one GVN.
5+
// So this query should have zero results.
6+
from Expr e
7+
where count(hashCons(e)) != 1
8+
select e, concat(HC h | h = hashCons(e) | h.getKind(), ", ")
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
int test00(int p0, int p1) {
2+
int x, y;
3+
unsigned char b;
4+
5+
x = p0 + p1;
6+
x = p0 + p1; // Same value as previous line. Should the assignment also be matched?
7+
y = x;
8+
}
9+
10+
int global01 = 1;
11+
12+
int test01(int p0, int p1) {
13+
int x, y;
14+
unsigned char b;
15+
16+
x = p0 + p1 + global01;
17+
x = p0 + p1 + global01; // Same structure as previous line.
18+
y = x; // x is the same as x above
19+
}
20+
21+
int global02 = 2;
22+
23+
void change_global02(); // Just a declaration
24+
25+
int test02(int p0, int p1) {
26+
int x, y;
27+
unsigned char b;
28+
29+
x = p0 + p1 + global02;
30+
change_global02();
31+
x = p0 + p1 + global02; // same HashCons as above
32+
y = x;
33+
}
34+
35+
int global03 = 3;
36+
37+
void change_global03(); // Just a declaration
38+
39+
int test03(int p0, int p1, int* p2) {
40+
int x, y;
41+
unsigned char b;
42+
43+
x = p0 + p1 + global03;
44+
*p2 = 0;
45+
x = p0 + p1 + global03; // same HashCons as 43
46+
y = x;
47+
}
48+
49+
unsigned int my_strspn(const char *str, const char *chars) {
50+
const char *ptr;
51+
unsigned int result = 0;
52+
53+
while (*str != '\0') {
54+
// check *str against chars
55+
ptr = chars;
56+
while ((*ptr != *str) && (*ptr != '\0')) {ptr++;}
57+
58+
// update
59+
if (*ptr == '\0') { // ptr same as ptr on lines 53 and 56
60+
break;
61+
}
62+
result++;
63+
}
64+
65+
return result; // result same as result on line 62
66+
}
67+
68+
int getAValue();
69+
70+
struct two_values {
71+
signed short val1;
72+
signed short val2;
73+
};
74+
75+
void test04(two_values *vals)
76+
{
77+
signed short v = getAValue(); // should this match getAValue() on line 80?
78+
79+
if (v < vals->val1 + vals->val2) {
80+
v = getAValue(); // should this match getAValue() on line 77?
81+
}
82+
}
83+
84+
void test05(int x, int y, void *p)
85+
{
86+
int v;
87+
88+
v = p != 0 ? x : y;
89+
}
90+
91+
int regression_test00() {
92+
int x = x = 10;
93+
return x;
94+
}
95+
96+
void test06(int x) {
97+
x++;
98+
x++; // x is matched but x++ is not matched?
99+
}
100+

0 commit comments

Comments
 (0)