Skip to content

Commit 2627afb

Browse files
committed
C++: Post-processing query for inline test expectations
1 parent 7a81911 commit 2627afb

File tree

5 files changed

+59
-35
lines changed

5 files changed

+59
-35
lines changed

cpp/ql/test/TestUtilities/InlineExpectationsTest.qll

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,5 @@
55

66
import cpp as C
77
private import codeql.util.test.InlineExpectationsTest
8-
9-
private module Impl implements InlineExpectationsTestSig {
10-
private newtype TExpectationComment = MkExpectationComment(C::CppStyleComment c)
11-
12-
/**
13-
* A class representing a line comment in the CPP style.
14-
* Unlike the `CppStyleComment` class, however, the string returned by `getContents` does _not_
15-
* include the preceding comment marker (`//`).
16-
*/
17-
class ExpectationComment extends TExpectationComment {
18-
C::CppStyleComment comment;
19-
20-
ExpectationComment() { this = MkExpectationComment(comment) }
21-
22-
/** Returns the contents of the given comment, _without_ the preceding comment marker (`//`). */
23-
string getContents() { result = comment.getContents().suffix(2) }
24-
25-
/** Gets a textual representation of this element. */
26-
string toString() { result = comment.toString() }
27-
28-
/** Gets the location of this comment. */
29-
Location getLocation() { result = comment.getLocation() }
30-
}
31-
32-
class Location = C::Location;
33-
}
34-
8+
private import internal.InlineExpectationsTestImpl
359
import Make<Impl>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @kind test-postprocess
3+
*/
4+
5+
private import cpp
6+
private import codeql.util.test.InlineExpectationsTest as T
7+
private import internal.InlineExpectationsTestImpl
8+
import T::TestPostProcessing
9+
import T::TestPostProcessing::Make<Impl, Input>
10+
11+
private module Input implements T::TestPostProcessing::InputSig<Impl> {
12+
string getRelativeUrl(Location location) {
13+
exists(File f, int startline, int startcolumn, int endline, int endcolumn |
14+
location.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
15+
f = location.getFile()
16+
|
17+
result =
18+
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
19+
)
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import cpp as C
2+
private import codeql.util.test.InlineExpectationsTest
3+
4+
module Impl implements InlineExpectationsTestSig {
5+
private newtype TExpectationComment = MkExpectationComment(C::CppStyleComment c)
6+
7+
/**
8+
* A class representing a line comment in the CPP style.
9+
* Unlike the `CppStyleComment` class, however, the string returned by `getContents` does _not_
10+
* include the preceding comment marker (`//`).
11+
*/
12+
class ExpectationComment extends TExpectationComment {
13+
C::CppStyleComment comment;
14+
15+
ExpectationComment() { this = MkExpectationComment(comment) }
16+
17+
/** Returns the contents of the given comment, _without_ the preceding comment marker (`//`). */
18+
string getContents() { result = comment.getContents().suffix(2) }
19+
20+
/** Gets a textual representation of this element. */
21+
string toString() { result = comment.toString() }
22+
23+
/** Gets the location of this comment. */
24+
Location getLocation() { result = comment.getLocation() }
25+
}
26+
27+
class Location = C::Location;
28+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Critical/SizeCheck.ql
1+
query: Critical/SizeCheck.ql
2+
postprocess: TestUtilities/InlineExpectationsTestQuery.ql

cpp/ql/test/query-tests/Critical/SizeCheck/test.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ void free(void *ptr);
1313

1414
void bad0(void) {
1515

16-
float *fptr = malloc(3); // BAD -- Too small
17-
double *dptr = malloc(5); // BAD -- Too small
16+
float *fptr = malloc(3); // $ BAD -- Too small
17+
double *dptr = malloc(5); // $ BAD -- Too small
1818
free(fptr);
1919
free(dptr);
2020
}
@@ -29,8 +29,8 @@ void good0(void) {
2929

3030
void bad1(void) {
3131

32-
float *fptr = malloc(sizeof(short)); // BAD -- Too small
33-
double *dptr = malloc(sizeof(float)); // BAD -- Too small
32+
float *fptr = malloc(sizeof(short)); // $ BAD -- Too small
33+
double *dptr = malloc(sizeof(float)); // $ BAD -- Too small
3434
free(fptr);
3535
free(dptr);
3636
}
@@ -56,7 +56,7 @@ typedef union _myUnion
5656

5757
void test_union() {
5858
MyUnion *a = malloc(sizeof(MyUnion)); // GOOD
59-
MyUnion *b = malloc(sizeof(MyStruct)); // BAD (too small)
59+
MyUnion *b = malloc(sizeof(MyStruct)); // $ BAD (too small)
6060
}
6161

6262
// --- custom allocators ---
@@ -66,6 +66,6 @@ void *MyMalloc2(size_t size);
6666

6767
void customAllocatorTests()
6868
{
69-
float *fptr1 = MyMalloc1(3); // BAD (too small) [NOT DETECTED]
70-
float *fptr2 = MyMalloc2(3); // BAD (too small) [NOT DETECTED]
69+
float *fptr1 = MyMalloc1(3); // $ MISSING: BAD (too small)
70+
float *fptr2 = MyMalloc2(3); // $ MISSING: BAD (too small)
7171
}

0 commit comments

Comments
 (0)