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.html (http://clang.llvm.org/extra/clang-tidy/index.html)
Installing clang and clang-tidy:
Here on ubuntu commandline, taken from
http://askubuntu.com/questions/787383/how-to-install-llvm-3-9#799998 (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.html (http://clang.llvm.org/extra/ReleaseNotes.html)
Add 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=fX2W3nNjJIo (https://www.youtube.com/watch?v=fX2W3nNjJIo)
enjoy
This is a nice tool, but it looks like configure the tool is complex. :)
About the screen shot, I think the line
is a bug.
In line 730, I don't see any issue:
if (lines.GetCount() == 0)
return false;
I made it deliberately detailed, so that even cb users with less experience than you can follow it.
The urls are there as a reference only. You can skip them.
Although recent clang-tidy got more checks, you can use the version a linux distro provides out of the box, like
$ sudo apt-get install clang-tidy
The core part is to add in cb a compiler and adjust the way it is called:
open Settings|compiler|clang-tidy|Other settings|Advanced options...|command line macro:
$compiler $file -config='' -header-filter='^[^/].*' -- $includes $options
The file .clang-tidy helps getting started and shows, how to select a check.
I think the line [...] is a bug.
See, how it helps to improve code. This may include cb as well;-)
In line 730, I don't see any issue
It is a note, no warning. Intention of the screenshot is to show, how it looks like and
that the apperearance and usage is like regular compiler warnings.
Hi, I'm having problems with clang-tidy, my advanced options look like this:
$compiler $file -header-filter='^[^/].*' -- $includes $options
But it can't find SOME .h files, it gets MOST of them but SOME are missing:
(https://i.ibb.co/RT3q72T/esb5.png)
Sadly that yields the same result.
Interestingly, when using:
(notice the single ' ) the results are different:
-------------- Build: Compilar amb CLANG-tidy STATIC ANALYSIS in Morgana (compiler: Clang-TIDY Static Analysis)---------------
clang-tidy.exe W:\Morgana\main.cpp -header-filter='^[^/].*' -- -IW:\Morgana\include -Wall -fexceptions -m64 -pedantic -w
clang-tidy.exe W:\Morgana\src\animations\AttributesAnimation.cpp -header-filter='^[^/].*' -- -IW:\Morgana\include -Wall -fexceptions -m64 -pedantic -w
clang-tidy.exe W:\Morgana\src\Audio.cpp -header-filter='^[^/].*' -- -IW:\Morgana\include -Wall -fexceptions -m64 -pedantic -w
clang-tidy.exe W:\Morgana\src\Companion.cpp -header-filter='^[^/].*' -- -IW:\Morgana\include -Wall -fexceptions -m64 -pedantic -w
clang-tidy.exe W:\Morgana\src\Engine.cpp -header-filter='^[^/].*' -- -IW:\Morgana\include -Wall -fexceptions -m64 -pedantic -w
clang-tidy.exe W:\Morgana\src\Error.cpp -header-filter='^[^/].*' -- -IW:\Morgana\include -Wall -fexceptions -m64 -pedantic -w
clang-tidy.exe W:\Morgana\src\Inventory.cpp -header-filter='^[^/].*' -- -IW:\Morgana\include -Wall -fexceptions -m64 -pedantic -w
clang-tidy.exe W:\Morgana\src\items\Book.cpp -header-filter='^[^/].*' -- -IW:\Morgana\include -Wall -fexceptions -m64 -pedantic -w
5 errors generated.
Error while processing W:\Morgana\src\items\Book.cpp.
Found compiler error(s).
W:\Morgana\src\items\Book.cpp:1:10: error: expected "FILENAME" or <FILENAME> [clang-diagnostic-error]
#include 'Book.h'
^
W:\Morgana\src\items\Book.cpp:4:1: error: use of undeclared identifier 'Book' [clang-diagnostic-error]
Book::Book(const string& name, const string& name_to_show){
^
W:\Morgana\src\items\Book.cpp:4:18: error: unknown type name 'string' [clang-diagnostic-error]
Book::Book(const string& name, const string& name_to_show){
^
W:\Morgana\src\items\Book.cpp:4:38: error: unknown type name 'string' [clang-diagnostic-error]
Book::Book(const string& name, const string& name_to_show){
^
W:\Morgana\src\items\Book.cpp:13:1: error: use of undeclared identifier 'Book' [clang-diagnostic-error]
Book::~Book(){} // don't delete or "undefined reference to vtable..."
^
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error generated.
Error while processing W:\Morgana\src\Engine.cpp.
Found compiler error(s).
W:\Morgana\src\Engine.cpp:3:10: error: 'Screen.h' file not found [clang-diagnostic-error]
#include <Screen.h>
^
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error generated.
Error while processing W:\Morgana\main.cpp.
Found compiler error(s).
W:\Morgana\main.cpp:2:10: error: 'Level1.h' file not found [clang-diagnostic-error]
#include <Level1.h>
^
1 error generated.
Error while processing W:\Morgana\src\animations\AttributesAnimation.cpp.
Found compiler error(s).
W:\Morgana\src\animations\AttributesAnimation.cpp:1:10: error: 'AttributesAnimation.h' file not found [clang-diagnostic-error]
#include <AttributesAnimation.h>
^
1 error generated.
Error while processing W:\Morgana\src\Inventory.cpp.
Found compiler error(s).
W:\Morgana\include\Inventory.h:6:10: error: 'Item.h' file not found [clang-diagnostic-error]
#include <Item.h>
^
Process terminated with status 1 (0 minute(s), 0 second(s))
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error generated.
Error while processing W:\Morgana\src\Audio.cpp.
Found compiler error(s).
W:\Morgana\include\SDL2/SDL_stdinc.h:683:12: error: use of undeclared identifier 'memcpy' [clang-diagnostic-error]
return SDL_memcpy(dst, src, dwords * 4);
^
W:\Morgana\include\SDL2/SDL_stdinc.h:653:20: note: expanded from macro 'SDL_memcpy'
#define SDL_memcpy memcpy
^
Process terminated with status 1 (0 minute(s), 0 second(s))
Process terminated with status 1 (0 minute(s), 0 second(s))
Process terminated with status 0 (0 minute(s), 3 second(s))
10 error(s), 0 warning(s) (0 minute(s), 3 second(s))
This has something to do with the regular expression:
I must dig into this and how it is formed. I don't understand it at all. Any resource explaining this particular type of regular expression?
Hello,
my command line looks like this:
$compiler $file -config='' -header-filter='^[^/].*' -- $includes $options
I am using Linux and this has worked so far for several years. The code compiles with MSVC as well but I do not use clang-tidy on windows.
Please do not use single quotes for header names. For separators of directory names I recommend / instead of \. This also works on Windows.
Thank you blauzahn for your answer and original post!
Using exactly your line from your last message the error for me is: (I use Code::Blocks on Windows 10)
File Line Message
YAML 1 error: not a mapping
Error: invalid configuration specified.
The log attached:
Hi again blauzahn. Thanks for your answer, your help, your time and your patience.
The project structure is like this:
(https://i.ibb.co/z4nqQHC/tidy1.png)
Is there where the file goes? It has no extension, right? I created a new empty file and added only to the build option "Clang Tidy".
The contents of the file are the ones you provided:
---
## 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_'
...
I tried it with or without "..." at the end (I don't understand if this is necessary).
Every time it tells the error YAML error: not a mapping, as seen in the attached log:
When you tell me "Then create a new one (see docs howto)." I don't understand what you are saying to me exactly. Please expand on this point, I don't know where is "docs howto".
About -Werror I looked it and it's not checked.
Clang-tidy version:
H:\morgana>clang-tidy --version
LLVM (http://llvm.org/):
LLVM version 11.0.0
Optimized build.
Default target: i686-pc-windows-msvc
Host CPU: haswell
Finally, removing the file .clang-tidy alltogether produces the same YAML errors seen in the log.txt attached.