工欲善其事,必先利其器:配置VS Code實(shí)現(xiàn)靜態(tài)代碼檢測(cè)

代碼并不是編譯通過(guò)就能正確運(yùn)行的即纲,比如if (value = 3) xxx;,編譯時(shí)可能連個(gè)警告都沒(méi)有,但是運(yùn)行時(shí)卻是另一個(gè)結(jié)果博肋。這時(shí)候通過(guò)靜態(tài)檢測(cè)可以提高一點(diǎn)“質(zhì)量”低斋,靜態(tài)檢測(cè)工具有很多蜂厅,每個(gè)都有自己的特色,所以盡可能用多的工具進(jìn)行檢測(cè)效果會(huì)更好膊畴。

安裝插件

平時(shí)本人都使用VS Code進(jìn)行代碼編輯掘猿,因此如果能在編輯時(shí)便進(jìn)行檢測(cè),肯定會(huì)方便很多唇跨。好在VS Code的插件豐富稠通,搜羅到了一個(gè)比較適合的。

這個(gè)插件可以聚合多個(gè)靜態(tài)檢測(cè)工具买猖,本人窮改橘,買(mǎi)不起收費(fèi)的,所以下面介紹的幾個(gè)檢測(cè)工具都是免費(fèi)的玉控。

安裝并配置檢測(cè)工具

CppCheck

安裝

Cppcheck官網(wǎng)下載并安裝最新的版本唧龄。注意:安裝路徑中不能包含中文或者空格!為了方便奸远,我就安裝到了C盤(pán)根目錄下既棺,C:\Cppcheck。

將Cppcheck添加到系統(tǒng)環(huán)境變量中懒叛,以便插件能檢測(cè)到
在命令行中輸入cppcheck并回車(chē)丸冕,如下圖提示即表示配置成功
由于我們需要使用到CppCheck的Python插件,因此還需要安裝Python環(huán)境薛窥。下載并安裝最新的Python即可胖烛,注意:安裝時(shí)務(wù)必勾選將Python添加到系統(tǒng)環(huán)境變量中!

配置

打開(kāi)VS Code配置诅迷,找到插件C/C++ Lint configuration的配置頁(yè)面佩番,按如下說(shuō)明配置

進(jìn)入settings.json文件,增加如下配置罢杉,這里以keil的編譯環(huán)境為例趟畏,根據(jù)實(shí)際情況設(shè)置路徑

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

進(jìn)入settings.json文件,增加如下配置滩租,

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

進(jìn)入settings.json文件赋秀,增加如下配置,這里選用CERT和MISRA兩個(gè)插件

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

在C:\Cppcheck\addons下增加misra.jsonmisra_rules_2012.txt兩個(gè)文件律想,

文件內(nèi)容如下

{
    "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(fā)” 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

安裝

命令行下通過(guò)執(zhí)行pip install lizard指令進(jìn)行安裝猎莲,安裝完成后執(zhí)行lizard --version驗(yàn)證安裝是否成功

配置

由于插件下對(duì)lizard的參數(shù)配置少得可憐,使用默認(rèn)參數(shù)并不是完全適合自己技即,因此需要通過(guò)特殊手段實(shí)現(xiàn)自定義參數(shù)著洼。搜索磁盤(pán)并找到lizard.py文件,打開(kāi)文件按如下地方進(jìn)行修改
上面4個(gè)參數(shù)依次是圈復(fù)雜度、函數(shù)長(zhǎng)度(包括空行和注釋)身笤、函數(shù)參數(shù)個(gè)數(shù)和函數(shù)代碼長(zhǎng)度(不包括空行和注釋)

Clang

安裝

LLVM 官網(wǎng)下載最新版本并安裝

添加環(huán)境變量

配置

暫時(shí)沒(méi)精力弄

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末豹悬,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子展鸡,更是在濱河造成了極大的恐慌屿衅,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,123評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件莹弊,死亡現(xiàn)場(chǎng)離奇詭異涤久,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)忍弛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門(mén)响迂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人细疚,你說(shuō)我怎么就攤上這事蔗彤。” “怎么了疯兼?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,723評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵然遏,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我吧彪,道長(zhǎng)待侵,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,357評(píng)論 1 283
  • 正文 為了忘掉前任姨裸,我火速辦了婚禮秧倾,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘傀缩。我一直安慰自己那先,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,412評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布赡艰。 她就那樣靜靜地躺著售淡,像睡著了一般。 火紅的嫁衣襯著肌膚如雪瞄摊。 梳的紋絲不亂的頭發(fā)上勋又,一...
    開(kāi)封第一講書(shū)人閱讀 49,760評(píng)論 1 289
  • 那天,我揣著相機(jī)與錄音换帜,去河邊找鬼。 笑死鹤啡,一個(gè)胖子當(dāng)著我的面吹牛惯驼,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 38,904評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼祟牲,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼隙畜!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起说贝,我...
    開(kāi)封第一講書(shū)人閱讀 37,672評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤议惰,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后乡恕,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體言询,經(jīng)...
    沈念sama閱讀 44,118評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,456評(píng)論 2 325
  • 正文 我和宋清朗相戀三年傲宜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了运杭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,599評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡函卒,死狀恐怖辆憔,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情报嵌,我是刑警寧澤虱咧,帶...
    沈念sama閱讀 34,264評(píng)論 4 328
  • 正文 年R本政府宣布,位于F島的核電站锚国,受9級(jí)特大地震影響腕巡,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜跷叉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,857評(píng)論 3 312
  • 文/蒙蒙 一逸雹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧云挟,春花似錦梆砸、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,731評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至沸枯,卻和暖如春日矫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背绑榴。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,956評(píng)論 1 264
  • 我被黑心中介騙來(lái)泰國(guó)打工哪轿, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人翔怎。 一個(gè)月前我還...
    沈念sama閱讀 46,286評(píng)論 2 360
  • 正文 我出身青樓窃诉,卻偏偏與公主長(zhǎng)得像杨耙,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子飘痛,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,465評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容