This is a basic example of C-style cast detection using clang
's AST Matchers
in less than 60 lines of C++ code.
git clone [email protected]:thegameg/cast-matcher.git llvm/tools/clang/extra/cast-matcher
echo 'add_subdirectory(cast-matcher)' >> llvm/tools/clang/extra/CMakeLists.txt
Build using ninja cast-matcher
or make cast-matcher
.
is_cast.c
:
#include <stddef.h>
void foo(void)
{
int *i = NULL;
void *a = (void *)i;
}
$ cast-matcher is_cast.c
is_cast.c:6:13: error: explicit cast detected
void *a = (void *)i;
^~~~~~~~~
1 error generated.
Error while processing is_cast.c.