mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Merge pull request #107 from rdmarsh2/rdmarsh/cpp/HashCons
C++: HashCons library
This commit is contained in:
20
change-notes/1.19/analysis-cpp.md
Normal file
20
change-notes/1.19/analysis-cpp.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Improvements to C/C++ analysis
|
||||
|
||||
## General improvements
|
||||
|
||||
## New queries
|
||||
|
||||
| **Query** | **Tags** | **Purpose** |
|
||||
|-----------------------------|-----------|--------------------------------------------------------------------|
|
||||
| *@name of query (Query ID)* | *Tags* |*Aim of the new query and whether it is enabled by default or not* |
|
||||
|
||||
## Changes to existing queries
|
||||
|
||||
| **Query** | **Expected impact** | **Change** |
|
||||
|----------------------------|------------------------|------------------------------------------------------------------|
|
||||
| *@name of query (Query ID)*| *Impact on results* | *How/why the query has changed* |
|
||||
|
||||
|
||||
## Changes to QL libraries
|
||||
|
||||
* Added a hash consing library for structural comparison of expressions.
|
||||
@@ -14,6 +14,7 @@ import semmle.code.cpp.dataflow.DataFlow2
|
||||
import semmle.code.cpp.dataflow.DataFlow3
|
||||
import semmle.code.cpp.dataflow.DataFlow4
|
||||
import semmle.code.cpp.dataflow.TaintTracking
|
||||
import semmle.code.cpp.valuenumbering.HashCons
|
||||
|
||||
from File f, string tag
|
||||
where none()
|
||||
|
||||
1095
cpp/ql/src/semmle/code/cpp/valuenumbering/HashCons.qll
Normal file
1095
cpp/ql/src/semmle/code/cpp/valuenumbering/HashCons.qll
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,131 @@
|
||||
| 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: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:55:5:55:7 | ptr | 55:c5-c7 56:c14-c16 56:c32-c34 56:c47-c49 59:c10-c12 |
|
||||
| test.cpp:59:9:59:12 | (int)... | 56:c13-c16 56:c31-c34 59:c9-c12 |
|
||||
| test.cpp:59:9:59:12 | * ... | 56:c13-c16 56:c31-c34 59:c9-c12 |
|
||||
| test.cpp:59:17:59:20 | 0 | 53:c18-c21 56:c39-c42 59:c17-c20 |
|
||||
| test.cpp:59:17:59:20 | (int)... | 53:c18-c21 56:c39-c42 59:c17-c20 |
|
||||
| test.cpp:62:5:62:10 | result | 62:c5-c10 65:c10-c15 |
|
||||
| test.cpp:79:11:79:14 | vals | 79:c11-c14 79:c24-c27 |
|
||||
| test.cpp:80:5:80:5 | v | 79:c7-c7 80:c5-c5 |
|
||||
| test.cpp:80:9:80:17 | call to getAValue | 77:c20-c28 80:c9-c17 |
|
||||
| test.cpp:80:9:80:19 | (signed short)... | 77:c20-c30 80:c9-c19 |
|
||||
| test.cpp:92:15:92:16 | 10 | 260:c21-c22 261:c21-c22 92:c15-c16 |
|
||||
| test.cpp:93:10:93:10 | x | 92:c11-c11 93:c10-c10 |
|
||||
| test.cpp:97:3:97:3 | x | 97:c3-c3 98:c3-c3 |
|
||||
| test.cpp:97:3:97:5 | ... ++ | 97:c3-c5 98:c3-c5 |
|
||||
| test.cpp:104:3:104:3 | x | 104:c3-c3 105:c3-c3 106:c3-c3 107:c3-c3 108:c3-c3 |
|
||||
| test.cpp:107:7:107:11 | ... + ... | 107:c7-c11 108:c7-c11 |
|
||||
| test.cpp:111:3:111:5 | str | 111:c3-c5 112:c3-c5 113:c3-c5 |
|
||||
| test.cpp:111:9:111:11 | 1 | 110:c15-c17 111:c9-c11 |
|
||||
| test.cpp:111:9:111:11 | (char *)... | 110:c15-c17 111:c9-c11 |
|
||||
| test.cpp:111:9:111:11 | array to pointer conversion | 110:c15-c17 111:c9-c11 |
|
||||
| test.cpp:112:9:112:11 | 2 | 112:c9-c11 113:c9-c11 |
|
||||
| test.cpp:112:9:112:11 | (char *)... | 112:c9-c11 113:c9-c11 |
|
||||
| test.cpp:112:9:112:11 | array to pointer conversion | 112:c9-c11 113:c9-c11 |
|
||||
| test.cpp:116:3:116:3 | y | 116:c3-c3 117:c3-c3 118:c3-c3 |
|
||||
| test.cpp:116:7:116:9 | 0.0 | 115:c13-c15 116:c7-c9 |
|
||||
| test.cpp:116:7:116:9 | (float)... | 115:c13-c15 116:c7-c9 |
|
||||
| test.cpp:117:7:117:9 | 0.5 | 117:c7-c9 118:c7-c9 |
|
||||
| test.cpp:117:7:117:9 | (float)... | 117:c7-c9 118:c7-c9 |
|
||||
| test.cpp:122:3:122:8 | call to test07 | 122:c3-c8 123:c3-c8 |
|
||||
| test.cpp:125:3:125:11 | call to my_strspn | 125:c3-c11 126:c3-c11 |
|
||||
| test.cpp:125:13:125:17 | array to pointer conversion | 125:c13-c17 126:c13-c17 129:c20-c24 |
|
||||
| test.cpp:125:13:125:17 | foo | 125:c13-c17 126:c13-c17 129:c20-c24 |
|
||||
| test.cpp:129:13:129:17 | array to pointer conversion | 125:c20-c24 126:c20-c24 129:c13-c17 |
|
||||
| test.cpp:129:13:129:17 | bar | 125:c20-c24 126:c20-c24 129:c13-c17 |
|
||||
| test.cpp:141:12:141:17 | call to getInt | 141:c12-c17 141:c29-c34 |
|
||||
| test.cpp:141:23:141:26 | this | 0:c0-c0 141:c23-c26 |
|
||||
| test.cpp:146:10:146:11 | ih | 146:c10-c11 146:c31-c32 |
|
||||
| test.cpp:146:13:146:25 | call to getDoubledInt | 146:c13-c25 146:c34-c46 |
|
||||
| test.cpp:150:3:150:3 | x | 150:c3-c3 150:c9-c9 151:c3-c3 151:c9-c9 152:c12-c12 |
|
||||
| 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:3:156:9 | 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 |
|
||||
| test.cpp:179:10:179:22 | (...) | 179:c10-c22 179:c10-c22 |
|
||||
| test.cpp:179:17:179:17 | y | 179:c17-c17 179:c17-c17 |
|
||||
| test.cpp:179:17:179:21 | ... + ... | 179:c17-c21 179:c17-c21 |
|
||||
| test.cpp:185:17:185:17 | y | 185:c17-c17 185:c17-c17 |
|
||||
| test.cpp:202:3:202:18 | sizeof(padded_t) | 202:c3-c18 204:c3-c18 |
|
||||
| test.cpp:205:24:205:34 | sizeof(int) | 205:c24-c34 245:c25-c35 246:c25-c35 |
|
||||
| test.cpp:206:3:206:21 | alignof(int_holder) | 206:c25-c43 206:c3-c21 |
|
||||
| test.cpp:209:3:209:18 | sizeof(<expr>) | 209:c22-c37 209:c3-c18 |
|
||||
| test.cpp:209:10:209:15 | holder | 209:c10-c15 209:c29-c34 |
|
||||
| test.cpp:209:10:209:18 | (...) | 209:c10-c18 209:c29-c37 |
|
||||
| test.cpp:209:17:209:17 | x | 209:c17-c17 209:c36-c36 |
|
||||
| test.cpp:210:10:210:10 | x | 208:c27-c27 210:c10-c10 211:c11-c11 211:c24-c24 |
|
||||
| test.cpp:210:10:210:11 | (...) | 210:c10-c11 211:c11-c12 211:c24-c25 |
|
||||
| test.cpp:211:3:211:12 | alignof(<expr>) | 211:c16-c25 211:c3-c12 |
|
||||
| test.cpp:239:3:239:12 | new | 239:c3-c12 240:c3-c12 |
|
||||
| test.cpp:245:16:245:36 | new[] | 245:c16-c36 246:c16-c36 |
|
||||
| test.cpp:248:3:248:28 | delete | 248:c3-c28 249:c3-c28 |
|
||||
| test.cpp:248:10:248:28 | call to operator new | 248:c10-c28 249:c10-c28 |
|
||||
| test.cpp:248:10:248:28 | new | 248:c10-c28 249:c10-c28 |
|
||||
| test.cpp:252:10:252:32 | call to operator new | 252:c10-c32 253:c12-c34 |
|
||||
| test.cpp:252:10:252:32 | new | 252:c10-c32 253:c12-c34 |
|
||||
| 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 |
|
||||
| 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 |
|
||||
| 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 |
|
||||
| test.cpp:254:11:254:14 | (void *)... | 250:c14-c17 254:c11-c14 |
|
||||
| test.cpp:254:11:254:14 | ptr2 | 250:c14-c17 254:c11-c14 |
|
||||
| test.cpp:255:11:255:14 | (void *)... | 248:c14-c17 249:c14-c17 252:c18-c21 253:c20-c23 255:c11-c14 |
|
||||
| test.cpp:255:11:255:14 | ptr1 | 248:c14-c17 249:c14-c17 252:c18-c21 253:c20-c23 255:c11-c14 |
|
||||
| test.cpp:257:3:257:19 | call to operator new | 257:c3-c19 258:c3-c19 |
|
||||
| test.cpp:257:3:257:19 | new | 257:c3-c19 258:c3-c19 |
|
||||
| test.cpp:260:3:260:23 | call to operator new[] | 260:c3-c23 261:c3-c23 |
|
||||
| test.cpp:260:3:260:23 | new[] | 260:c3-c23 261:c3-c23 |
|
||||
| test.cpp:263:3:263:32 | delete[] | 263:c3-c32 264:c3-c32 |
|
||||
| test.cpp:263:12:263:32 | new[] | 263:c12-c32 264:c12-c32 |
|
||||
| test.cpp:263:12:263:32 | {...} | 263:c12-c32 264:c12-c32 |
|
||||
| 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 |
|
||||
| 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 |
|
||||
| test.cpp:269:3:269:19 | new[] | 269:c3-c19 270:c3-c19 |
|
||||
| test.cpp:269:3:269:19 | {...} | 269:c3-c19 270:c3-c19 |
|
||||
| test.cpp:271:15:271:15 | 3 | 265:c28-c28 271:c15-c15 35:c16-c16 |
|
||||
| test.cpp:273:3:273:12 | new[] | 273:c3-c12 274:c3-c12 |
|
||||
| test.cpp:273:11:273:11 | x | 273:c11-c11 274:c11-c11 |
|
||||
| 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 |
|
||||
| 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 |
|
||||
| test.cpp:299:3:299:9 | throw ... | 299:c3-c9 300:c3-c9 |
|
||||
| test.cpp:301:3:301:9 | throw ... | 301:c3-c9 302:c3-c9 |
|
||||
| test.cpp:303:3:303:7 | re-throw exception | 303:c3-c7 304:c3-c7 |
|
||||
| test.cpp:308:3:308:3 | x | 308:c3-c3 309:c3-c3 310:c3-c3 311:c3-c3 |
|
||||
| test.cpp:308:3:308:6 | access to array | 308:c3-c6 309:c3-c6 |
|
||||
| test.cpp:308:5:308:5 | 0 | 308:c5-c5 309:c5-c5 312:c5-c5 44:c9-c9 51:c25-c25 88:c12-c12 |
|
||||
| test.cpp:310:3:310:6 | access to array | 310:c3-c6 311:c3-c6 |
|
||||
| test.cpp:312:3:312:3 | y | 312:c3-c3 313:c3-c3 |
|
||||
| test.cpp:320:3:320:11 | test_18_p | 320:c3-c11 321:c3-c11 |
|
||||
| test.cpp:320:3:320:13 | call to expression | 320:c3-c13 321:c3-c13 |
|
||||
| test.cpp:324:3:324:11 | test_19_p | 324:c3-c11 325:c3-c11 326:c3-c11 |
|
||||
| test.cpp:324:3:324:17 | call to expression | 324:c3-c17 325:c3-c17 |
|
||||
| test.cpp:324:13:324:13 | x | 324:c13-c13 325:c13-c13 326:c16-c16 |
|
||||
| test.cpp:326:13:326:13 | y | 324:c16-c16 325:c16-c16 326:c13-c13 |
|
||||
| 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 |
|
||||
| test.cpp:330:3:330:8 | ... == ... | 330:c3-c8 331:c3-c8 333:c3-c8 |
|
||||
| test.cpp:330:3:330:16 | ... ? ... : ... | 330:c3-c16 331:c3-c16 |
|
||||
| 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,0 +1,12 @@
|
||||
import cpp
|
||||
import semmle.code.cpp.valuenumbering.HashCons
|
||||
|
||||
from HashCons h
|
||||
where strictcount(h.getAnExpr()) > 1
|
||||
select
|
||||
h,
|
||||
strictconcat(Location loc
|
||||
| loc = h.getAnExpr().getLocation()
|
||||
| loc.getStartLine() +
|
||||
":c" + loc.getStartColumn() + "-c" + loc.getEndColumn()
|
||||
, " ")
|
||||
@@ -0,0 +1,8 @@
|
||||
import cpp
|
||||
import semmle.code.cpp.valuenumbering.HashCons
|
||||
|
||||
// Every expression should have exactly one HC.
|
||||
// So this query should have zero results.
|
||||
from Expr e
|
||||
where count(hashCons(e)) != 1
|
||||
select e, concat(HashCons h | h = hashCons(e) | h.getKind(), ", ")
|
||||
334
cpp/ql/test/library-tests/valuenumbering/HashCons/test.cpp
Normal file
334
cpp/ql/test/library-tests/valuenumbering/HashCons/test.cpp
Normal file
@@ -0,0 +1,334 @@
|
||||
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
|
||||
}
|
||||
|
||||
// literals
|
||||
void test07() {
|
||||
int x = 1;
|
||||
x = 1;
|
||||
x = 2;
|
||||
x = 2;
|
||||
x = 1 + 2;
|
||||
x = 1 + 2;
|
||||
|
||||
char *str = "1";
|
||||
str = "1";
|
||||
str = "2";
|
||||
str = "2";
|
||||
|
||||
float y = 0.0;
|
||||
y = 0.0;
|
||||
y = 0.5;
|
||||
y = 0.5;
|
||||
}
|
||||
|
||||
void test08() {
|
||||
test07();
|
||||
test07();
|
||||
|
||||
my_strspn("foo", "bar");
|
||||
my_strspn("foo", "bar");
|
||||
|
||||
|
||||
my_strspn("bar", "foo");
|
||||
}
|
||||
|
||||
class IntHolder {
|
||||
int myInt;
|
||||
|
||||
int getInt() {
|
||||
return myInt;
|
||||
}
|
||||
|
||||
public:
|
||||
int getDoubledInt() {
|
||||
return getInt() + this->getInt(); // getInt() and this->getInt() should be the same
|
||||
}
|
||||
};
|
||||
|
||||
int test09(IntHolder ih) {
|
||||
return ih.getDoubledInt() + ih.getDoubledInt();
|
||||
}
|
||||
|
||||
int test10(int x) {
|
||||
x++ + x++;
|
||||
x++ + x++; // same as above
|
||||
return ++x; // ++x is not the same as x++
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#define SQUARE(x) ((x) * (x))
|
||||
|
||||
int test13(int y) {
|
||||
return SQUARE(y + 1);
|
||||
}
|
||||
|
||||
#define SQUARE(x) x * x
|
||||
|
||||
int test14(int y) {
|
||||
return SQUARE(y);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
int x;
|
||||
char y;
|
||||
} padded_t;
|
||||
|
||||
typedef struct {
|
||||
int x;
|
||||
} int_holder;
|
||||
|
||||
typedef unsigned long size_t;
|
||||
|
||||
void *malloc(size_t size);
|
||||
|
||||
int test15(int x) {
|
||||
sizeof(padded_t);
|
||||
alignof(padded_t);
|
||||
sizeof(padded_t);
|
||||
sizeof(int_holder) + sizeof(int);
|
||||
alignof(int_holder) + alignof(int_holder);
|
||||
|
||||
int_holder holder = {x: x};
|
||||
sizeof(holder.x) + sizeof(holder.x);
|
||||
sizeof(x);
|
||||
alignof(x) + alignof(x);
|
||||
}
|
||||
|
||||
static void *operator new(size_t size, void *placement) {
|
||||
return placement;
|
||||
}
|
||||
|
||||
static void *operator new(size_t size, size_t alignment, void *placement) {
|
||||
return placement;
|
||||
}
|
||||
|
||||
static void *operator new(size_t size, size_t alignment) {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
static void *operator new[](size_t size, void *placement) {
|
||||
return placement;
|
||||
}
|
||||
|
||||
static void *operator new[](size_t size, size_t alignment, void *placement) {
|
||||
return placement;
|
||||
}
|
||||
|
||||
static void *operator new[](size_t size, size_t alignment) {
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void test16(int y, int z) {
|
||||
new int(1);
|
||||
new int(1);
|
||||
new int(2);
|
||||
|
||||
int x;
|
||||
|
||||
char *ptr1 = new char[sizeof(int)];
|
||||
char *ptr2 = new char[sizeof(int)];
|
||||
|
||||
delete new(ptr1) IntHolder;
|
||||
delete new(ptr1) IntHolder;
|
||||
delete new(ptr2) IntHolder;
|
||||
|
||||
delete new(32, ptr1) IntHolder;
|
||||
delete[] new(32, ptr1) IntHolder;
|
||||
new(32, ptr2) IntHolder;
|
||||
new(16, ptr1) IntHolder;
|
||||
|
||||
new(32) IntHolder;
|
||||
new(32) IntHolder;
|
||||
|
||||
new(32) IntHolder[10];
|
||||
new(32) IntHolder[10];
|
||||
|
||||
delete[] new(32) int[2] {1, 2};
|
||||
delete[] new(32) int[2] {1, 2};
|
||||
delete[] new(32) int[2] {3, 4};
|
||||
new(32) int[2] {1, 1};
|
||||
new(32) int[2] {2, 2};
|
||||
|
||||
new(32) int[2] {};
|
||||
new(32) int[2] {};
|
||||
new(32) int[3] {};
|
||||
|
||||
new int[x];
|
||||
new int[x];
|
||||
new int[z];
|
||||
}
|
||||
|
||||
typedef struct point{
|
||||
int x;
|
||||
int y;
|
||||
} point_t;
|
||||
|
||||
void test17() {
|
||||
point_t p1 = {
|
||||
1,
|
||||
2
|
||||
};
|
||||
point_t p2 = {
|
||||
1,
|
||||
2
|
||||
};
|
||||
point_t p3 = {
|
||||
2,
|
||||
1
|
||||
};
|
||||
}
|
||||
|
||||
void test18() {
|
||||
throw 1;
|
||||
throw 1;
|
||||
throw 2;
|
||||
throw 2;
|
||||
throw;
|
||||
throw;
|
||||
}
|
||||
|
||||
void test19(int *x, int *y) {
|
||||
x[0];
|
||||
x[0];
|
||||
x[1];
|
||||
x[1];
|
||||
y[0];
|
||||
y[1];
|
||||
}
|
||||
|
||||
void test20(int *x, int *y) {
|
||||
void (*test_18_p)() = &test18;
|
||||
void (*test_17_p)() = &test17;
|
||||
void (*test_19_p)(int *, int *) = &test19;
|
||||
test_18_p();
|
||||
test_18_p();
|
||||
test_17_p();
|
||||
|
||||
test_19_p(x, y);
|
||||
test_19_p(x, y);
|
||||
test_19_p(y, x);
|
||||
}
|
||||
|
||||
void test21(int x, int y) {
|
||||
x == y ? x : y;
|
||||
x == y ? x : y;
|
||||
y == x ? x : y;
|
||||
x == y ? y : x;
|
||||
}
|
||||
Reference in New Issue
Block a user