clang-tool is a simple and powerful project template for clang-based tools using libtooling[1]. It helps getting started to write standalone tools useful for refactoring, static code analysis, auto-completion etc.
Install the necessary llvm and clang headers and libraries for you system an run:
git clone https://github.com/firolino/clang-tool
cd clang-tool
mkdir build
cd build
cmake ..
make
A simple example is included in src/transformers/functioncalltransformer.cc
that rewriters all function calls from functionName()
to fn_functionName()
.
bin/clang-tool ../examples/simple.cc -- -std=c++11
Another example can be found in finder/integervariablefinder.cc
. Uncomment the intFinder
in consumer.cc
to print all integer variables.
FunctionCallTransformer fntransformer(context, rewriter);
fntransformer.start();
fntransformer.print(llvm::outs());
IntegerVariableFinder intFinder(context);
intFinder.start();
You can even specifiy more than one source file when calling clang-tool.
bin/clang-tool s1.cc s2.cc -- -std=c++11
More matchers for writing own finders and transformers are available under [2].
There are multiple ways to provide your projects' compiler arguments to a clang tool:
-
Direct
As can be seen in the example above, compiler arguments can be placed directly after
--
. -
Compilation Database
If your project is CMake-based use
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
in your cmake call. It will generate acompile_commands.json
in your build directory containing your compiler arguments etc. With the json file you don't need to provide--
anymore. clang-tool will automatically use your json file. -
Makefiles
If you have an ordinary Makefile based project, use rizsottos awesome tool from https://github.com/rizsotto/Bear and run
bear make
on a cleanded project directory. It will generate acompile_commands.json
for you!
Inlude PPCallbacks