|
|
<div align="justify">
|
|
|
|
|
|
Table of Contents
|
|
|
=================
|
|
|
|
|
|
[[_TOC_]]
|
|
|
|
|
|
# Introduction
|
|
|
|
|
|
*Clang-Tidy* is a tool developed and maintained by the *Clang/LLVM* community. The official documentation can be found at [http://clang.llvm.org/extra/clang-tidy/](http://clang.llvm.org/extra/clang-tidy/).
|
|
|
|
|
|
# Install
|
|
|
|
|
|
When running Linux, *clang-tidy* is usually easy to get via your distribution’s package manager. On Ubuntu Linux:
|
|
|
|
|
|
~~~sh
|
|
|
sudo apt-get install clang-tidy
|
|
|
~~~
|
|
|
|
|
|
Additionally you can download directly in the [project page](http://releases.llvm.org/download.html).
|
|
|
|
|
|
# How to use it
|
|
|
|
|
|
A typical invocation of the command-line tool looks like this:
|
|
|
|
|
|
~~~sh
|
|
|
clang-tidy test.cpp -- -Imy_project/include -DMY_DEFINES ...
|
|
|
~~~
|
|
|
|
|
|
Executing it like this, the tool will print a bunch of warnings and notes (if applicable), in exactly the same way Clang/GCC provide diagnostics, too.
|
|
|
|
|
|
The avalaible checkers avaible can be found with the following command:
|
|
|
|
|
|
~~~sh
|
|
|
clang-tidy --list-checks -checks='*' | grep "modernize"
|
|
|
modernize-avoid-bind
|
|
|
modernize-deprecated-headers
|
|
|
modernize-loop-convert
|
|
|
modernize-make-shared
|
|
|
modernize-make-unique
|
|
|
modernize-pass-by-value
|
|
|
modernize-raw-string-literal
|
|
|
modernize-redundant-void-arg
|
|
|
modernize-replace-auto-ptr
|
|
|
modernize-shrink-to-fit
|
|
|
modernize-use-auto
|
|
|
modernize-use-bool-literals
|
|
|
modernize-use-default
|
|
|
modernize-use-emplace
|
|
|
modernize-use-nullptr
|
|
|
modernize-use-override
|
|
|
modernize-use-using
|
|
|
~~~
|
|
|
|
|
|
To correct the code we can use the command `-fix` (not all the avalaible checkers have the `-fix` function). For example to add the missing overrides in the following code:
|
|
|
|
|
|
~~~c
|
|
|
struct Base {
|
|
|
virtual void reimplementMe(int a) {}
|
|
|
};
|
|
|
struct Derived : public Base {
|
|
|
virtual void reimplementMe(int a) {}
|
|
|
};
|
|
|
~~~
|
|
|
|
|
|
We use the next command:
|
|
|
|
|
|
~~~sh
|
|
|
clang-tidy -checks='modernize-use-override' -fix test.cpp -- -std=c++11
|
|
|
~~~
|
|
|
|
|
|
# Using to correct a whole project
|
|
|
|
|
|
The previous example will work just with a very simple example contained in one file. To correct the whole project we will need to create a `json` file containing all the file in the project. For that we add the following line to ou configure.sh:
|
|
|
|
|
|
~~~sh
|
|
|
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
|
|
|
~~~
|
|
|
|
|
|
This will create a file named `compile_commands.json` that we will use with the following python script from the [LLVM project](https://gitlab.inria.fr/felisce/extra/documentation/-/raw/master/Resources/Clang-tidy%20modernize/run-clang-tidy.py?inline=false)).
|
|
|
|
|
|
Once we have the script and the json file we can check and fix the whole project by the following way:
|
|
|
|
|
|
~~~sh
|
|
|
run-clang-tidy.py -header-filter='.*' -checks='-*,modernize-use-override' -fix
|
|
|
~~~
|
|
|
|
|
|
You can run simmultaneously all the possible modernize commands using the following [shell script]( https://gitlab.inria.fr/felisce/extra/documentation/-/raw/master/Resources/Clang-tidy%20modernize/modernize.sh?inline=false).
|
|
|
|
|
|
~~~sh
|
|
|
sh modernize.sh
|
|
|
~~~
|
|
|
|
|
|
# References
|
|
|
|
|
|
- [LLVM](http://releases.llvm.org/download.html)
|
|
|
- [Clang-Tidy](http://clang.llvm.org/extra/clang-tidy/)
|
|
|
- [Clang-Tidy, part 1: Modernize your source code using C++11/C++14](https://www.kdab.com/clang-tidy-part-1-modernize-source-code-using-c11c14/)
|
|
|
|
|
|
</div> |
|
|
\ No newline at end of file |