diff --git a/_posts/2015-10-17-cppcheck-and-clang-format.html b/_posts/2015-10-17-cppcheck-and-clang-format.html
new file mode 100644
index 0000000..0b81ed0
--- /dev/null
+++ b/_posts/2015-10-17-cppcheck-and-clang-format.html
@@ -0,0 +1,161 @@
+---
+category: en
+type: paper
+hastr: true
+layout: paper
+tags: awesome-widgets, development, c++, cmake
+title: Add cppcheck and clang-format for a cmake project
+short: cppcheck-and-clang-format
+description: A small How-To which describes how to add automatic code style checking and static analyser to a project on C++
which uses cmake
as a build system.
+---
+
The project has the following structure:
+ +{% highlight bash %} +sources/ +|- CMakeLists.txt +|- 3rdparty/ +|- first_component/ +|- second_component/ +{% endhighlight %} + +3rdparty is a directory which contains additional libraries and which should be excluded from checking (PROJECT_TRDPARTY_DIR
cmake variable is used to indicate path to this directory). Also let's assume that we have additional files (e.g. *.qml
) in addition to common source files (*.cpp
, *.h
).
In addition the described below commands may be inserted to pre-commit hook; it allows us to troll colleagues which will be able to commit nothing till they read CONTRIBUTING.md
.
As far as there are no good (out-of-box) static analysers in open source we will use it. Knowledgeable people say that cppcheck in case of good configuration it is better than the any alternative, but its configuration is the same that the new project creation. cppcheck
shows obvious errors and recommend to fix them.
Here it is:
+ +{% highlight bash %} +cppcheck --enable=warning,performance,portability,information,missingInclude --std=c++11 --library=qt.cfg --template="[{severity}][{id}] {message} {callstack} (On {file}:{line})" --verbose --quiet path/to/source/files/or/directory +{% endhighlight %} + +--enable
says which notifications should be enabled. I've disabled style
(we will use clang-format
to do it), unusedFunction
which shows false-positive for some methods.--std
says which standard should be used.--library=qt.cfg
a configuration file, which describes how to check files. The developers recommend to read the following manual. I've used the ready template from /usr/share/cppcheck/cfg/
.--template
is notification template.---verbose --quiet
are two "conflicting" options. The first one enables more verbose messages, the second one disables progress reports.cppcheck.cmake
file in the project root:
cppcheck
may work with directories recursive, but I need to skip qml-files checking in my example, because this cppcheck
will segfault on some of them. To do it source files search is used followed by the ejection of unnecessary files.
Include to the project (CMakeLists.txt
)...
...and run:
+ +{% highlight bash %} +cmake +make cppcheck +{% endhighlight %} + +Then edit files to avoid warnings in the future.
+ +-I dir
option-i path/to/file/or/directory
option.clang-format is used to automatic code style checking and correction. astyle, which has a very modest capabilities, and uncrustify, which on the contrary has too many options, should be mentioned from analogues.
+ +(Unfortunately it cound not work with directories recursive.)
+ +-i
enables files auto replace (otherwise the result will be printed to stdout).-style
is a style preset selection or from file (file
), see below.clang-format.cmake
file in the project root:
There is the same method to get source files list as for cppcheck
, because clang-format
doesn't support recursive directory search.
Include to the project (CMakeLists.txt
)...
...and run:
+ +{% highlight bash %} +cmake +make clangformat +{% endhighlight %} + +No other actions required.
+ +.clang-format
. To enable it you need set -style=file
option, file should be placed to the any parent directory of each source file (e.g., to the project root). Also you may send required options to the command line directly, e.g. -style="{BasedOnStyle: llvm, IndentWidth: 8}"
C++
, который использует в качестве системы сборки cmake
.
+---
+Наш проект имеет следующую структуру:
+ +{% highlight bash %} +sources/ +|- CMakeLists.txt +|- 3rdparty/ +|- first_component/ +|- second_component/ +{% endhighlight %} + +3rdparty - директория с различными дополнительными библиотеками, которую надо исключить из проверок (в дальнейшем соответствует переменной cmake PROJECT_TRDPARTY_DIR
). Дополнительно допустим, что у нас, помимо обычных файлов исходного кода (*.cpp
, *.h
) есть еще какие-либо (например, *.qml
).
Дополнительно используемые ниже команды можно вставить в pre-commit hook и невозбранно тролить коллег по ынтырпрайзу, не давая им закоммитить ничего, пока они не научатся читать CONTRIBUTING.md
.
Коль скоро нормальных (из коробки) статических анализаторов не завезли в open source будем использовать то, что имеется. Знатоки говорят, что cppcheck при должной конфигурации будет лучше, чем любой аналог, но конфигурация его для достаточно большого проекта похожа больше на написание нового проекта. Суть добавления cppheck к проекту сводится к указанию очевидных недоработок в коде и тыканью в лужу них.
Тут все, казалось бы, очень просто:
+ +{% highlight bash %} +cppcheck --enable=warning,performance,portability,information,missingInclude --std=c++11 --library=qt.cfg --template="[{severity}][{id}] {message} {callstack} (On {file}:{line})" --verbose --quiet path/to/source/files/or/directory +{% endhighlight %} + +--enable
говорит о том, какие уведомления надо включить. Я выключил style
(для этого ниже мы заведем clang-format
), unusedFunction
- выдает false-positive для некоторых мест.--std
говорит об используемом стандарте.--library=qt.cfg
некий файл настроек, который говорит о том, что и как надо обрабатывать. Добрые разработчики предлагаю почитать на эту тему мануал. В данном случае я использовал шаблон из /usr/share/cppcheck/cfg/
.--template
- шаблон строки уведомления.---verbose --quiet
две противоречащие друг другу опции. Первая включает более информативные сообщения, вторая выключает отчет о прогрессе.Файл cppcheck.cmake
в корне проекта:
cppcheck
умеет рекурсивно директории проверять, однако, на моем примере, мне нужно было пропустить проверку qml-файлов, потому что open source проект, в лучших традициях, сегфолтился на некоторых из них - именно для этого используется поиск исходных файлов с дальнейшим выбрасыванием из них файлов, которые не должны проверяться.
Включаем в проект (CMakeLists.txt
)...
...и запускаем:
+ +{% highlight bash %} +cmake +make cppcheck +{% endhighlight %} + +Дальше руками вносим необходимые исправления.
+ +-I dir
.-i path/to/file/or/directory
.clang-format предназначен для автоматического подгона стиля под желаемый или требуемый. Среди аналогов стоит выделить astyle, который имеет очень скромные возможности, и uncrustify, который, наоборот, имеет слишком много опций.
+ +(К сожалению, он не умеет в рекурсивный обход директории.)
+ +-i
включает автозамену файлов (в противном случае, результат будет печататься в stdout).-style
выбор определенного стиля либо из предустановленных, либо из файла (file
), см. ниже.Файл clang-format.cmake
в корне проекта:
Аналогичных способ поиска исходных файлов, как и для cppcheck
, поскольку clang-format
не умеет в рекурсию.
Включаем в проект (CMakeLists.txt
)...
...и запускаем:
+ +{% highlight bash %} +cmake +make clangformat +{% endhighlight %} + +Никаких дополнительных действий не требуется.
+ +.clang-format
. Для включения его необходимо передать опцию -style=file
, файл должен находиться в одной из родительских директории для каждого файла (например, в корне проекта). Также, можно передать нужные опции прямо в командной строке, например -style="{BasedOnStyle: llvm, IndentWidth: 8}"
.