User forums > Using Code::Blocks

howto use clang-tidy in code::blocks

(1/3) > >>

blauzahn:
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?


--- Quote ---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.

--- End quote ---

see: 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


--- Code: ---$ 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

--- End code ---


Check, whether clang-tidy has been installed correctly:

--- Code: ---$ clang-tidy --version
--- End code ---

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


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):


--- Code: ------

## 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_'
...

--- End code ---

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:

--- Quote ---People will not have to find the rule. The rule will find you.
--- End quote ---
https://www.youtube.com/watch?v=fX2W3nNjJIo

enjoy

ollydbg:
This is a nice tool, but it looks like configure the tool is complex. :)

About the screen shot, I think the line

--- Code: ---*p = 0;
--- End code ---
is a bug.

In line 730, I don't see any issue:

--- Code: ---    if (lines.GetCount() == 0)
        return false;

--- End code ---

blauzahn:
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

--- Code: ---$ sudo apt-get install clang-tidy

--- End code ---

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:

--- Code: ---$compiler $file -config='' -header-filter='^[^/].*' -- $includes $options
--- End code ---

The file .clang-tidy helps getting started and shows, how to select a check.


--- Quote --- I think the line [...] is a bug.
--- End quote ---
See, how it helps to improve code. This may include cb as well;-)



--- Quote ---In line 730, I don't see any issue
--- End quote ---
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.

jalcazo:
Hi, I'm having problems with clang-tidy, my advanced options look like this:


--- Code: ---$compiler $file -header-filter='^[^/].*' -- $includes $options
--- End code ---

But it can't find SOME .h files, it gets MOST of them but SOME are missing:

jalcazo:
Book.cpp for instance looks like this currently:



Audio.cpp for instance does #include <Audio.h> with no problems.

What's going on?

Navigation

[0] Message Index

[#] Next page

Go to full version