代碼風(fēng)格審查工具Cpplint
它山之石,可以攻玉。 --《詩(shī)經(jīng)·小雅·鶴鳴》
1,簡(jiǎn)介
Cpplint是一個(gè)Python腳本,作為一款開(kāi)源免費(fèi)的代碼靜態(tài)檢測(cè)工具,Google也使用它作為自己的C++代碼檢測(cè)工具,也就是說(shuō),只要你想代碼遵從Google C++代碼規(guī)范,那么Cpplint將會(huì)提供很好的代碼靜態(tài)檢測(cè)支持。
cpplint
Cpplint.py支持的文件格式包括.cc、.h、.cpp、.cu、.cuh。
Cpplint只是一個(gè)代碼風(fēng)格檢測(cè)工具,其并不對(duì)代碼邏輯、語(yǔ)法錯(cuò)誤等進(jìn)行檢查。
2,安裝
Cpplint在Window下可以如下幾種方式使用
2.1運(yùn)行命令行檢測(cè)
使用命令即可對(duì)代碼文件進(jìn)行檢測(cè)。
Python cpplint.py <file>
2.2 集成到VS中使用
在vs中使用的配置
這樣使用比較方便,支持錯(cuò)誤雙擊跳轉(zhuǎn)。
集成到VS:打開(kāi)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,命令行的詳細(xì)使用
cpplint.py [--verbose=#]
[--output=vs7]
[--filter=-x,+y,...]
[--counting=total|toplevel|detailed]
[--root=subdir]
[--linelength=digits]
[--headers=x,y,...]
[--quiet]
<file> [file] ...
verbose指定輸出錯(cuò)誤級(jí)別
對(duì)于發(fā)現(xiàn)的每個(gè)問(wèn)題,cpplint都會(huì)給出一個(gè)位于區(qū)間[1, 5]之間的置信度評(píng)分,分?jǐn)?shù)越高就代表問(wèn)題越肯定,可以通過(guò)verbose選項(xiàng)控制輸出哪些級(jí)別,如下,置信度評(píng)分為1、2的將不會(huì)再輸出
cpplint.py --verbose=3 test.cpp
cpplint的輸出格式
[--output=vs7]:cpplint的輸出格式有emacs和vs7兩種, 默認(rèn)是emacs,vs7是Visual Studio輸出的兼容格式
指定輸出錯(cuò)誤類型
[--filter=-x,+y,...]:指定輸出錯(cuò)誤類型,-表示不輸出,+表示輸出(錯(cuò)誤類型可以查看腳本中的_ERROR_CATEGORIES 定義的對(duì)應(yīng)的列表)
例子:
--filter=-build,-whitespace,+whitespace/comma
-whitespace,所有的[whitespace*]都將不輸出,但是有了+whitespace/comma,則[whitespace/comma]類型的錯(cuò)誤將被輸出
控制每行的最長(zhǎng)長(zhǎng)度
[--linelength=digits]:控制每行的最長(zhǎng)長(zhǎng)度,google cpplint默認(rèn)是80字符
擴(kuò)展檢查文件后綴
輸出錯(cuò)誤的方式
[--counting=total|toplevel|detailed]:輸出錯(cuò)誤總數(shù)的方式,默認(rèn)為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支持每個(gè)目錄放置CPPLINT.cfg 單獨(dú)配置,CPPLINT.cfg通過(guò)包含多組鍵值對(duì)實(shí)現(xiàn)配置
set noparent
filter=+filter1,-filter2,...
exclude_files=regex
linelength=80
root=subdir
headers=x,y,...
屏蔽代碼不被檢查
假設(shè)代碼中有些部分不希望被檢查,或者覺(jué)得是cpplint產(chǎn)生的誤報(bào)。僅僅須要在對(duì)應(yīng)代碼行尾加入凝視”// NOLINT”就可以,cpplint就會(huì)跳過(guò)這些行。如:
return 0; // NOLINT
控制過(guò)濾置信度
對(duì)于發(fā)現(xiàn)的每一個(gè)問(wèn)題,cpplint都會(huì)給出一個(gè)位于區(qū)間[1, 5]之間的置信度評(píng)分,分?jǐn)?shù)越高就代表問(wèn)題越肯定,能夠通過(guò)verbose選項(xiàng)控制輸出哪些級(jí)別。例如以下。置信度評(píng)分為1、2的將不會(huì)再輸出:
cpplint --verbose=3 test.cpp
假設(shè)須要讓cpplint支持其他文件擴(kuò)展,又不想改動(dòng)源代碼。則能夠通過(guò)extensions選項(xiàng)如:
cpplint --extensions=hpp test.cpp
能夠通過(guò)filter選項(xiàng)。依照自己的須要過(guò)濾掉特定的警告,”-FOO”表示不輸出帶有FOO的警告,”+FOO”表示輸出帶有FOO的警告,如:
cpplint --filter=-whitespace/tab,+whitespace/ending_newline test.cpp
能夠通過(guò)counting選項(xiàng)。來(lái)顯示每種Category有多少個(gè)錯(cuò)誤,如:
cpplint.py --counting=detailed test.cpp
4, Q&A
常見(jiàn)報(bào)錯(cuò)原因
- Tab found; better to use spaces 沒(méi)有使用四個(gè)空格代替縮進(jìn)
- Lines should be <= 80 characters long 存在大于80字符的行
- Should have a space between // and comment 應(yīng)該在//和注釋之間有一個(gè)空格
- 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] 上兩個(gè)錯(cuò)誤經(jīng)常一起出現(xiàn),為大括號(hào)的位置不合規(guī)范
- Extra space for operator ++; ++符號(hào)和變量間不能有空格
- Redundant blank line at the end of a code block should be deleted. 代碼塊最后的空行應(yīng)該被刪除
- Line contains invalid UTF-8 (or Unicode replacement character) 使用了中文注釋報(bào)的錯(cuò)
- Line ends in whitespace. 代碼行最后存在空格
- Include the directory when naming .h files [build/include_subdir] [4]
屏蔽指定報(bào)錯(cuò)的具體示例
如下報(bào)錯(cuò):
1.h:113: Could not find a newline character at the end of the file. [whitespace/ending_newline] [5]
其中 whitespace/ending_newline 是此類出錯(cuò)的標(biāo)識(shí)。
使用如下配置可以屏蔽 readability/utf8,whitespace/ending_newline。多于1個(gè)以逗號(hào)作為分割,-代表屏蔽,+代表取消屏蔽。
cpplint.exe --filter=-readability/utf8,-whitespace/ending_newline info.h
5,參考鏈接
