原創(chuàng)|行業(yè)資訊|編輯:鄭恭琳|2020-08-26 15:59:43.917|閱讀 1387 次
概述:Gimpel軟件為MISRA社區(qū)提供了近20年的支持,致力于為MISRA C 2012提供可靠的一流支持。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Gimpel軟件為MISRA社區(qū)提供了近20年的支持,致力于為MISRA C 2012提供可靠的一流支持。
PC-lint Plus 1.3為包括MISRA C 2012的幾乎所有可靜態(tài)執(zhí)行的準(zhǔn)則提供支持,并且其中絕大多數(shù)都得到了全面的支持。通過將au-misra3.lnt文件(隨PC-lint Plus分發(fā))的引用添加到現(xiàn)有配置中,可以輕松完成對MISRA C 2012合規(guī)性的檢查。該文件啟用與MISRA C 2012指南相對應(yīng)的消息,并在已發(fā)布消息中添加文本,以指定與每個(gè)適用消息相關(guān)的特定規(guī)則。au-misra3.lnt文件是使用標(biāo)準(zhǔn)PC-lint Plus選項(xiàng)語法的,易于閱讀的純文本配置文件,可以輕松地對其進(jìn)行修改以滿足任何單個(gè)項(xiàng)目的需求。
PC-lint Plus隨附的參考手冊包括一個(gè)支持矩陣,詳細(xì)列出了每個(gè)指南的支持級別以及每個(gè)指南的支持機(jī)制。
考慮以下示例,其中包含許多MISRA C 2012違規(guī):
typedef short int16_t; typedef int int32_t; typedef unsigned short uint16_t; int32_t calc(uint16_t id_1, uint16_t id_2, int16_t *idl, uint16_t idl_size); int32_t calc(uint16_t id_1, uint16_t id_2, int16_t *idl, uint16_t idl_size) { if (idl_size && id_1 < idl_size && id_2 < idl_size) return idl[id_1] * idl[id_2] + idl[id_1]; return 0; }
當(dāng)使用PC-lint Plus分析此示例時(shí),報(bào)告的MISRA C 2012違規(guī)包括(其中包括):
9046: 'idl' is typographically ambiguous with respect to 'id_1' when ignoring underscores,
treating '1' as 'I', and treating 'l' as 'I' [MISRA 2012 Directive 4.5, advisory]
int32_t calc(uint16_t id_1, uint16_t id_2, int16_t *idl, uint16_t idl_size);
^
891: previous declaration is here
int32_t calc(uint16_t id_1, uint16_t id_2, int16_t *idl, uint16_t idl_size);
^
904: return statement before end of function 'calc' [MISRA 2012 Rule 15.5, advisory]
return idl[id_1] * idl[id_2] + idl[id_1];
^
9012: body should be a compound statement [MISRA 2012 Rule 15.6, required]
return idl[id_1] * idl[id_2] + idl[id_1];
^
9050: dependence placed on operator precedence (operators '&&' and '<') [MISRA
2012 Rule 12.1, advisory]
if (idl_size && id_1 < idl_size && id_2 < idl_size)
^ ~~~~
9027: an unsigned value is not an appropriate left operand to && [MISRA 2012
Rule 10.1, required]
if (idl_size && id_1 < idl_size && id_2 < idl_size)
~~~~~~~~ ^
9031: cannot assign a composite expression of type 'signed16' to an object of
wider type 'signed32' [MISRA 2012 Rule 10.6, required]
return idl[id_1] * idl[id_2] + idl[id_1];
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9050: dependence placed on operator precedence (operators '+' and '*') [MISRA
2012 Rule 12.1, advisory]
return idl[id_1] * idl[id_2] + idl[id_1];
~ ^
818: parameter 'idl' of function 'calc(uint16_t, uint16_t, int16_t *, uint16_t)'
could be pointer to const [MISRA 2012 Rule 8.13, advisory]
int32_t calc(uint16_t id_1, uint16_t id_2, int16_t *idl,
報(bào)告的每項(xiàng)違規(guī)都包括發(fā)生違規(guī)的位置、消息號和基本問題的文本描述,以及違反的MISRA C 2012規(guī)則。例如,在消息中:
904: return statement before end of function 'calc' [MISRA 2012 Rule 15.5, advisory]
return idl[id_1] * idl[id_2] + idl[id_1];
^
904是與在函數(shù)結(jié)束之前出現(xiàn)return語句時(shí)發(fā)出的消息相關(guān)聯(lián)的PC-lint Plus消息號。在這種情況下,消息的實(shí)際文本是“函數(shù)‘calc’結(jié)束之前的return語句”,它標(biāo)識了有問題的函數(shù)。違反MISRA規(guī)則或指令的消息包含在消息文本末尾的方括號中。接下來的兩行顯示了與消息關(guān)聯(lián)的上下文和位置。
《PC-lint Plus參考手冊》包含每條消息的說明,并經(jīng)常提供可用于解決問題的其他指導(dǎo)。此信息也可以顯示在命令行上。例如,要顯示消息904的描述,請運(yùn)行帶有選項(xiàng)-help=904的PC-lint Plus,以使PC-lint Plus顯示以下注釋:
A return statement was found before the end of a function definition.
Many programming standards require that functions contain a single exit
point located at the end of the function. This can enhance readability
and may make subsequent modification less error prone.
(在函數(shù)定義結(jié)束之前找到了return語句。許多編程標(biāo)準(zhǔn)都要求函數(shù)必須包含位于函數(shù)末尾的單個(gè)出口點(diǎn)。這可以增強(qiáng)可讀性,并且可以使后續(xù)修改的出錯(cuò)率降低。)
有關(guān)指定規(guī)則或指令的信息,可查閱MISRA C 2012指南文檔。
重寫上面的calc函數(shù)以解決所報(bào)告的違規(guī)的一種方法是:
int32_t calc(uint16_t id_1, uint16_t id_2, const int16_t *id_list, uint16_t idl_size); int32_t calc(uint16_t id_1, uint16_t id_2, const int16_t *id_list, uint16_t idl_size) { int32_t result = 0; if ((idl_size > 0U) && (id_1 < idl_size) && (id_2 < idl_size)) { result = ((int32_t)id_list[id_1] * id_list[id_2]) + id_list[id_1]; } return result; }
偏差是指源代碼中的違反規(guī)則或指令的情況已被檢查并視為可接受的實(shí)例。 MISRA C 2012文檔包含咨詢,必需和強(qiáng)制性準(zhǔn)則。合規(guī)項(xiàng)目可能不會偏離強(qiáng)制性準(zhǔn)則,但可能包含批準(zhǔn)的要求和咨詢準(zhǔn)則偏差。雖然偏差過程因項(xiàng)目而異,但可以使用非常靈活的抑制機(jī)制在PC-lint Plus中配置偏差。當(dāng)引用特定的符號或類型時(shí),或在特定的行上時(shí),可以通過多種方式來抑制大多數(shù)消息,例如在文件、函數(shù)或語句中。某些類型的禁止操作要求在源代碼中添加注釋,但大多數(shù)不需要。
例如,MISRA 2012規(guī)則15.5(上面由消息904報(bào)告)是一項(xiàng)建議性規(guī)則,建議為每個(gè)功能使用一個(gè)退出點(diǎn)。可以使用選項(xiàng)-efunc(904,calc)對函數(shù)calc禁止顯示此消息,該選項(xiàng)可以作為棉絨注釋放置在項(xiàng)目配置文件或源代碼中。可以以相同的方式抑制來自此功能內(nèi)的其他消息。評論可以遵循抑制選項(xiàng),其中可能包括基本原理或形式偏差信息。
PC-lint Plus區(qū)分庫代碼(默認(rèn)情況下包括外部和系統(tǒng)頭,但可以自定義以包括頭和模塊的任何子集)和項(xiàng)目代碼。默認(rèn)情況下,PC-lint Plus將同時(shí)檢查庫代碼和項(xiàng)目代碼是否符合MISRA C2012。通常希望將檢查范圍限制為項(xiàng)目代碼,這很容易在引用au-misra3.lnt文件后,通過使用選項(xiàng)-wlib=4 -wlib=1來重置庫警告級別來完成。也可以使用-elib和+elib選項(xiàng)輕松地為庫代碼啟用或禁用單個(gè)消息。
基本類型計(jì)算
PC-lint Plus可以解釋使用+f12選項(xiàng)計(jì)算復(fù)合表達(dá)式的最終基本類型所涉及的步驟。消息9903將在完整的表達(dá)式之后發(fā)出,以說明表達(dá)式中涉及的MISRA C 2012基本類型以及它們?nèi)绾谓M合形成新的基本類型。
例如,給定:
int32_t f(int32_t a, int16_t b) {
return (b + b) + (a + b);
}
PC-lint Plus發(fā)出:
note 9032: left operand to + is a composite expression of type 'signed16'
which is smaller than the right operand of type 'signed32'
[MISRA 2012 Rule 10.7, required]
return (b + b) + (a + b);
~~~~~~~ ^
為什么左側(cè)是signed16,右側(cè)是signed32?
用+f12和+e9903處理示例會逐步評估表達(dá)式,并在每個(gè)步驟中涉及相應(yīng)的基本類型:
info 9903: (signed16 + signed16) + (signed32 + signed16)
info 9903: (signed16) + (signed32)
info 9903: signed32
說明在右操作數(shù)中,sign3232 + signed16產(chǎn)生了與左操作數(shù)沖突的sign32。
相關(guān)推薦:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn