1. 安装clang-tidy和clang编译工具
sudo apt install clang-tidy clang
(ubuntu 16.04默认安装的是3.8版本)sudo apt install clang-tidy-6.0 clang-6.0 clang-tools-6.0
(ubuntu 16.04 apt最高可安装6.0版本)- 多版本可使用update-alternatives来管理
2. 简单使用
-
查看所有可静态分析内容:参考
clang-tidy -list-checks -checks="*"
,分为以下几大类:
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.
zircon- Checks related to Zircon kernel coding conventions. -
readability-identifier-naming
lower_case, UPPER_CASE, camelBack, CamelCase, camel_Snake_Back, Camel_Snake_Case, aNy_CasE.
-
modernize 具体内容:参考
modernize-avoid-bind: 使用lambda替换std::binding。
modernize-deprecated-headers: 将C标准库的头文件include替换成C++style,#include <assert.h> => #include 。
modernize-loop-convert: 使用for-range loop替换for(…;…;…😉, 并更新for语句的相关变量。
modernize-make-shared: 找出所有显式创建std::shared_ptr变量的表达式,并使用make_shared替换。
modernize-make-unique: 跟make-shared一样,使用std::make_unique替换所有std::unique_ptr显式创建表达式。
modernize-pass-by-value: 在构造函数中使用move语义
modernize-raw-string-literal: 用C++11的raw string literal(R"…")替换原来的string literal, 这样的好处就是不用再添加转义符\了。
modernize-redundant-void-arg: 去掉void函数参数。
modernize-replace-auto-ptr: 用std::unique_ptr替换std::shared_ptr, std::shared_ptr是不推荐使用的,即使在C++98。
modernize-shrink-to-fit: 在C++03中,如果我们想修改STL容器的capacity,只能通过copy & swap的方式,C++11提供了shink_to_fit的方法。
modernize-use-auto: 在变量定义的时候,使用auto代替显式的类型声明,这个在定义STL容器类的Iterator特别方便。
modernize-use-bool-literals: 找出所有隐式从int转成bool的literal, 使用true或者false代替。
modernize-use-default: 对于没有任何自定义行为(定义为{})的特殊的成员函数,构造函数,析构函数,移动/复制构造函数,用=default代替掉{}。
modernize-use-emplace: 使用STL容器中的emplace代替push_back。
modernize-use-equals-delete: 在C++98中,类设计为了实现禁止调用某些特殊的成员函数,通常把它们声明成private;在C++11中,只需要在声明中体检=delete,找出所有private的特殊成员函数,并将它们标记成=delete。
modernize-use-nullptr: 用nullptr代替NULL。
modernize-use-override: 对于子类改写父类的virtual方法,在方法后面添加override, 并删掉virtual前缀,即virtual void NewOveride() => void NewOverride() override {}。
modernize-use-using: 用using代替typedef, 如typedef int V => using V = int。 -
cmake 中的使用(cmake时加上
-DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang
)
#clang-tidy
if (NOT WIN32)
set(CLANG-TIDY-EXE "/usr/bin/clang-tidy-6.0")
endif()
if (EXISTS "${CLANG-TIDY-EXE}")
message(STATUS "use clang-tidy to check")
set(CMAKE_CXX_CLANG_TIDY "${CLANG-TIDY-EXE};-checks=-*,modernize-use-nullptr;-warnings-as-errors=*;-p=${CMAKE_BUILD_DIR};-export-fixes=clang-tidy-fixes.yml")
if(CLANG_TIDY_FIX)
message(STATUS "apply suggested fix" )
set(CMAKE_CXX_CLANG_TIDY "${CMAKE_CXX_CLANG_TIDY};-fix")
endif()
endif()