mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
C++: Add examples.
This commit is contained in:
@@ -10,7 +10,15 @@ We are keen to have unit tests for all of our QL code.
|
||||
|
||||
Every query in `cpp/ql/src` (outside of `cpp/ql/src/experimental`) should have a test in the corresponding subdirectory of `cpp/ql/test/query-tests`. At a minimum, each query test shall contain one case that should be detected by the query, and one related case that should not.
|
||||
|
||||
TODO: example?
|
||||
For example a simple test for the "Memory is never freed" (`cpp/memory-never-freed`) query might contain the following cases:
|
||||
```
|
||||
int *array1, *array2;
|
||||
|
||||
array1 = (int *)malloc(sizeof(int) * 100); // BAD: never freed
|
||||
|
||||
array2 = (int *)malloc(sizeof(int) * 100); // GOOD
|
||||
free(array2);
|
||||
```
|
||||
|
||||
Features of the QL libraries in `cpp/ql/src` should also have test coverage, in `cpp/ql/test/library-tests`.
|
||||
|
||||
@@ -20,14 +28,25 @@ The contents of `cpp/ql/test` should be original - nothing should be copied from
|
||||
- ISO/IEC Programming languages - C (all versions)
|
||||
- ISO/IEC Programming languages - C++ (all versions)
|
||||
|
||||
TODO: example
|
||||
|
||||
In addition, C/C++ and QL code may be copied from other queries and tests in this repository.
|
||||
|
||||
For example the test above for the "Memory is never freed" (`cpp/memory-never-freed`) query requires the following declarations, taken from Programming languages <20> C (November 2018):
|
||||
```
|
||||
void *malloc(size_t size);
|
||||
void free(void *ptr);
|
||||
```
|
||||
We also need to write our own definition of `size_t`. Any unsigned integral type will do for our purposes:
|
||||
```
|
||||
typedef unsigned int size_t;
|
||||
```
|
||||
|
||||
## Including files
|
||||
|
||||
Standard and third party library header files should not be included in tests by means of `#include` or similar mechanisms. This is because the tests should be independent of platform and library versions installed on the running machine. Standard library declarations may be inserted directly where necessary (see the rules in the section above), but it is generally better to avoid using the standard library at all when possible.
|
||||
|
||||
`#include` may be used to include files from the same directory within `cpp/ql/test`.
|
||||
|
||||
TODO: example
|
||||
`#include` may be used to include files from the same directory within `cpp/ql/test`. For example the test for "Include header files only" (`cpp/include-non-header`) includes other files in the test directory:
|
||||
```
|
||||
#include "test.H"
|
||||
#include "test.xpm"
|
||||
#include "test2.c"
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user