- 背景
C++ 是 Google 大部分開源項目的主要編程語言. 正如每個 C++ 程序員都知道的, C++ 有很多強大的特性, 但這種強大不可避免的導致它走向復雜,使代碼更容易產生 bug, 難以閱讀和維護.
為了加強代碼的一致性,保持統一編程風格,使任何程序員都可以快速讀懂你的代碼,Google開發了一個C++代碼風格檢查工具(cpplint),但是該工具僅僅將檢查結果輸出到控制台,並不很容易讀,也並不能很好的和代碼結合在一起展示。沒有一個html頁面的檢查報表,也不利於jenkins的集成展示,在這種強大的需求下,Cppstyle這款工具就誕生了。Cppstyle對原有工具的控制台輸出做了,從繁雜的檢查結果字符串中提出關鍵信息,結合代碼文件,輸出了html頁面。簡潔的頁面,更方便的編碼人員進行代碼風格的修改。
2. 工具介紹
該工具在Google原有工具的基礎上做了如下幾點的改進:
1. 支持對一個目錄底下的所有源文件進行代碼檢查,同時支持針對一個源文件的代碼檢查
2. 支持檢查規則的定制,可以在當前目錄底下的filter.sdp文件里指定檢查規則,通過設置檢查規則,可以將不需要檢查的規則過濾掉,規則以逗號分隔
3. 支持html頁面展示
3. 使用方法
python Gaara.py -s sourcefile > result.html
檢查規則過濾:可在當前目錄下filter.sdp設置,並以逗號分隔
example:
-whitespace,+whitespace/braces
將過濾規則為whitespace,但是保留規則whitespace/braces,未指定規則默認保留。目前Google原始工具支持的規則如下:
'build/class', 'build/deprecated', 'build/endif_comment', 'build/explicit_make_pair', 'build/forward_decl', 'build/header_guard', 'build/include', 'build/include_alpha', 'build/include_order', 'build/include_what_you_use', 'build/namespaces', 'build/printf_format', 'build/storage_class', 'legal/copyright', 'readability/alt_tokens', 'readability/braces', 'readability/casting', 'readability/check', 'readability/constructors', 'readability/fn_size', 'readability/function', 'readability/multiline_comment', 'readability/multiline_string', 'readability/namespace', 'readability/nolint', 'readability/streams', 'readability/todo', 'readability/utf8', 'runtime/arrays', 'runtime/casting', 'runtime/explicit', 'runtime/int', 'runtime/init', 'runtime/invalid_increment', 'runtime/member_string_references', 'runtime/memset', 'runtime/operator', 'runtime/printf', 'runtime/printf_format', 'runtime/references', 'runtime/rtti', 'runtime/sizeof', 'runtime/string', 'runtime/threadsafe_fn', 'whitespace/blank_line', 'whitespace/braces', 'whitespace/comma', 'whitespace/comments', 'whitespace/empty_loop_body', 'whitespace/end_of_line', 'whitespace/ending_newline', 'whitespace/forcolon', 'whitespace/indent', 'whitespace/labels', 'whitespace/line_length', 'whitespace/newline', 'whitespace/operators', 'whitespace/parens', 'whitespace/semicolon', 'whitespace/tab', 'whitespace/todo'
4. 創新點
1. html頁面展示,幫助開發人員,從繁雜的檢查結果中解放出來,更加方便RD修改自己的代碼
2. 可定制檢查規則,方便開發人員過濾不需要檢查的規則
2. 可以和持續集成jenkins融合在一起,目前已有RD使用。QA檢查RD的代碼風格。