clang-tidy使用

介绍

使用时,用-checks=选项来选择要运行的check,该选型用逗号分离每个check,如果在具体的check前面添加-号表示不使用该check。例如

1
$ clang-tidy test.cpp -checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus*

该命令表示禁止所有默认的chekcs(-*), 使用clang-analyzer-*除了clang-analyzer-cplusplus*以外的所有check

Name prefix Description
android- Checks related to Android.
boost- Checks related to Boost library.
bugprone- Checks that target bugprone code constructs.
cert- Checks related to CERT Secure Coding Guidelines.
cppcoreguidelines- Checks related to C++ Core Guidelines.
clang-analyzer- Clang Static Analyzer checks.
fuchsia- Checks related to Fuchsia coding conventions.
google- Checks related to Google coding conventions.
hicpp- Checks related to High Integrity C++ Coding Standard.
llvm- Checks related to the LLVM coding conventions.
misc- Checks that we didn’t have a better category for.
modernize- Checks that advocate usage of modern (currently “modern” means “C++11”) language constructs.
mpi- Checks related to MPI (Message Passing Interface).
objc- Checks related to Objective-C coding conventions.
performance- Checks that target performance-related issues.
portability- Checks that target portability-related issues that don’t relate to any particular coding style.
readability- Checks that target readability-related issues that don’t relate to any particular coding style.

安装

ubuntu下使用以下命令下载安装

1
$ sudo apt install clang-tidy-5.0

cmake中使用

1
2
3
4
5
6
7
#clang-tidy
if (NOT WIN32)
set(CLANG-TIDY-PATH "/usr/bin/clang-tidy-5.0")
endif()
if (EXISTS "${CLANG-TIDY-PATH}")
set(CMAKE_CXX_CLANG_TIDY "${CLANG-TIDY-PATH}" "-checks=*")
endif()