I have a project that I recently split up into headers + sources, and now clang-tidy wont "actually" check my test files that include these headers. Below is an example of the situation:
File Structure:
src/
main.c
a.h
a.c
test/
test.c
Code:
// src/main.c
#include "src/a.h"
int main(void) {
a();
return 0;
}
// src/a.h
void a(void);
// src/a.c
#include <stdlib.h>
#include "src/a.h"
void a(void) {
malloc(1); // obvious memory leak
}
// test/test.c
#include "src/a.h"
int main(void) {
// do test stuff
a();
return 0;
}
Compile src: cc src/*.c -I. -o main
Compile test: cc src/a.c test/test.c -I. -o test
Running does nothing, but the code does compile.
Then, I run the code through clang-tidy
:
clang-tidy src/* -- -I.
This works:
1 warning generated.
1 warning generated.
1 warning generated.
/src/a.c:8:1: warning: Potential memory leak [clang-analyzer-unix.Malloc]
}
^
/src/a.c:7:2: note: Memory is allocated
malloc(1);
^
/src/a.c:8:1: note: Potential memory leak
}
^
But if I do:
clang-tidy test/test.c -- -I.
Nothing is outputed (ive tried to add the header-filter
flag as well).
I pressume that the headers are being included, and the test code has no knowledge of the source. So, how can I make sure my tests are actually being checked if this is the case? These headers and sources are in my project, so it would only make sense that clang-tidy would be able to pick up on this?
Edit:
I can do the following, and it seems to only partially fix the problem:
// test/test.c
#include "src/a.h"
#include "src/a.c" //added this line
int main(void) {
// do test stuff
a();
return 0;
}
Now, clang-tidy picks up on the actual .c
file, and thus finds the issue. But, now I cant compile my code. There is supposed to be a __clang_analyzer__
define that gets set when the code is being analyzed, but it seems to just ignore it:
#ifdef __clang_analyzer__
#include "src/a.c" // doesnt include file, thus the malloc() call is never detected
#endif