代碼風格審查工具Cpplint
它山之石哥蔚,可以攻玉。 --《詩經(jīng)·小雅·鶴鳴》
1宇驾,簡介
Cpplint是一個Python腳本课舍,作為一款開源免費的代碼靜態(tài)檢測工具筝尾,Google也使用它作為自己的C++代碼檢測工具办桨,也就是說损姜,只要你想代碼遵從Google C++代碼規(guī)范殊霞,那么Cpplint將會提供很好的代碼靜態(tài)檢測支持绷蹲。
cpplint
Cpplint.py支持的文件格式包括.cc比规、.h、.cpp灾常、.cu岗憋、.cuh仔戈。
Cpplint只是一個代碼風格檢測工具,其并不對代碼邏輯晋修、語法錯誤等進行檢查。
2,安裝
Cpplint在Window下可以如下幾種方式使用
2.1運行命令行檢測
使用命令即可對代碼文件進行檢測忠怖。
Python cpplint.py <file>
2.2 集成到VS中使用
在vs中使用的配置
這樣使用比較方便凡泣,支持錯誤雙擊跳轉(zhuǎn)鞋拟。
集成到VS:打開VS2015—》工具—》外部工具—》添加工具
寫入如下代碼:
Title:Cpplint.py
Command:E:\CpplintToPython\Python27\python.exe
Arguments:”E:\Cpplint\cpplint.py\cpplint.py” –output=vs7 $(ItemPath)
Initial directory:$(ItemDir)
Check Use Output window
2.3 使用python安裝版本
也可以使用python安裝cpplint模塊贺纲,
pip install cpplint
或者
easy_install cpplint
最新版本v1.4.4 支持python2 python3版本。
注意:直接只用python源碼版本或者安裝版本的話福铅,需要判斷其是否支持python3
3嘁圈,命令行的詳細使用
cpplint.py [--verbose=#]
[--output=vs7]
[--filter=-x,+y,...]
[--counting=total|toplevel|detailed]
[--root=subdir]
[--linelength=digits]
[--headers=x,y,...]
[--quiet]
<file> [file] ...
verbose指定輸出錯誤級別
對于發(fā)現(xiàn)的每個問題检柬,cpplint都會給出一個位于區(qū)間[1, 5]之間的置信度評分,分數(shù)越高就代表問題越肯定肛根,可以通過verbose選項控制輸出哪些級別,如下臼氨,置信度評分為1芭届、2的將不會再輸出
cpplint.py --verbose=3 test.cpp
cpplint的輸出格式
[--output=vs7]:cpplint的輸出格式有emacs和vs7兩種, 默認是emacs,vs7是Visual Studio輸出的兼容格式
指定輸出錯誤類型
[--filter=-x,+y,...]:指定輸出錯誤類型持隧,-表示不輸出树叽,+表示輸出(錯誤類型可以查看腳本中的_ERROR_CATEGORIES 定義的對應的列表)
例子:
--filter=-build,-whitespace,+whitespace/comma
-whitespace题诵,所有的[whitespace*]都將不輸出,但是有了+whitespace/comma性锭,則[whitespace/comma]類型的錯誤將被輸出
控制每行的最長長度
[--linelength=digits]:控制每行的最長長度草冈,google cpplint默認是80字符
擴展檢查文件后綴
輸出錯誤的方式
[--counting=total|toplevel|detailed]:輸出錯誤總數(shù)的方式,默認為total參數(shù)
例子:
--counting=total
#輸出
Total errors found: 96
--counting=toplevel
#輸出
Category 'whitespace' errors found: 88
Category 'build' errors found: 8
Total errors found: 96
--counting=detailed
#輸出:
Category 'whitespace/braces' errors found: 28
Category 'whitespace/semicolon' errors found: 1
Total errors found: 29
cpplint支持每個目錄放置CPPLINT.cfg 單獨配置,CPPLINT.cfg通過包含多組鍵值對實現(xiàn)配置
set noparent
filter=+filter1,-filter2,...
exclude_files=regex
linelength=80
root=subdir
headers=x,y,...
屏蔽代碼不被檢查
假設代碼中有些部分不希望被檢查哩俭,或者覺得是cpplint產(chǎn)生的誤報拳恋。僅僅須要在對應代碼行尾加入凝視”// NOLINT”就可以,cpplint就會跳過這些行谬运。如:
return 0; // NOLINT
控制過濾置信度
對于發(fā)現(xiàn)的每一個問題梆暖,cpplint都會給出一個位于區(qū)間[1, 5]之間的置信度評分,分數(shù)越高就代表問題越肯定轰驳,能夠通過verbose選項控制輸出哪些級別弟灼。例如以下蝗肪。置信度評分為1、2的將不會再輸出:
cpplint --verbose=3 test.cpp
假設須要讓cpplint支持其他文件擴展辛馆,又不想改動源代碼豁延。則能夠通過extensions選項如:
cpplint --extensions=hpp test.cpp
能夠通過filter選項。依照自己的須要過濾掉特定的警告苔可,”-FOO”表示不輸出帶有FOO的警告袋狞,”+FOO”表示輸出帶有FOO的警告,如:
cpplint --filter=-whitespace/tab,+whitespace/ending_newline test.cpp
能夠通過counting選項同蜻。來顯示每種Category有多少個錯誤早处,如:
cpplint.py --counting=detailed test.cpp
4, Q&A
常見報錯原因
- Tab found; better to use spaces 沒有使用四個空格代替縮進
- Lines should be <= 80 characters long 存在大于80字符的行
- Should have a space between // and comment 應該在//和注釋之間有一個空格
- An else should appear on the same line as the preceding }
- If an else has a brace on one side, it should have it on both [readability/braces] 上兩個錯誤經(jīng)常一起出現(xiàn),為大括號的位置不合規(guī)范
- Extra space for operator ++; ++符號和變量間不能有空格
- Redundant blank line at the end of a code block should be deleted. 代碼塊最后的空行應該被刪除
- Line contains invalid UTF-8 (or Unicode replacement character) 使用了中文注釋報的錯
- Line ends in whitespace. 代碼行最后存在空格
- Include the directory when naming .h files [build/include_subdir] [4]
屏蔽指定報錯的具體示例
如下報錯:
1.h:113: Could not find a newline character at the end of the file. [whitespace/ending_newline] [5]
其中 whitespace/ending_newline 是此類出錯的標識默责。
使用如下配置可以屏蔽 readability/utf8,whitespace/ending_newline咸包。多于1個以逗號作為分割,-代表屏蔽葡缰,+代表取消屏蔽忱反。
cpplint.exe --filter=-readability/utf8,-whitespace/ending_newline info.h