c++静态代码检查

可以保证在提交代码的时候的规范

cpplint

这个只做格式的规范检查
Google风格

--counting

输出结果统计,有三个选项, 默认选项是 total:

  • total :只输出错误总数
  • toplevel :输出各顶层分类的错误统计,比如说 ‘build’, ‘whitespace’
  • detailed :输出各子分类的错误统计,比如说 ‘build/class’, ‘whitespace/braces’

--filter

过滤器,+是包含,-是不包含
cpplint --filter=可以输出支持的所有选项

--filter=-whitespace,+whitespace/braces
--filter=whitespace,runtime/printf,+runtime/printf_format
--filter=-,+build/include_what_you_use

--linelength

一行代码的长度限制。默认值为 80 个字符,超 80 个字符后,会提示错误。对于现代的大屏幕来说,我感觉 80 个字符太小了,设置为 120 比较合理。

$ cpplint --linelength=120 bucket.cpp 
bucket.cpp:0:  No copyright message found.  You should have a line: "Copyright [year] <Copyright Owner>"  [legal/copyright] [5]
bucket.cpp:1:  Include the directory when naming header files  [build/include_subdir] [4]
bucket.cpp:931:  Small and focused functions are preferred: Bucket::Test() has 750 non-comment lines (error triggered by exceeding 500 lines).  [readability/fn_size] [1]
Done processing bucket.cpp
Total errors found: 3

git 提交前自动检查

使用git pre-commit hooks

clang tidy

posted @ 2024-04-15 12:51  feipeng8848  阅读(46)  评论(0编辑  收藏  举报