Skip to content

Commit 1e3bf85

Browse files
committed
C++: select example exprs for HashCons portably
This makes two changes to how example exprs are selected. Example exprs are now ordered separately by each piece of the location, rather than by stringifying their location. Second, UnknownLocations are now ordered after locations with absolute paths, by using "~" in the lexicographic comparison of absolute paths. I think this works on both POSIX and Windows systems, but it's possible I'm missing a way to start an absolute path with a unicode character.
1 parent 3c9bd4f commit 1e3bf85

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ class HashCons extends HCBase {
277277
result =
278278
min(Expr e
279279
| this = hashCons(e)
280-
| e order by e.getLocation().toString())
280+
| e order by exampleLocationString(e.getLocation()), e.getLocation().getStartColumn(),
281+
e.getLocation().getEndLine(), e.getLocation().getEndColumn())
281282
}
282283

283284
/** Gets a textual representation of this element. */
@@ -291,6 +292,17 @@ class HashCons extends HCBase {
291292
}
292293
}
293294

295+
/**
296+
* Gets the absolute path of a known location or "~" for an unknown location. This ensures that
297+
* expressions with unknown locations are ordered after expressions with known locations when
298+
* selecting an example expression for a HashCons value.
299+
*/
300+
private string exampleLocationString(Location l) {
301+
if l instanceof UnknownLocation
302+
then result = "~"
303+
else result = l.getFile().getAbsolutePath()
304+
}
305+
294306
private predicate analyzableIntLiteral(Literal e) {
295307
strictcount (e.getValue().toInt()) = 1 and
296308
strictcount (e.getType().getUnspecifiedType()) = 1 and
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
| file://:0:0:0:0 | this | 0:c0-c0 141:c23-c26 |
21
| test.cpp:5:3:5:3 | x | 5:c3-c3 6:c3-c3 7:c7-c7 |
32
| test.cpp:5:7:5:8 | p0 | 5:c7-c8 6:c7-c8 |
43
| test.cpp:5:7:5:13 | ... + ... | 5:c7-c13 6:c7-c13 |
@@ -24,48 +23,48 @@
2423
| test.cpp:53:10:53:13 | (int)... | 53:c10-c13 56:c21-c24 |
2524
| test.cpp:53:10:53:13 | * ... | 53:c10-c13 56:c21-c24 |
2625
| 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:53:18:53:21 | (int)... | 53:c18-c21 56:c39-c42 59:c17-c20 |
2926
| test.cpp:55:5:55:7 | ptr | 55:c5-c7 56:c14-c16 56:c32-c34 56:c47-c49 59:c10-c12 |
30-
| test.cpp:56:13:56:16 | (int)... | 56:c13-c16 56:c31-c34 59:c9-c12 |
31-
| test.cpp:56:13:56:16 | * ... | 56:c13-c16 56:c31-c34 59:c9-c12 |
27+
| test.cpp:59:9:59:12 | (int)... | 56:c13-c16 56:c31-c34 59:c9-c12 |
28+
| test.cpp:59:9:59:12 | * ... | 56:c13-c16 56:c31-c34 59:c9-c12 |
29+
| test.cpp:59:17:59:20 | 0 | 53:c18-c21 56:c39-c42 59:c17-c20 |
30+
| test.cpp:59:17:59:20 | (int)... | 53:c18-c21 56:c39-c42 59:c17-c20 |
3231
| test.cpp:62:5:62:10 | result | 62:c5-c10 65:c10-c15 |
33-
| test.cpp:77:20:77:28 | call to getAValue | 77:c20-c28 80:c9-c17 |
34-
| test.cpp:77:20:77:30 | (signed short)... | 77:c20-c30 80:c9-c19 |
35-
| test.cpp:79:7:79:7 | v | 79:c7-c7 80:c5-c5 |
3632
| test.cpp:79:11:79:14 | vals | 79:c11-c14 79:c24-c27 |
37-
| test.cpp:92:11:92:11 | x | 92:c11-c11 93:c10-c10 |
33+
| test.cpp:80:5:80:5 | v | 79:c7-c7 80:c5-c5 |
34+
| test.cpp:80:9:80:17 | call to getAValue | 77:c20-c28 80:c9-c17 |
35+
| test.cpp:80:9:80:19 | (signed short)... | 77:c20-c30 80:c9-c19 |
36+
| test.cpp:92:15:92:16 | 10 | 260:c21-c22 261:c21-c22 92:c15-c16 |
37+
| test.cpp:93:10:93:10 | x | 92:c11-c11 93:c10-c10 |
3838
| test.cpp:97:3:97:3 | x | 97:c3-c3 98:c3-c3 |
3939
| test.cpp:97:3:97:5 | ... ++ | 97:c3-c5 98:c3-c5 |
40-
| 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 239:c11-c11 240:c11-c11 263:c28-c28 264:c28-c28 266:c19-c19 266:c22-c22 285:c5-c5 289:c5-c5 294:c5-c5 299:c9-c9 300:c9-c9 310:c5-c5 311:c5-c5 313:c5-c5 |
4140
| test.cpp:104:3:104:3 | x | 104:c3-c3 105:c3-c3 106:c3-c3 107:c3-c3 108:c3-c3 |
42-
| test.cpp:105:7:105:7 | 2 | 105:c7-c7 106:c7-c7 107:c11-c11 108:c11-c11 21:c16-c16 241:c11-c11 263:c24-c24 263:c31-c31 264:c24-c24 264:c31-c31 265:c24-c24 266:c15-c15 267:c15-c15 267:c19-c19 267:c22-c22 269:c15-c15 270:c15-c15 286:c5-c5 290:c5-c5 293:c5-c5 301:c9-c9 302:c9-c9 |
4341
| test.cpp:107:7:107:11 | ... + ... | 107:c7-c11 108:c7-c11 |
44-
| test.cpp:110:15:110:17 | 1 | 110:c15-c17 111:c9-c11 |
45-
| test.cpp:110:15:110:17 | (char *)... | 110:c15-c17 111:c9-c11 |
46-
| test.cpp:110:15:110:17 | array to pointer conversion | 110:c15-c17 111:c9-c11 |
4742
| test.cpp:111:3:111:5 | str | 111:c3-c5 112:c3-c5 113:c3-c5 |
43+
| test.cpp:111:9:111:11 | 1 | 110:c15-c17 111:c9-c11 |
44+
| test.cpp:111:9:111:11 | (char *)... | 110:c15-c17 111:c9-c11 |
45+
| test.cpp:111:9:111:11 | array to pointer conversion | 110:c15-c17 111:c9-c11 |
4846
| test.cpp:112:9:112:11 | 2 | 112:c9-c11 113:c9-c11 |
4947
| test.cpp:112:9:112:11 | (char *)... | 112:c9-c11 113:c9-c11 |
5048
| test.cpp:112:9:112:11 | array to pointer conversion | 112:c9-c11 113:c9-c11 |
51-
| test.cpp:115:13:115:15 | 0.0 | 115:c13-c15 116:c7-c9 |
52-
| test.cpp:115:13:115:15 | (float)... | 115:c13-c15 116:c7-c9 |
5349
| test.cpp:116:3:116:3 | y | 116:c3-c3 117:c3-c3 118:c3-c3 |
50+
| test.cpp:116:7:116:9 | 0.0 | 115:c13-c15 116:c7-c9 |
51+
| test.cpp:116:7:116:9 | (float)... | 115:c13-c15 116:c7-c9 |
5452
| test.cpp:117:7:117:9 | 0.5 | 117:c7-c9 118:c7-c9 |
5553
| test.cpp:117:7:117:9 | (float)... | 117:c7-c9 118:c7-c9 |
5654
| test.cpp:122:3:122:8 | call to test07 | 122:c3-c8 123:c3-c8 |
5755
| test.cpp:125:3:125:11 | call to my_strspn | 125:c3-c11 126:c3-c11 |
5856
| test.cpp:125:13:125:17 | array to pointer conversion | 125:c13-c17 126:c13-c17 129:c20-c24 |
5957
| test.cpp:125:13:125:17 | foo | 125:c13-c17 126:c13-c17 129:c20-c24 |
60-
| test.cpp:125:20:125:24 | array to pointer conversion | 125:c20-c24 126:c20-c24 129:c13-c17 |
61-
| test.cpp:125:20:125:24 | bar | 125:c20-c24 126:c20-c24 129:c13-c17 |
58+
| test.cpp:129:13:129:17 | array to pointer conversion | 125:c20-c24 126:c20-c24 129:c13-c17 |
59+
| test.cpp:129:13:129:17 | bar | 125:c20-c24 126:c20-c24 129:c13-c17 |
6260
| test.cpp:141:12:141:17 | call to getInt | 141:c12-c17 141:c29-c34 |
61+
| test.cpp:141:23:141:26 | this | 0:c0-c0 141:c23-c26 |
6362
| test.cpp:146:10:146:11 | ih | 146:c10-c11 146:c31-c32 |
6463
| test.cpp:146:13:146:25 | call to getDoubledInt | 146:c13-c25 146:c34-c46 |
6564
| test.cpp:150:3:150:3 | x | 150:c3-c3 150:c9-c9 151:c3-c3 151:c9-c9 152:c12-c12 |
6665
| test.cpp:150:3:150:5 | ... ++ | 150:c3-c5 150:c9-c11 151:c3-c5 151:c9-c11 152:c10-c12 |
6766
| test.cpp:150:3:150:11 | ... + ... | 150:c3-c11 151:c3-c11 |
68-
| test.cpp:156:14:156:20 | 0 | 156:c14-c20 156:c3-c9 157:c10-c16 |
67+
| test.cpp:156:3:156:9 | 0 | 156:c14-c20 156:c3-c9 157:c10-c16 |
6968
| test.cpp:171:3:171:6 | (int)... | 171:c3-c6 172:c3-c6 |
7069
| test.cpp:171:3:171:6 | e1x1 | 171:c3-c6 172:c3-c6 |
7170
| test.cpp:179:10:179:22 | (...) | 179:c10-c22 179:c10-c22 |
@@ -74,43 +73,44 @@
7473
| test.cpp:185:17:185:17 | y | 185:c17-c17 185:c17-c17 |
7574
| test.cpp:202:3:202:18 | sizeof(padded_t) | 202:c3-c18 204:c3-c18 |
7675
| test.cpp:205:24:205:34 | sizeof(int) | 205:c24-c34 245:c25-c35 246:c25-c35 |
77-
| test.cpp:206:25:206:43 | alignof(int_holder) | 206:c25-c43 206:c3-c21 |
78-
| test.cpp:208:27:208:27 | x | 208:c27-c27 210:c10-c10 211:c11-c11 211:c24-c24 |
76+
| test.cpp:206:3:206:21 | alignof(int_holder) | 206:c25-c43 206:c3-c21 |
77+
| test.cpp:209:3:209:18 | sizeof(<expr>) | 209:c22-c37 209:c3-c18 |
7978
| test.cpp:209:10:209:15 | holder | 209:c10-c15 209:c29-c34 |
8079
| test.cpp:209:10:209:18 | (...) | 209:c10-c18 209:c29-c37 |
8180
| test.cpp:209:17:209:17 | x | 209:c17-c17 209:c36-c36 |
82-
| test.cpp:209:22:209:37 | sizeof(<expr>) | 209:c22-c37 209:c3-c18 |
81+
| test.cpp:210:10:210:10 | x | 208:c27-c27 210:c10-c10 211:c11-c11 211:c24-c24 |
8382
| test.cpp:210:10:210:11 | (...) | 210:c10-c11 211:c11-c12 211:c24-c25 |
84-
| test.cpp:211:16:211:25 | alignof(<expr>) | 211:c16-c25 211:c3-c12 |
83+
| test.cpp:211:3:211:12 | alignof(<expr>) | 211:c16-c25 211:c3-c12 |
8584
| test.cpp:239:3:239:12 | new | 239:c3-c12 240:c3-c12 |
8685
| test.cpp:245:16:245:36 | new[] | 245:c16-c36 246:c16-c36 |
8786
| test.cpp:248:3:248:28 | delete | 248:c3-c28 249:c3-c28 |
88-
| test.cpp:248:10:248:28 | <error expr> | 248:c10-c28 249:c10-c28 250:c10-c28 252:c10-c32 253:c12-c34 254:c3-c25 255:c3-c25 257:c3-c19 258:c3-c19 260:c3-c23 261:c3-c23 |
8987
| test.cpp:248:10:248:28 | call to operator new | 248:c10-c28 249:c10-c28 |
9088
| test.cpp:248:10:248:28 | new | 248:c10-c28 249:c10-c28 |
91-
| test.cpp:248:14:248:17 | (void *)... | 248:c14-c17 249:c14-c17 252:c18-c21 253:c20-c23 255:c11-c14 |
92-
| test.cpp:248:14:248:17 | ptr1 | 248:c14-c17 249:c14-c17 252:c18-c21 253:c20-c23 255:c11-c14 |
93-
| test.cpp:250:14:250:17 | (void *)... | 250:c14-c17 254:c11-c14 |
94-
| test.cpp:250:14:250:17 | ptr2 | 250:c14-c17 254:c11-c14 |
9589
| test.cpp:252:10:252:32 | call to operator new | 252:c10-c32 253:c12-c34 |
9690
| test.cpp:252:10:252:32 | new | 252:c10-c32 253:c12-c34 |
97-
| test.cpp:252:14:252:15 | 32 | 252:c14-c15 253:c16-c17 254:c7-c8 257:c7-c8 258:c7-c8 260:c7-c8 261:c7-c8 263:c16-c17 264:c16-c17 265:c16-c17 266:c7-c8 267:c7-c8 269:c7-c8 270:c7-c8 271:c7-c8 |
98-
| test.cpp:252:14:252:15 | (size_t)... | 252:c14-c15 253:c16-c17 254:c7-c8 257:c7-c8 258:c7-c8 260:c7-c8 261:c7-c8 263:c16-c17 264:c16-c17 265:c16-c17 266:c7-c8 267:c7-c8 269:c7-c8 270:c7-c8 271:c7-c8 |
91+
| test.cpp:254:3:254:25 | <error expr> | 248:c10-c28 249:c10-c28 250:c10-c28 252:c10-c32 253:c12-c34 254:c3-c25 255:c3-c25 257:c3-c19 258:c3-c19 260:c3-c23 261:c3-c23 |
92+
| test.cpp:254:7:254:8 | 32 | 252:c14-c15 253:c16-c17 254:c7-c8 257:c7-c8 258:c7-c8 260:c7-c8 261:c7-c8 263:c16-c17 264:c16-c17 265:c16-c17 266:c7-c8 267:c7-c8 269:c7-c8 270:c7-c8 271:c7-c8 |
93+
| test.cpp:254:7:254:8 | (size_t)... | 252:c14-c15 253:c16-c17 254:c7-c8 257:c7-c8 258:c7-c8 260:c7-c8 261:c7-c8 263:c16-c17 264:c16-c17 265:c16-c17 266:c7-c8 267:c7-c8 269:c7-c8 270:c7-c8 271:c7-c8 |
94+
| test.cpp:254:11:254:14 | (void *)... | 250:c14-c17 254:c11-c14 |
95+
| test.cpp:254:11:254:14 | ptr2 | 250:c14-c17 254:c11-c14 |
96+
| test.cpp:255:11:255:14 | (void *)... | 248:c14-c17 249:c14-c17 252:c18-c21 253:c20-c23 255:c11-c14 |
97+
| test.cpp:255:11:255:14 | ptr1 | 248:c14-c17 249:c14-c17 252:c18-c21 253:c20-c23 255:c11-c14 |
9998
| test.cpp:257:3:257:19 | call to operator new | 257:c3-c19 258:c3-c19 |
10099
| test.cpp:257:3:257:19 | new | 257:c3-c19 258:c3-c19 |
101100
| test.cpp:260:3:260:23 | call to operator new[] | 260:c3-c23 261:c3-c23 |
102101
| test.cpp:260:3:260:23 | new[] | 260:c3-c23 261:c3-c23 |
103-
| test.cpp:260:21:260:22 | 10 | 260:c21-c22 261:c21-c22 92:c15-c16 |
104102
| test.cpp:263:3:263:32 | delete[] | 263:c3-c32 264:c3-c32 |
105-
| test.cpp:263:12:263:32 | <error expr> | 263:c12-c32 264:c12-c32 265:c12-c32 266:c3-c23 267:c3-c23 269:c3-c19 270:c3-c19 271:c3-c19 |
106-
| test.cpp:263:12:263:32 | call to operator new[] | 263:c12-c32 264:c12-c32 265:c12-c32 266:c3-c23 267:c3-c23 269:c3-c19 270:c3-c19 271:c3-c19 |
107103
| test.cpp:263:12:263:32 | new[] | 263:c12-c32 264:c12-c32 |
108104
| test.cpp:263:12:263:32 | {...} | 263:c12-c32 264:c12-c32 |
109-
| test.cpp:265:28:265:28 | 3 | 265:c28-c28 271:c15-c15 35:c16-c16 |
105+
| test.cpp:266:3:266:23 | <error expr> | 263:c12-c32 264:c12-c32 265:c12-c32 266:c3-c23 267:c3-c23 269:c3-c19 270:c3-c19 271:c3-c19 |
106+
| test.cpp:266:3:266:23 | call to operator new[] | 263:c12-c32 264:c12-c32 265:c12-c32 266:c3-c23 267:c3-c23 269:c3-c19 270:c3-c19 271:c3-c19 |
110107
| test.cpp:269:3:269:19 | new[] | 269:c3-c19 270:c3-c19 |
111108
| test.cpp:269:3:269:19 | {...} | 269:c3-c19 270:c3-c19 |
109+
| test.cpp:271:15:271:15 | 3 | 265:c28-c28 271:c15-c15 35:c16-c16 |
112110
| test.cpp:273:3:273:12 | new[] | 273:c3-c12 274:c3-c12 |
113111
| test.cpp:273:11:273:11 | x | 273:c11-c11 274:c11-c11 |
112+
| test.cpp:285:5:285:5 | 1 | 103:c10-c11 104:c7-c7 107:c7-c7 108:c7-c7 10:c16-c16 179:c21-c21 239:c11-c11 240:c11-c11 263:c28-c28 264:c28-c28 266:c19-c19 266:c22-c22 285:c5-c5 289:c5-c5 294:c5-c5 299:c9-c9 300:c9-c9 310:c5-c5 311:c5-c5 313:c5-c5 |
113+
| test.cpp:286:5:286:5 | 2 | 105:c7-c7 106:c7-c7 107:c11-c11 108:c11-c11 21:c16-c16 241:c11-c11 263:c24-c24 263:c31-c31 264:c24-c24 264:c31-c31 265:c24-c24 266:c15-c15 267:c15-c15 267:c19-c19 267:c22-c22 269:c15-c15 270:c15-c15 286:c5-c5 290:c5-c5 293:c5-c5 301:c9-c9 302:c9-c9 |
114114
| test.cpp:299:3:299:9 | throw ... | 299:c3-c9 300:c3-c9 |
115115
| test.cpp:301:3:301:9 | throw ... | 301:c3-c9 302:c3-c9 |
116116
| test.cpp:303:3:303:7 | re-throw exception | 303:c3-c7 304:c3-c7 |
@@ -124,8 +124,8 @@
124124
| test.cpp:324:3:324:11 | test_19_p | 324:c3-c11 325:c3-c11 326:c3-c11 |
125125
| test.cpp:324:3:324:17 | call to expression | 324:c3-c17 325:c3-c17 |
126126
| test.cpp:324:13:324:13 | x | 324:c13-c13 325:c13-c13 326:c16-c16 |
127-
| test.cpp:324:16:324:16 | y | 324:c16-c16 325:c16-c16 326:c13-c13 |
127+
| test.cpp:326:13:326:13 | y | 324:c16-c16 325:c16-c16 326:c13-c13 |
128+
| test.cpp:330:3:330:3 | x | 330:c12-c12 330:c3-c3 331:c12-c12 331:c3-c3 332:c12-c12 332:c8-c8 333:c16-c16 333:c3-c3 |
128129
| test.cpp:330:3:330:8 | ... == ... | 330:c3-c8 331:c3-c8 333:c3-c8 |
129130
| test.cpp:330:3:330:16 | ... ? ... : ... | 330:c3-c16 331:c3-c16 |
130-
| test.cpp:330:12:330:12 | x | 330:c12-c12 330:c3-c3 331:c12-c12 331:c3-c3 332:c12-c12 332:c8-c8 333:c16-c16 333:c3-c3 |
131-
| test.cpp:330:16:330:16 | y | 330:c16-c16 330:c8-c8 331:c16-c16 331:c8-c8 332:c16-c16 332:c3-c3 333:c12-c12 333:c8-c8 |
131+
| test.cpp:332:3:332:3 | y | 330:c16-c16 330:c8-c8 331:c16-c16 331:c8-c8 332:c16-c16 332:c3-c3 333:c12-c12 333:c8-c8 |

0 commit comments

Comments
 (0)