2019-06-01 21:08:41 +00:00
|
|
|
|
= Статический анализ кода
|
|
|
|
|
:category: Программирование
|
|
|
|
|
:tags: программирование, отладка, cppcheck, iwyu, clang-tidy, cpplint, clazy,
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
== Общее описание
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
Ниже приведены утилиты для проверки кода на C++ и примеры их настройки и
|
|
|
|
|
использования совместно с https://cmake.org/[CMake].
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
=== http://clang.llvm.org/extra/clang-tidy/[clang-tidy]
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
|
|
|
|
Установка:
|
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,sh]
|
|
|
|
|
----
|
2020-04-08 17:09:58 +00:00
|
|
|
|
sudo apt-get install clang-tidy-10
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
|
|
|
|
Использование:
|
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,sh]
|
|
|
|
|
----
|
2020-04-08 17:09:58 +00:00
|
|
|
|
cmake "-DCMAKE_CXX_CLANG_TIDY=/usr/bin/clang-tidy-10" path/to/source
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
В каталоге проекта нужно создать файл `.clang-tidy` в формате YAML со
|
|
|
|
|
списком выполняемых проверок. Например:
|
2019-04-24 13:57:04 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,yaml]
|
|
|
|
|
----
|
2019-04-24 14:17:12 +00:00
|
|
|
|
---
|
|
|
|
|
Checks: '-*,
|
2020-04-08 17:09:58 +00:00
|
|
|
|
bugprone-*,
|
|
|
|
|
clang-analyzer-*,
|
|
|
|
|
cppcoreguidelines-*,
|
|
|
|
|
google-*,
|
|
|
|
|
llvm-*,
|
|
|
|
|
misc-*,
|
|
|
|
|
modernize-*,
|
|
|
|
|
readability-*,
|
|
|
|
|
performance-*,
|
|
|
|
|
portability-*,
|
|
|
|
|
-cppcoreguidelines-owning-memory,
|
|
|
|
|
-cppcoreguidelines-avoid-magic-numbers,
|
|
|
|
|
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
|
|
|
|
|
-readability-magic-numbers,
|
|
|
|
|
-readability-else-after-return,
|
|
|
|
|
-modernize-use-trailing-return-type,
|
|
|
|
|
-modernize-avoid-c-arrays,
|
|
|
|
|
-performance-no-automatic-move,
|
|
|
|
|
'
|
2019-04-24 14:17:12 +00:00
|
|
|
|
CheckOptions:
|
|
|
|
|
- key: readability-identifier-naming.ClassCase
|
|
|
|
|
value: CamelCase
|
|
|
|
|
- key: readability-identifier-naming.ClassMemberCase
|
|
|
|
|
value: camelBack
|
|
|
|
|
...
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
Пример файла `.clang-tidy`, в котором перечислены все правила для
|
|
|
|
|
проверки именования идентификаторов приведён
|
|
|
|
|
https://git.246060.ru/f1x1t/clang-tidy-readability-identifier-naming[здесь].
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
=== CppCheck
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
|
|
|
|
Установка:
|
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,sh]
|
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
sudo apt-get install cppcheck
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
|
|
|
|
Использование:
|
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,sh]
|
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
cmake "-DCMAKE_CXX_CPPCHECK=/usr/bin/cppcheck;--std=c++11" path/to/source
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
This will run /usr/bin/cppcheck;–std=c++11″
|
|
|
|
|
–source=/path/to/source/file.cxx on each c++ file in the project being
|
|
|
|
|
built.
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
=== CppLint
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
|
|
|
|
Установка:
|
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,sh]
|
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
sudo apt-get install python3-cpplint
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
|
|
|
|
Использование:
|
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,sh]
|
|
|
|
|
----
|
2019-04-24 13:57:04 +00:00
|
|
|
|
cmake "-DCMAKE_CXX_CPPLINT=/usr/bin/cpplint;--linelength=79" path/to/source
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 13:57:04 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,sh]
|
|
|
|
|
----
|
2019-04-24 13:57:04 +00:00
|
|
|
|
make -j24 2>&1 1>/dev/null | ../cpplint2tasks.pl > 222.tasks
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
This will run /usr/local/bin/cpplint –linelength=79 on each c++ file in
|
|
|
|
|
the project being built.
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
=== IWYU
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
|
|
|
|
Установка:
|
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,sh]
|
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
sudo apt-get install iwyu
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
|
|
|
|
Использование:
|
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,sh]
|
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
cmake "-DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=/usr/bin/iwyu;--transitive_includes_only" ..
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
This will run /usr/bin/iwyu –transitive_includes_only on each c++ file
|
|
|
|
|
in the project being built.
|
2019-04-24 13:57:04 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
=== LWYU
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,sh]
|
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
cmake -DCMAKE_LINK_WHAT_YOU_USE=TRUE ..
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
=== Clazy
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
|
|
|
|
Установка:
|
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,sh]
|
|
|
|
|
----
|
2020-04-08 17:09:58 +00:00
|
|
|
|
sudo apt-get install clazy clang-9
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|
|
|
|
|
Использование:
|
|
|
|
|
|
2019-06-01 21:08:41 +00:00
|
|
|
|
[source,sh]
|
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
CLAZY_CHECKS=level2 cmake -DCMAKE_CXX_COMPILER=clazy ..
|
2020-04-08 17:09:58 +00:00
|
|
|
|
CLANGXX=clang++-9 make
|
2019-06-01 21:08:41 +00:00
|
|
|
|
----
|
2019-04-24 05:20:13 +00:00
|
|
|
|
|