Static Check with CPPLINT during Code Review

以下分享一些实践的经验关于如何在code review里加入C++代码的静态检查。

一. 什么是代码静态检查

静态检查是指在不执行代码的情况下对其进行分析评估的过程,是软件质量和软件安全保障的重要一环。它通过词法分析、语义分析、控制流分析、数据流分析等技术对代码逐行解析暴露问题,从而协助我们将许多在运行时才会暴露的棘手麻烦扼杀于摇篮之中。

二. C++代码静态检查工具

Cpplint是一个依据Google's C++ style guide规范的对C/C++文件进行静态检查的命令行工具。在这里我们使用Cpplint工具对看图的C++代码进行静态检查。

三. 配置步骤

1. 配置Cpplint运行环境

假设你的环境里已经安装过python了,支持python2和3。

pip install cpplint

安装完成后,可以直接用命令行进行检查了

cpplint [Options] files

也可以用以下命令获得更多cpplint的帮助

cpplint --help

2. 安装Jenkins Violation Comments to Gitlab插件

在进行第二步之前,假设环境里已经有了Gitlab plugin, 并且做好了相应的配置。若没有,请参考我的另一篇文章GitLab Code Review进行配置

3. Jenkins静态检查工程配置

创建一个新freestyle的工程,在工程配置里,首页要把GitLab trigger打开,这样才会当一个GitLab Merge Request创建了之后,自动触发这个代码静态检查的工程,进行后续的代码审查确认

执行cpplint步骤:在jenkins build step里增加"Execute shell"。以下是亲测比较有用的一些参数,检查的是src目录下(包括子目录)所有后缀名是.cpp和.h的文件。并且将结果输出到cpplint.xml中待后续步骤使用。

另外增加|| exit 0,是因为cpplint执行返回的结果是非0,会导致Jenkins job结果失败

cpplint.exe --extensions=cpp,h --recursive --filter=-whitespace/tab --linelength=120 src 2> cpplint.xml || exit 0

cpplint.xml报告的格式如下:

src\utility\http\HttpRequest.h:0: No copyright message found. You should have a line: "Copyright [year] <Copyright Owner>" [legal/copyright] [5]src\utility\http\HttpRequest.h:14: Found C system header after other header. Should be: HttpRequest.h, c system, c++ system, other. [build/include_order] [4]src\utility\http\HttpRequest.h:15: Found C++ system header after other header. Should be: HttpRequest.h, c system, c++ system, other. [build/include_order] [4]src\utility\http\HttpRequest.h:16: Found C++ system header after other header. Should be: HttpRequest.h, c system, c++ system, other. [build/include_order] [4]src\utility\http\HttpRequest.h:17: Found C system header after other header. Should be: HttpRequest.h, c system, c++ system, other. [build/include_order] [4]src\utility\http\HttpRequest.h:18: Found C system header after other header. Should be: HttpRequest.h, c system, c++ system, other. [build/include_order] [4]src\utility\http\HttpRequest.h:27: { should almost always be at the end of the previous line [whitespace/braces] [4]src\utility\http\HttpRequest.h:28: public: should be indented +1 space inside class HttpRequestUserData [whitespace/indent] [3]src\utility\http\HttpRequest.h:36: { should almost always be at the end of the previous line [whitespace/braces] [4]src\utility\http\HttpRequest.h:37: public: should be indented +1 space inside class HttpRequestTask [whitespace/indent] [3]src\utility\http\HttpRequest.h:38: Missing space before { [whitespace/braces] [5]src\utility\http\HttpRequest.h:39: Missing space before { [whitespace/braces] [5]src\utility\http\HttpRequest.h:45: { should almost always be at the end of the previous line [whitespace/braces] [4]src\utility\http\HttpRequest.h:56: { should almost always be at the end of the previous line [whitespace/braces] [4]src\utility\http\HttpRequest.h:57: Redundant blank line at the start of a code block should be deleted. [whitespace/blank_line] [2]src\utility\http\HttpRequest.h:57: Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]src\utility\http\HttpRequest.h:60: { should almost always be at the end of the previous line [whitespace/braces] [4]src\utility\http\HttpRequest.h:61: Redundant blank line at the start of a code block should be deleted. [whitespace/blank_line] [2]src\utility\http\HttpRequest.h:61: Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]src\utility\http\HttpRequest.h:80: At least two spaces is best between code and comments [whitespace/comments] [2]src\utility\http\HttpRequest.h:80: Use int16/int64/etc, rather than the C type long [runtime/int] [4]src\utility\http\HttpRequest.h:89: { should almost always be at the end of the previous line [whitespace/braces] [4]src\utility\http\HttpRequest.h:96: { should almost always be at the end of the previous line [whitespace/braces] [4]src\utility\http\HttpRequest.h:97: Redundant blank line at the start of a code block should be deleted. [whitespace/blank_line] [2]src\utility\http\HttpRequest.h:97: Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]src\utility\http\HttpRequest.h:100: { should almost always be at the end of the previous line [whitespace/braces] [4]

接下来在构建后步骤里添加"Report Violation to GitLab",按照截图进行配置。

配置完成后,可以创建一个GitLab Merge Request来测试下。

截取部分Jenkins build log比较有价值的信息:

从上面的Jenkins build log里可以看出,cpplint是对src目录下做了全量检查,总共发现了15144个缺陷。GitLab Violation Comments to GitLab插件比对Merge Request改动的文件和改动的部分,只报告跟Merge Request里改动相关的缺陷。所以最终发现了一个ERROR级别的缺陷。

打开GitLab 对应的Merge Request,就可以看到在Disucction里多一条跟violation相关的缺陷报告,并且GitLab也会给提交者发邮件提醒,开发人员可根据缺陷内容(文件/行数/缺陷内容)做相应的处理。

这样可以提高代码的质量,将一些缺陷在Merge之前修改掉。

3. 只检查Merge Request里改动的文件

按照上面的步骤是对所有仓库里的后缀名是.cpp/.h的文件进行检查,进行的是全量检查。如果我们想把代码静态检查放在code review的流程里,那么我们就会想只需要对修改过的文件进行扫描就可以了。这样可以更加有效率,更有针对性。

我是通过小段shell脚本做了处理:

echo "gitlabMergeRequestLastCommit is $gitlabMergeRequestLastCommit" changeFileList=`git show --pretty="" --name-only $gitlabMergeRequestLastCommit`

echo "Changed file list $changeFileList"

for changeFile in $changeFileList

do

    cpplint.exe --extensions=cpp,h --recursive  --linelength=120 $changeFile 2>> cpplint.xml || exit 0

done



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,106评论 0 10
  • This project was bootstrapped with Create React App. Belo...
    unspecx阅读 5,083评论 0 2
  • 印度谚语有句谚语:慢慢走,请等身后的灵魂。你是否觉得大学时同等学历的同学,毕业一两年之后差距越来越大,想当年满腔...
    粒粒往前冲阅读 248评论 0 0
  • 悠悠暖阳照四方 盈盈湖水泛波光 莲花岛上不产莲 螃蟹背起小村庄
    晚风轻拂阅读 185评论 13 10