第1章 简介1.1. 概述注意:请使用新版本0.2.3.0,之前的版本在自动更新上有一个严重的Bug。 Nsiqcppctyle的目标是对C/C++代码进行代码风格的检查,保持代码的可扩展性、易读性以及高维护性。用户可以定制适合自己风格的检查规则,也可以建立自己的规则服务器(可以基于GoogleAppEngine或dJango搭建服务器)。本项目是韩国的NHN公司开发的,用于检查其内部项目的工具。你可以从http://dev.naver.com/projects/nsiqcppstyle/下载源代码。 可用的检查规则可以从公网服务器上下载(http://nsiqcppstyle.appspot.com)。 |
1.2. NsiqCppStyle功能 IntroductionToNsiqCppStyle
NSIQCppStyle是一个用于C/C++编码风格检查的小工具,由韩国的NHN公司开发,并且在http://dev.naver.com/projects/nsiqcppstyle/上开放了其源代码。
功能
- 一共47条检查规则,可以从http://nsiqcppstyle.appspot.com上找到。
- 无需配置(只需要在要检查的目录下运行nsiqcppcytle即可)。
- 规则和分析引擎分离的架构:
- 比较容易增加自定义规则;
- 分析引擎采用触发的方式分析源码,不包含具体的语法风险,所以适合于大多数C/C++。
- 支持IDE集成:
- 比较容易和Visual Studio/Emacs/Eclipse CDT集成,并且支持IDE的错误输出格式。
- 支持持续集成服务器集成:
- 可以输出checkstyle格式的xml文件,在CI服务其上使用ckeckstyle插件查看结果。
- 多种忽略方式:
- 提供4中忽略报警的方法(filefilter/basefile/rule ignore per/violation ignore)。
第2章 C/C++风格检查
2.1. 快速入门
2.1.1. 规则使用
- 它真的很简单
- 确保在C/C++文件的根目录下有一filefilter.txt文件,用于控制使用那些规则。你可以从http://nsiqcppstyle.appspot.com/rule_doc/上获取最新的规则,也可以在NsiqCppStyle的安装目录下找到规则。filefilter.txt文件格式如下:
~ RULE_10_1_A_do_not_use_bufferoverflow_risky_function_for_unix
~ RULE_10_1_B_do_not_use_bufferoverflow_risky_function_for_windows
~ RULE_3_1_A_do_not_start_filename_with_underbar
~ RULE_3_2_B_do_not_use_same_filename_more_than_once
~ RULE_3_2_CD_do_not_use_special_characters_in_filename
~ RULE_3_2_F_use_representitive_classname_for_cpp_filename
~ RULE_3_2_H_do_not_use_underbars_for_cpp_filename
~ RULE_3_2_H_do_not_use_uppercase_for_c_filename
~ RULE_3_3_A_start_function_name_with_is_or_has_when_return_bool
~ RULE_3_3_A_start_function_name_with_lowercase_unix
~ RULE_3_3_A_start_function_name_with_upperrcase_windows
~ RULE_3_3_B_start_private_function_name_with_underbar
~ RULE_4_1_A_A_use_tab_for_indentation
~ RULE_4_1_A_B_use_space_for_indentation
~ RULE_4_1_B_indent_each_enum_item_in_enum_block
~ RULE_4_1_B_locate_each_enum_item_in_seperate_line
~ RULE_4_1_C_align_long_function_parameter_list
~ RULE_4_1_E_align_conditions
~ RULE_4_2_A_A_space_around_operator
~ RULE_4_2_A_B_space_around_word
~ RULE_4_4_A_do_not_write_over_120_columns_per_line
~ RULE_4_5_A_braces_for_function_definition_should_be_located_in_seperate_line
~ RULE_4_5_A_braces_for_type_definition_should_be_located_in_seperate_line
~ RULE_4_5_A_braces_inside_of_function_should_be_located_in_end_of_line
~ RULE_4_5_A_indent_blocks_inside_of_function
~ RULE_4_5_A_matching_braces_inside_of_function_should_be_located_same_column
~ RULE_4_5_B_use_braces_even_for_one_statement
~ RULE_5_2_C_provide_doxygen_class_comment_on_class_def
~ RULE_5_2_C_provide_doxygen_namespace_comment_on_namespace_def
~ RULE_5_2_C_provide_doxygen_struct_comment_on_struct_def
~ RULE_5_3_A_provide_doxygen_function_comment_on_function_in_header
~ RULE_5_3_A_provide_doxygen_function_comment_on_function_in_impl
~ RULE_6_1_A_do_not_omit_function_parameter_names
~ RULE_6_1_E_do_not_use_more_than_5_paramters_in_function
~ RULE_6_1_G_write_less_than_200_lines_for_function
~ RULE_6_2_A_do_not_use_system_dependent_type
~ RULE_6_4_B_initialize_first_item_of_enum
~ RULE_6_5_B_do_not_use_lowercase_for_macro_constants
~ RULE_6_5_B_do_not_use_macro_for_constants
~ RULE_7_1_B_A_do_not_use_double_assignment
~ RULE_7_1_C_do_not_use_question_keyword
~ RULE_7_2_B_do_not_use_goto_statement
~ RULE_8_1_A_provide_file_info_comment
~ RULE_9_1_A_do_not_use_hardcorded_include_path
~ RULE_9_2_D_use_reentrant_function
~ RULE_A_3_avoid_too_deep_blocks
- 如果你想使用所有规则,可以在统计代码的根目录下执行“nsiqcppstyle –r > filefilter.txt”。
- filefilter.txt是NSIQCollector工具的配置文件,使用这个文件设置在进行规则检查时启用那些规则。在文件中使用“~”字符表示启用此规则。
- 一些规则是相互矛盾的,比如:RULE_4_1_A_A_use_tab_for_indent和RULE_4_1_A_B_use_space_for_indent,所以你应该根据需求选择合适的规则。
2.1.2. 检查目录下的C/C++文件
- 设置完规则之后,运行如下命令(当然要确保nsiqcppstyle正确安装,并设置环境变量):
nsiqcppstyle folder_to_be_analyzed
- 然后,NSIQCppStyle会输出如下结果:
======================================================================================
Update: checking for update
============================ Analyzing box ===============================
======================================================================================
* load rule set in filefilter.txt
- RULE_10_1_A_do_not_use_bufferoverflow_risky_function_for_unix is applied.
~~~
- RULE_9_1_A_do_not_use_hardcorded_include_path is applied.
======================================================================================
* report filefilter.txt settings.
Filter Scope "default" is applied.
Current Filter Setting (Following is applied sequentially)
1. \externals\ is excluded
2. \box\ is excluded
3. \tet\ is excluded
4. \.cvs\ is excluded
5. \.svn\ is excluded
Current File extension and Language Settings
C/C++=c,cxx,h,hpp,cpp,hxx
======================================================================================
* Violation list
d:\sample\a.c(1, 5): Do not start function name(SetGlobalAuthority) with uppercase [RULE_3_3_A_start_function_name_with_lowercase_unix]
d:\sample\a.c(1, 5): Doxygen Comment should be provided in front of function (SetGlobalAuthority) in impl file. [RULE_5_3_A_provide_doxygen_function_comment_on_function_in_impl]
d:\sample\utils\trunk\boxmonlog\boxmonlog.cpp(1, 1): Please provide file info comment in front of file [RULE_8_1_A_provide_file_info_comment]
d:\sample\utils\trunk\boxmonlog\boxmonlog.cpp(21, 6): Do not start function name(writeLog) with lowercase [RULE_3_3_A_start_function_name_with_upperrcase_windows]
d:\sample\utils\trunk\boxmonlog\boxmonlog.cpp(21, 6): Doxygen Comment should be provided in front of function (writeLog) in impl file. [RULE_5_3_A_provide_doxygen_function_comment_on_function_in_impl]
d:\sample\utils\trunk\boxmonlog\boxmonlog.cpp(36, 6): Do not start function name(openOut) with lowercase [RULE_3_3_A_start_function_name_with_upperrcase_windows]
d:\sample\utils\trunk\boxmonlog\boxmonlog.cpp(36, 6): Doxygen Comment should be provided in front of function (openOut) in impl file. [RULE_5_3_A_provide_doxygen_function_comment_on_function_in_impl]
d:\sample\utils\trunk\boxmonlog\boxmonlog.cpp(40, 2): Matching Braces inside of function should be located in the same column [RULE_4_5_A_braces_inside_of_function_should_be_located_same_column]
d:\sample\utils\trunk\boxmonlog\boxmonlog.cpp(44, 3): Do not use system dependent type(int). Use system independent type like (int32_t)
============================= Summary Report ==============================
** Total Available Rules : 46
** Total Applied Rules : 43
** Total Violated Rules : 20
** Total Errors Occurs : 243
** Total Analyzed Files : 8
** Total Violated Files Count: 8
** Build Quality : 0.00% <== (total analyzed files - the ratio total violated file count) / total analyzed files * 100
=========================== Violated Rule Details ==========================
- RULE_4_2_A_A_space_between_operators rule violated : 42
~~
- RULE_3_3_A_start_function_name_with_is_or_has_when_return_bool rule violated : 11
=========================== Violated File Details ==========================
- d:\sample\utils\_service\jobqueue.h violated in total : 5
* RULE_4_2_A_A_space_between_operators : 1
~~
* RULE_5_2_C_provide_doxygen_struct_comment_on_struct_def : 1
- d:\sample\utils\_service\broker.cpp violated in total : 44
* RULE_4_2_A_A_space_between_operators : 10
~~
* RULE_5_2_C_provide_doxygen_class_comment_on_class_def : 1
2.1.3. 检查单个C/C++文件
- NSIQCppStyle支持单个文件的分析,运行的命令如下:
nsiqcppstyle -f file_filter_file_name file_to_be_analyzed
- 在指定文件夹检查的时,NSIQCppStyle会自动查找目录下的filefilter.txt文件;但是在指定单个文件时,就无法定位到filefilter.txt文件,所以需要指定filefilter.txt的位置。
2.2. 详细参数
-h / --help | 帮助 |
-r / --show-rules | 输出有效规则 |
-o output_file_name | 输出的文件(只有—output设置为cvs或xml时,才有效)。如果不指定,会在源码的目录下生成nsiqcppstyle_result.xxx报告。然而,如果指定多个源码目录是,就可以通过此选项指定报告的位置和名称。 |
--output= (csv|xml|vs7|emacs) | 输出报告格式,csv和xml检结果输出到文件中。 |
--no-update | 禁止使用自动更新 |
-f file_filter_file_location | 制定filefilter.txt的位置 |
--show-url | 违反规则时,报告中输出URL格式。 |
--var=key:value,key:value | 一些自定规则,你可以通过它设置自定义的值。 |
- 如果你已经看过快速入门,就知道如何配置filefilter.txt。
- 另外,NSIQCppStyle提供了忽略规则的支持。
- 你可以通过如下方式忽略掉规则。
ErrorCase // NS
- // NA是No Style的缩写,意味着在本行不使用规则。
- 在每个文件中,你可以通过注释的方式忽略掉一些规则:
/*
-- RULE_NAME
*/
2.3. 规则过滤
如果你想过滤目标目录下的一些源码文件,请在filefilter.txt中设置过滤条件,格式如下:
* FILTER_SCOPE_NAME
+ INCLUDE_PATH_STRING
- EXCLUDE_PATH_STRING
~ RULE NAME
例如:
* default
- \
+ \src\
- \src\test
~ RULES....
上面的例子中,指定nsiqcppctyle分析出除\src\test之外的所有\src下的源码。
第3章 工具集成
3.1. IDE集成
- NSIQCppStyle可以很容易的和IDE集成
- 比如,在Visual Studio中的集成:首先在“工具”菜单下选择“外部工具”。
- 然后,指定外部工具NsiqCppStyle,选上“使用输出窗口”,确定保存。
- 接下来,请运行设置的外部工具,就会每次都检查工程。
- 如果你想只检查当前打开的文件,就必须设置另外一个外部工具。你应该指定filefilter.txt文件,才能进行单独文件的检查。
- 最后,你还可以为其设置一快捷键,这样就可以使用快捷键执行代码的检查。设置方式为:工具=>自定义=>键盘=>选择外部命令。
3.2. Hudson集成
NSIQCppStyle支持Checkstyle格式输出,所以可以使用Checkstyle的Hudson插件来集成NSIQCppstyle。
在你的编译文件中添加如下命令:
nsiqcppstyle --ci --output=xml folder_to_be_analyzed
或者添加自定义编译步骤:
设置Checkstyle结果位置,即为nsiqcppstyle_result.xml的位置;注意一定要设置为“**/nsiqcppstyle_result.xml”,如果没有“**”将无法显示。
第4章 搭建NsiqCppStyle服务器
可以基于Google AppEngine或Django来搭建自己的NSIQCppStyle服务器。通用服务器位于http://nsiqcppstyle.appspot.com/,你可以使用通用服务器也可以使用自己部署的服务器。
如何部署自己的规则服务器。
Google AppEngine
- 请将nsiqcppstyle服务设置到包含nsiqcppstyle源码的目录下:
- nsiqcppstyle_all
+ nsiqcppstylesvr
+ nsiqcppstyle.
- 从http://appengine.google.com/上获取账户;
- 安装google appengine SDK.
- 修改app.yaml文件
application: nsiqcppstyle ==> application: new google appengine app name.
- 在nsiqcppstyle服务器上运行UpdateToAppEngine.bat:
nsiqcppstyle重新解压到updatefiles目录下,上传修改过的文件;你应该通过输入你的邮件地址和密码来上传规则。
Django
- 安装Django
- 请将nsiqcppstyle服务设置到包含nsiqcppstyle源码的目录下:
- nsiqcppstyle_all
+ nsiqcppstylesvr
+ nsiqcppstyle.
- 运行ant更新命令,将会把nsiqcppstyle重新解压到updatefiles目录下。
- 运行RunNsiqCppStyleSvrOnDJango.bat测试一下:
测试nsiqcppstyle服务器是否正常工作(http://localhost:8080)。
- Django服务只有登录之后才能运行,所以你应该将其附加在apache web服务器之后。
请参考:http://docs.djangoproject.com/en/dev/howto/deployment/modpython/。
如何连接自己的规则服务器
- 默认情况下,NsiqCppStyle连接的服务器是:http://nsiqcppstyle.appspot.com。
- 你可以通过修改ant的build.xml参数来改变连接的规则服务器。
- 你可以通过如下参数来运行ant:
ant package -Dtype=customVersionName -Durl=customRuleServerLocation
- 你可以从服务器上获得新版本的NsiqCppStyle。