教程;
https://hokein.github.io/clang-tools-tutorial/
要用clang-tidy首先要在电脑上安装clang-tools
Linux Ubuntu系统
sudo apt install clang-tools
装好后就可以看到clang-tidy以及其它相关工具了。
clang-tidy 官方文档
接下来我们要使用clang-tidy-8 --help
,注意最后面
Configuration files:
clang-tidy attempts to read configuration for each source file from a
.clang-tidy file located in the closest parent directory of the source
file. If any configuration options have a corresponding command-line
option, command-line option takes precedence. The effective
configuration can be inspected using -dump-config:
$ clang-tidy -dump-config
---
Checks: '-*,some-check'
WarningsAsErrors: ''
HeaderFilterRegex: ''
FormatStyle: none
User: user
CheckOptions:
- key: some-check.SomeOption
value: 'some value'
...
就是说你可以通过建立一个文件.clang-tidy
来减少命令行中-checks= option的命令。
在你需要检查的文件夹下建立文件.clang-tidy
在.clang-tidy中
{
Checks: "-*,bugprone-*,cert-*,clang-analyzer-*,cppcoreguidelines-avoid-goto,cppcoreguidelines-c-copy-assignment-signature,cppcoreguidelines-interfaces-global-init,cppcoreguidelines-narrowing-conversions,cppcoreguidelines-no-malloc,cppcoreguidelines-pro-bounds-constant-array-index,cppcoreguidelines-pro-type-const-cast,cppcoreguidelines-pro-type-cstyle-cast,cppcoreguidelines-pro-type-member-init,cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-type-static-cast-downcast,cppcoreguidelines-pro-type-union-access,cppcoreguidelines-slicing,cppcoreguidelines-special-member-functions,google-build-*,google-default-arguments,google-explicit-constructor,google-global-names-in-headers,google-objc-*,google-readability-casting,google-readability-function-size,google-readability-namespace-comments,google-readability-todo,google-runtime-operator,hicpp-avoid-goto,hicpp-deprecated-headers,hicpp-exception-baseclass,hicpp-explicit-conversions,hicpp-function-size,hicpp-invalid-access-moved,hicpp-member-init,hicpp-move-const-arg,hicpp-multiway-paths-covered,hicpp-named-parameter,hicpp-new-delete-operators,hicpp-no-assembler,hicpp-no-malloc,hicpp-noexcept-move,hicpp-special-member-functions,hicpp-static-assert,hicpp-undelegated-constructor,hicpp-use-*,misc-*,modernize-*,-modernize-use-trailing-return-type,performance-*,readability-avoid-const-params-in-decls,readability-container-size-empty,readability-delete-null-pointer,readability-deleted-default,readability-else-after-return,readability-function-size,readability-identifier-naming,readability-inconsistent-declaration-parameter-name,readability-misleading-indentation,readability-misplaced-array-index,readability-named-parameter,readability-non-const-parameter,readability-redundant-*,readability-simplify-*,readability-static-*,readability-string-compare,readability-uniqueptr-delete-release,readability-rary-objects"
}
这些就是检测规则
然后执行:
clang-tidy-8 -p ${PWD} -dump-config
然后运行clang-tidy
clant-tidy-8 --quite -p ./ src.cpp
这里注意-p 参数,在help里是这样说的
-p <build-path> is used to read a compile command database.
For example, it can be a CMake build directory in which a file named
compile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
CMake option to get this output). When no build path is specified,
a search for compile_commands.json will be attempted through all
parent paths of the first input file . See:
http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html for an
example of setting up Clang Tooling on a source tree.
很多童鞋遇到一个问题:
rror while trying to load a compilation database:
Could not auto-detect compilation database from directory "./"
No compilation database found in /builds/crystal/crystal-media/ or any parent directory
就是因为上述原因引起的,所以我们通过-p来指定compile_commands.json的位置,官方文档说明
z这里还有一个问题,如果你使用的是qmake,那么如何使用qmake来clang-tidy呢?
问题解决入口
howtouseclangtidyinQt
具体检查规则:
1、readability-identifier-naming (规则2.1.1)
这条规则是用来检查名命名规则的。在这条总的规则之下,还有关于具体要检查项的key和value。比如:
这里ClassCase说明检查的是类的命名,CamelCase说明要求的风格是大驼峰命名法。方舟里涉及到命名规则,目前有CamelCase(大驼峰命名法)、camelBack(小驼峰命名法)和lower_case(小写命名法)。涉及到的检查项主要有ClassCase(类)、StructCase(结构体)、TypedefCase(Typedef)、EnumCase(枚举)、EnumConstantCase(枚举常量)、UnionCase(联合)、NamespaceCase(命名空间)、FunctionCase(函数)、VariableCase(变量)、ConstantCase(常量)。
2、readability-function-size
这条规则是用来检查函数的大小的。总规则之下,也包含了两条具体的检查项。
检查函数内的语句不超过50条。(建议8.1.1)
检查函数的参数不超过5个。(建议8.3.3)
3、readability-braces-around-statements
语句必须在大括号之内。这里主要是针对条件判断之后进行执行的语句,比如if-else,哪怕是只有一条语句,也应该在括号之内。(规则3.6.1,规则3.7.1)
4、readability-magic-numbers
魔鬼数字检查。(建议9.1.3)
5、misc-unused-parameters
未使用函数参数检查。
6、modernize-use-nullptr
检查使用nullptr,而不是NULL或0。(规则10.1.3)
7、modernize-replace-auto-ptr
检查禁止使用auto_ptr。(规则9.5.2)
8、modernize-use-noexcept
检查不使用异常机制。(规则11.4.1)
9、modernize-use-override
检查重写虚函数时使用override关键字。(规则10.1.1)
10、performance-move-const-arg
检查禁止使用std::move操作const对象。(规则10.1.4)
11、cppcoreguidelines-pro-type-cstyle-cast
不使用C风格转换检查。(规则9.3.1)
12、cppcoreguidelines-pro-type-reinterpret-cast
不使用reinterpret_cast 检查。(建议9.3.2)
13、cppcoreguidelines-pro-type-const-cast
不使用const_cast检查。(建议9.3.3) 作者:小乖他爹-知乎 https://www.bilibili.com/read/cv5153605 出处:bilibili
具体脚本如下:
VERSION=debug
cd ./${CI_PROJECT_NAME}_${VERSION}
DIR_BUILD=${PWD}
echo PWD:${PWD}
ls
echo "${PROMPT}================= Check with Clang-tidy ======================"
chk_report="${DIR_BUILD}/clang-tidy-check.txt"
rm -f $chk_report
touch $chk_report
echo "${PROMPT}Report filename: $chk_report"
echo "${PROMPT}Current directory: ${PWD}"
echo "${PROMPT}Clang-tidy configuration:"
clang-tidy-8 -p ${DIR_BUILD} -dump-config
fifo="/tmp/$$.fifo"
mkfifo $fifo
exec 6<>$fifo
thread_num=8
count=0;
while [[ $count -lt $thread_num ]]; do
echo >&6
count=$((count + 1 ))
done
fail_flag_file="./quality-failed.txt"
file_list=$(find -name '*.cpp' )
echo -e ${file_list}
for src_file in $file_list; do
read -u6
{
if [[ $(basename $src_file) != moc_* && $(basename $src_file) != test_* && $(basename $src_file) != qrc_* && $(basename $src_file) != AES* ]]; then
time_start=$(date +%s)
echo "${PROMPT}Analyzing: $src_file"
eval "clang-tidy-8 --quiet -p ./ $src_file >>$chk_report"
[ $? -ne 0 ] && echo "FAIL" > $fail_flag_file
time_end=$(date +%s)
duration=$[${time_end}-${time_start}]
if [[ ${print_job_duration} == "on" ]]; then
echo ${PROMPT}"Task duration time: ${duration}"
fi
if [[ ${duration} -gt ${task_duration_limit} ]]; then
echo ${PROMPT}"Warning: This task takes time more than ${task_duration_limit} seconds."
fi
fi
echo >&6
}&
done
wait
exec 6>&-
rm $fifo
if [ -f $fail_flag_file ];then
CHECK_PASS=NO
else
CHECK_PASS=YES
fi
[ $(cat $chk_report | wc -l) -gt 0 ] && CHECK_PASS=NO
echo "${PROMPT}=================== Clang-tidy Report ========================"
cat $chk_report
echo "${PROMPT}=============================================================="
if [ "#$CHECK_PASS" = "#NO" ]; then
exit 1;
echo ${PROMPT}Cod Quality check failed.
fi
exit 0
clang-tidy静态语义检查,安装、使用、检查项注解
我们先来看一下clang与LLVM之间的区别与联系
LLVM与Clang的概述及关系
深入浅出让你理解什么是LLVM
还有一个clang-format
.clang-format
BasedOnStyle: LLVM
Cpp11BracedListStyle: true
Language: Cpp
Standard: Cpp14
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)