Author Topic: howto use clang-tidy in code::blocks  (Read 970 times)

Offline blauzahn

  • Multiple posting newcomer
  • *
  • Posts: 79
howto use clang-tidy in code::blocks
« on: October 03, 2017, 02:01:25 pm »
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.


Installing clang and clang-tidy:

Here on ubuntu commandline, taken from

Code: [Select]
$ wget
$ sudo apt-key add llvm-snapshot.gpg.key
$ sudo apt-add-repository "deb 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:
Code: [Select]
$ clang-tidy --version
Should print a few lines with version info on stdout.

You can try bleeding edge clang-6.0 clang-tidy-6.0 as well.

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: [Select]

## 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

  - 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


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.


Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5055
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: howto use clang-tidy in code::blocks
« Reply #1 on: October 04, 2017, 05:52:23 am »
This is a nice tool, but it looks like configure the tool is complex. :)

About the screen shot, I think the line
Code: [Select]
*p = 0;is a bug.

In line 730, I don't see any issue:
Code: [Select]
    if (lines.GetCount() == 0)
        return false;
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline blauzahn

  • Multiple posting newcomer
  • *
  • Posts: 79
Re: howto use clang-tidy in code::blocks
« Reply #2 on: October 04, 2017, 07:48:49 am »
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: [Select]
$ 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:
Code: [Select]
$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.