I use that very helpful tool in code::blocks regularly and just want to share that knowledge.
Trick is, to use it as if it were a compiler and rely on the build-mechanism within code::blocks
for that.
These notes have been tried with svn 11182 under ubuntu 16.04. Apart from the apt install part,
the rest should work for other Linux distros as well.
What is it?clang-tidy is a clang-based C++ “linter” tool [...] for diagnosing and fixing typical programming errors,
like style violations, interface misuse, or bugs that can be deduced via static analysis.
see:
http://clang.llvm.org/extra/clang-tidy/index.htmlInstalling clang and clang-tidy:Here on ubuntu commandline, taken from
http://askubuntu.com/questions/787383/how-to-install-llvm-3-9#799998$ wget http://apt.llvm.org/llvm-snapshot.gpg.key
$ sudo apt-key add llvm-snapshot.gpg.key
$ sudo apt-add-repository "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial main"
$ sudo apt-get update
$ sudo apt-get install clang-5.0 clang-tidy-5.0
Check, whether clang-tidy has been installed correctly:
Should print a few lines with version info on stdout.
You can try bleeding edge clang-6.0 clang-tidy-6.0 as well.
http://clang.llvm.org/extra/ReleaseNotes.htmlAdd compiler in code::blocks:- open Settings|compiler|Selected compiler: LLVM Clang Compiler
- Copy|Please enter the new compiler's name: clang-tidy
- open Toolchain executables|c++ compiler: clang-tidy-5.0
- open Settings|compiler|clang-tidy|Other settings|Advanced options...|command line macro:
$compiler $file -config='' -header-filter='^[^/].*' -- $includes $options
Please change this only for clang-tidy, not for the other compiler.
Add target to your code::blocks project:- open your project workspace
- open Project|Properties...|Build targets
- select a Build target (e.g. Debug)| Duplicate
- Enter the duplicated build target's name: (e.g. Debug clang-tidy)
- select the newly duplicated Build target (e.g. Debug clang-tidy)
- open Build options...| selected compiler: clang-tidy
- select menu File | Save project
Create a file named .clang-tidy:Please put it into the directory where your code::blocks workspace file resides.
Here is a sample file to begin with (Here I uncommented (activated) the NullDereference check):
---
## disable all, then enable one check only:
Checks: '-*,clang-analyzer-core.NullDereference'
# Checks: '-*,modernize-use-nullptr'
# Checks: '-*,cppcoreguidelines-special-member-functions'
## disable all,then enable 2 checks:
# Checks: '-*,modernize-use-nullptr,readability-identifier-naming'
## enable all, then disable unwanted group and a single check:
# Checks: '*,-clang-analyzer-alpha*,-llvm-header-guard'
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
CheckOptions:
- key: modernize-use-nullptr.NullMacros
value: 'NULL'
- key: readability-identifier-naming.PrivateMemberCase
value: aNy_CasE
- key: readability-identifier-naming.MemberPrefix
value: 'm_'
...
Please feel free to adopt it to select the checks you want.
Execute clang-tidy from within code::blocks:- open your project
- select menu Build | Rebuild (Ctrl-F11)
Note: "compiling" takes longer than a regular compile - open View | Logs (F2) | Build messages
Note: ignore the messages about missing object files like: error: no such file or directory: 'obj/foo.o - select one of the blue compiler-warnings and by that jump to the file and line
(see attached screenshot) - fix it
Recommendation:When applied to an existing larger codebase, start with a single check.
Otherwise you might get an overwhelming number of warnings.
That way helps you to focus and attack one problem at a time.
Once you cleaned your code enough, you might settle on your favourite set of checks
against future regressions.
Bjarne Stroustrup @ CppCon 2017:
People will not have to find the rule. The rule will find you.
https://www.youtube.com/watch?v=fX2W3nNjJIoenjoy