Oscar Forner Martinez bio photo

Oscar Forner Martinez

I am software engineer at VCA Technology. Learning Linux Kernel, passionate about GNU/Linux and OSS, enjoying algorithms and data structures and coding in C and C++.

Email Twitter Google+ LinkedIn Github

Clang is a compiler front end for the C, C++, Objective-C and Objective-C++ programming languages. It uses LLVM as its back end. There are also several awesome tools build on top of Clang and I am going to show the three of them I use the most.

As usual, I am working from an Arch Linux computer. Therefore, I can install Clang and the tools from the repository (clang, clang-tools-extra). For other distributions you can find the information in the documentation.

As always, all the code used in this post is available in this repo.

The videos are made with asciinema, that means you can copy from the video.

Clang-format

This tool is perfect to make sure that you are following a specific code style and it is fully configurable.

It can integrated in your editor or IDE. In my case, I have it integrated in my Vim configuration. You can see how it works in the following video:

Or it can be used from the command line. The original state of the code is: After formatting:

Clang-tidy

This tool is really useful to have it integrated with your version control system. I like to have it configurated to run as a pre-commit hook. In case it detects any problem the commit fails. Therefore, you only commit code that has been aproved by the tool. As I use CMake I have to add a definition to generate the json file needed from the tool. After that the code can be analyzed for all checks as follows: You can deal on your own with these problems, or let it fix them for you using the -fix flag.

Clang-modernize

Last but not least, this tool is made to update old code to C++11. This tool is not bullet proof, but it can make your job easier if you have to migrate the old code to newer standards. It has an option called -risk that can have three values: safe, reasonable or risky, by default it uses reasonable.

Old code: Modern code:

Conclusion

These three tools make your live easier as a developer. You will be sure you code is following the code style from your company/project, it will ensure that you are committing code that is using the new features from C++11 and it will help you migrate the old code to use the features from C++11. I strongly recommend to include them in your workflow, because it will boost your work performance.

NOTE: This post is based on **Clang version 3.7.1. In version 3.8, clang-tidy will include some checks from clang-modernize. So, it will enforce you to use features from C++11**.