工欲善其事,必先利其器:配置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  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  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  shall not be used
Rule 21.4
Rule Required The standard header le  shall not be used
Rule 21.5
Rule Required The standard header le  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  shall not be used
Rule 21.8
Rule Required The library functions abort, exit, getenv and system of  shall not be used
Rule 21.9
Rule Required The library functions bsearch and qsort of  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  shall not be used
Rule 21.12
Rule Advisory The exception handling features of  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 官网下载最新版本并安装

添加环境变量

配置

暂时没精力弄

你可能感兴趣的:(工欲善其事,必先利其器:配置VS Code实现静态代码检测)