工欲善其事,必先利其器:配置VS Code实现静态代码检测

代码并不是编译通过就能正确运行的,比如if (value = 3) xxx;,编译时可能连个警告都没有,但是运行时却是另一个结果。这时候通过静态检测可以提高一点“质量”,静态检测工具有很多,每个都有自己的特色,所以尽可能用多的工具进行检测效果会更好。

安装插件

平时本人都使用VS Code进行代码编辑,因此如果能在编辑时便进行检测,肯定会方便很多。好在VS Code的插件丰富,搜罗到了一个比较适合的。

这个插件可以聚合多个静态检测工具,本人穷,买不起收费的,所以下面介绍的几个检测工具都是免费的。

安装并配置检测工具

CppCheck

安装

Cppcheck官网下载并安装最新的版本。注意:安装路径中不能包含中文或者空格!为了方便,我就安装到了C盘根目录下,C:\Cppcheck。

将Cppcheck添加到系统环境变量中,以便插件能检测到
在命令行中输入cppcheck并回车,如下图提示即表示配置成功
由于我们需要使用到CppCheck的Python插件,因此还需要安装Python环境。下载并安装最新的Python即可,注意:安装时务必勾选将Python添加到系统环境变量中!

配置

打开VS Code配置,找到插件C/C++ Lint configuration的配置页面,按如下说明配置

进入settings.json文件,增加如下配置,这里以keil的编译环境为例,根据实际情况设置路径

    "c-cpp-flylint.cppcheck.includePaths": [
        "C:\\Keil_v5\\ARM\\ARMCC\\include"
    ]

进入settings.json文件,增加如下配置,

    "c-cpp-flylint.cppcheck.extraArgs": [
        "--platform=arm32-wchar_t2.xml",
        "--library=gnu.cfg"
    ]

进入settings.json文件,增加如下配置,这里选用CERT和MISRA两个插件

    "c-cpp-flylint.cppcheck.addons": [
        "cert.py",
        "C:\\Cppcheck\\addons\\misra.json"
    ]

在C:\Cppcheck\addons下增加misra.jsonmisra_rules_2012.txt两个文件,

文件内容如下

{
    "script": "misra.py",
    "args": [
        "--rule-texts=C:\\Cppcheck\\addons\\misra_rules_2012.txt",
        "--suppress-rules 15.4,15.5,18.4"
    ]
}
Appendix A Summary of guidelines
Rule 1.1
Rule Required The program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation’s translation limits
Rule 1.2
Rule Advisory Language extensions should not be used
Rule 1.3
Rule Required There shall be no occurrence of unde ned or critical unspecied behaviour
Rule 2.1
Rule Required A project shall not contain unreachable code
Rule 2.2
Rule Required There shall be no dead code
Rule 2.3
Rule Advisory A project should not contain unused type declarations
Rule 2.4
Rule Advisory A project should not contain unused tag declarations
Rule 2.5
Rule Advisory A project should not contain unused macro declarations
Rule 2.6
Rule Advisory A function should not contain unused label declarations
Rule 2.7
Rule Advisory There should be no unused parameters in functions
Rule 3.1
Rule Required The character sequences /* and // shall not be used within a comment
Rule 3.2
Rule Required Line-splicing shall not be used in // comments
Rule 4.1
Rule Required Octal and hexadecimal escape sequences shall be terminated
Rule 4.2
Rule Advisory Trigraphs should not be used
Rule 5.1
Rule Required External identi ers shall be distinct
Rule 5.2
Rule Required Identi ers declared in the same scope and name space shall be distinct
Rule 5.3
Rule Required An identi er declared in an inner scope shall not hide an identier declared in an outer scope
Rule 5.4
Rule Required Macro identi ers shall be distinct
Rule 5.5
Rule Required Identi ers shall be distinct from macro names
Rule 5.6
Rule Required A typedef name shall be a unique identi er
Rule 5.7
Rule Required A tag name shall be a unique identi er
Rule 5.8
Rule Required Identi ers that de ne objects or functions with external linkage shall be unique
Rule 5.9
Rule Advisory Identi ers that de ne objects or functions with internal linkage should be unique
Rule 6.1
Rule Required Bit- elds shall only be declared with an appropriate type
Rule 6.2
Rule Required Single-bit named bit elds shall not be of a signed type
Rule 7.1
Rule Required Octal constants shall not be used
Rule 7.2
Rule Required A “u” or “U” suffix shall be applied to all integer constants that are represented in an unsigned type
Rule 7.3
Rule Required The lowercase character “l” shall not be used in a literal suffix
Rule 7.4
Rule Required A string literal shall not be assigned to an object unless the object's type is "pointer to const-quali ed char"
Rule 8.1
Rule Required Types shall be explicitly speci ed
Rule 8.2
Rule Required Function types shall be in prototype form with named parameters
Rule 8.3
Rule Required All declarations of an object or function shall use the same names and type qualiers
Rule 8.4
Rule Required A compatible declaration shall be visible when an object or function with external linkage is de ned
Rule 8.5
Rule Required An external object or function shall be declared once in one and only one file
Rule 8.6
Rule Required An identi er with external linkage shall have exactly one external definition
Rule 8.7
Rule Advisory Functions and objects should not be de ned with external linkage if they are referenced in only one translation unit
Rule 8.8
Rule Required The static storage class speci er shall be used in all declarations of objects and functions that have internal linkage
Rule 8.9
Rule Advisory An object should be de ned at block scope if its identi er only appears in a single function
Rule 8.10
Rule Required An inline function shall be declared with the static storage class
Rule 8.11
Rule Advisory When an array with external linkage is declared, its size should be explicitly specified
Rule 8.12
Rule Required Within an enumerator list, the value of an implicitly-speci ed
enumeration constant shall be unique
Rule 8.13
Rule Advisory A pointer should point to a const-quali ed type whenever possible
Rule 8.14
Rule Required The restrict type quali er shall not be used
Rule 9.1
Rule Mandatory The value of an object with automatic storage duration shall not be read before it has been set
Rule 9.2
Rule Required The initializer for an aggregate or union shall be enclosed in braces
Rule 9.3
Rule Required Arrays shall not be partially initialized
Rule 9.4
Rule Required An element of an object shall not be initialized more than once
Rule 9.5
Rule Required Where designated initializers are used to initialize an array object the size of the array shall be speci ed explicitly
Rule 10.1
Rule Required Operands shall not be of an inappropriate essential type
Rule 10.2
Rule Required Expressions of essentially character type shall not be used inappropriately in addition and subtraction operations
Rule 10.3
Rule Required The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category
Rule 10.4
Rule Required Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category
Rule 10.5
Rule Advisory The value of an expression should not be cast to an inappropriate essential type
Rule 10.6
Rule Required The value of a composite expression shall not be assigned to an object with wider essential type
Rule 10.7
Rule Required If a composite expression is used as one operand of an operator in which the usual arithmetic conversions are performed then the other operand shall not have wider essential type
Rule 10.8
Rule Required The value of a composite expression shall not be cast to a different essential type category or a wider essential type
Rule 11.1
Rule Required Conversions shall not be performed between a pointer to a function and any other type
Rule 11.2
Rule Required Conversions shall not be performed between a pointer to an incomplete type and any other type
Rule 11.3
Rule Required A cast shall not be performed between a pointer to object type and a pointer to a different object type
Rule 11.4
Rule Advisory A conversion should not be performed between a pointer to object and an integer type
Rule 11.5
Rule Advisory A conversion should not be performed from pointer to void into pointer to object
Rule 11.6
Rule Required A cast shall not be performed between pointer to void and an arithmetic type
Rule 11.7
Rule Required A cast shall not be performed between pointer to object and a noninteger arithmetic type
Rule 11.8
Rule Required A cast shall not remove any const or volatile quali cation from the type pointed to by a pointer
Rule 11.9
Rule Required The macro NULL shall be the only permitted form of integer null pointer constant
Rule 12.1
Rule Advisory The precedence of operators within expressions should be made explicit
Rule 12.2
Rule Required The right hand operand of a shift operator shall lie in the range zero to one less than the width in bits of the essential type of the left hand operand
Rule 12.3
Rule Advisory The comma operator should not be used
Rule 12.4
Rule Advisory Evaluation of constant expressions should not lead to unsigned integer wrap-around
Rule 13.1
Rule Required Initializer lists shall not contain persistent side effects
Rule 13.2
Rule Required The value of an expression and its persistent side effects shall be the same under all permitted evaluation orders
Rule 13.3
Rule Advisory A full expression containing an increment (++) or decrement (--) operator should have no other potential side effects other than that caused by the increment or decrement operator
Rule 13.4
Rule Advisory The result of an assignment operator should not be used
Rule 13.5
Rule Required The right hand operand of a logical && or || operator shall not contain persistent side effects
Rule 13.6
Rule Mandatory The operand of the sizeof operator shall not contain any expression which has potential side effects
Rule 14.1
Rule Required A loop counter shall not have essentially oating type
Rule 14.2
Rule Required A for loop shall be well-formed
Rule 14.3
Rule Required Controlling expressions shall not be invariant
Rule 14.4
Rule Required The controlling expression of an if statement and the controlling expression of an iteration-statement shall have essentially Boolean type
Rule 15.1
Rule Advisory The goto statement should not be used
Rule 15.2
Rule Required The goto statement shall jump to a label declared later in the same function
Rule 15.3
Rule Required Any label referenced by a goto statement shall be declared in the same block, or in any block enclosing the goto statement
Rule 15.4
Rule Advisory There should be no more than one break or goto statement used to terminate any iteration statement
Rule 15.5
Rule Advisory A function should have a single point of exit at the end
Rule 15.6
Rule Required The body of an iteration-statement or a selection-statement shall be a compound-statement
Rule 15.7
Rule Required All if … else if constructs shall be terminated with an else statement Switch statements
Rule 16.1
Rule Required All switch statements shall be well-formed
Rule 16.2
Rule Required A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement
Rule 16.3
Rule Required An unconditional break statement shall terminate every switch-clause
Rule 16.4
Rule Required Every switch statement shall have a default label
Rule 16.5
Rule Required A default label shall appear as either the rst or the last switch label of a switch statement
Rule 16.6
Rule Required Every switch statement shall have at least two switch-clauses
Rule 16.7
Rule Required A switch-expression shall not have essentially Boolean type
Rule 17.1
Rule Required The features of <stdarg.h> shall not be used
Rule 17.2
Rule Required Functions shall not call themselves, either directly or indirectly
Rule 17.3
Rule Mandatory A function shall not be declared implicitly
Rule 17.4
Rule Mandatory All exit paths from a function with non-void return type shall have an explicit return statement with an expression
Rule 17.5
Rule Advisory The function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elements
Rule 17.6
Rule Mandatory The declaration of an array parameter shall not contain the static keyword between the [ ]
Rule 17.7
Rule Required The value returned by a function having non-void return type shall be used
Rule 17.8
Rule Advisory A function parameter should not be modified
Rule 18.1
Rule Required A pointer resulting from arithmetic on a pointer operand shall address an element of the same array as that pointer operand
Rule 18.2
Rule Required Subtraction between pointers shall only be applied to pointers that address elements of the same array
Rule 18.3
Rule Required The relational operators >, >=, < and <= shall not be applied to objects of pointer type except where they point into the same object
Rule 18.4
Rule Advisory The +, -, += and -= operators should not be applied to an expression of pointer type
Rule 18.5
Rule Advisory Declarations should contain no more than two levels of pointer nesting
Rule 18.6
Rule Required The address of an object with automatic storage shall not be copied to another object that persists after the rst object has ceased to exist
Rule 18.7
Rule Required Flexible array members shall not be declared
Rule 18.8
Rule Required Variable-length array types shall not be used
Rule 19.1
Rule Mandatory An object shall not be assigned or copied to an overlapping object
Rule 19.2
Rule Advisory The union keyword should not be used
Rule 20.1
Rule Advisory #include directives should only be preceded by preprocessor directives
or comments
Rule 20.2
Rule Required The ', " or \ characters and the /* or // character sequences shall not occur in a header le name
Rule 20.3
Rule Required The #include directive shall be followed by either a <filename> or "filename" sequence
Rule 20.4
Rule Required A macro shall not be de ned with the same name as a keyword
Rule 20.5
Rule Advisory #undef should not be used
Rule 20.6
Rule Required Tokens that look like a preprocessing directive shall not occur within a macro argument
Rule 20.7
Rule Required Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses
Rule 20.8
Rule Required The controlling expression of a #if or #elif preprocessing directive shall evaluate to 0 or 1
Rule 20.9
Rule Required All identi ers used in the controlling expression of #if or #elif preprocessing directives shall be #de ne’d before evaluation
Rule 20.10
Rule Advisory The # and ## preprocessor operators should not be used
Rule 20.11
Rule Required A macro parameter immediately following a # operator shall not immediately be followed by a ## operator
Rule 20.12
Rule Required A macro parameter used as an operand to the # or ## operators, which is itself subject to further macro replacement, shall only be used as an
operand to these operators
Rule 20.13
Rule Required A line whose rst token is # shall be a valid preprocessing directive
Rule 20.14
Rule Required All #else, #elif and #endif preprocessor directives shall reside in the same le as the #if, #ifdef or #ifndef directive to which they are related
Rule 21.1
Rule Required #de ne and #undef shall not be used on a reserved identier or reserved macro name
Rule 21.2
Rule Required A reserved identi er or macro name shall not be declared
Rule 21.3
Rule Required The memory allocation and deallocation functions of <stdlib.h> shall not be used
Rule 21.4
Rule Required The standard header le <setjmp.h> shall not be used
Rule 21.5
Rule Required The standard header le <signal.h> shall not be used
Rule 21.6
Rule Required The Standard Library input/output functions shall not be used
Rule 21.7
Rule Required The atof, atoi, atol and atoll functions of <stdlib.h> shall not be used
Rule 21.8
Rule Required The library functions abort, exit, getenv and system of <stdlib.h> shall not be used
Rule 21.9
Rule Required The library functions bsearch and qsort of <stdlib.h> shall not be used
Rule 21.10
Rule Required The Standard Library time and date functions shall not be used
Rule 21.11
Rule Required The standard header le <tgmath.h> shall not be used
Rule 21.12
Rule Advisory The exception handling features of <fenv.h> should not be used
Rule 22.1
Rule Required All resources obtained dynamically by means of Standard Library functions shall be explicitly released
Rule 22.2
Rule  Mandatory A block of memory shall only be freed if it was allocated by means of a Standard Library function
Rule 22.3
Rule  Required The same le shall not be open for read and write access at the same time on different streams
Rule 22.4
Rule  Mandatory There shall be no attempt to write to a stream which has been opened as read-only
Rule 22.5
Rule  Mandatory A pointer to a FILE object shall not be dereferenced
Rule 22.6
Rule  Mandatory The value of a pointer to a FILE shall not be used after the associated stream has been closed

lizard

安装

命令行下通过执行pip install lizard指令进行安装,安装完成后执行lizard --version验证安装是否成功

配置

由于插件下对lizard的参数配置少得可怜,使用默认参数并不是完全适合自己,因此需要通过特殊手段实现自定义参数。搜索磁盘并找到lizard.py文件,打开文件按如下地方进行修改
上面4个参数依次是圈复杂度、函数长度(包括空行和注释)、函数参数个数和函数代码长度(不包括空行和注释)

Clang

安装

LLVM 官网下载最新版本并安装

添加环境变量

配置

暂时没精力弄

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

推荐阅读更多精彩内容