原創|行業資訊|編輯:鄭恭琳|2020-08-26 15:59:43.917|閱讀 1387 次
概述:Gimpel軟件為MISRA社區提供了近20年的支持,致力于為MISRA C 2012提供可靠的一流支持。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Gimpel軟件為MISRA社區提供了近20年的支持,致力于為MISRA C 2012提供可靠的一流支持。
PC-lint Plus 1.3為包括MISRA C 2012的幾乎所有可靜態執行的準則提供支持,并且其中絕大多數都得到了全面的支持。通過將au-misra3.lnt文件(隨PC-lint Plus分發)的引用添加到現有配置中,可以輕松完成對MISRA C 2012合規性的檢查。該文件啟用與MISRA C 2012指南相對應的消息,并在已發布消息中添加文本,以指定與每個適用消息相關的特定規則。au-misra3.lnt文件是使用標準PC-lint Plus選項語法的,易于閱讀的純文本配置文件,可以輕松地對其進行修改以滿足任何單個項目的需求。
PC-lint Plus隨附的參考手冊包括一個支持矩陣,詳細列出了每個指南的支持級別以及每個指南的支持機制。
考慮以下示例,其中包含許多MISRA C 2012違規:
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; }
當使用PC-lint Plus分析此示例時,報告的MISRA C 2012違規包括(其中包括):
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,
報告的每項違規都包括發生違規的位置、消息號和基本問題的文本描述,以及違反的MISRA C 2012規則。例如,在消息中:
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是與在函數結束之前出現return語句時發出的消息相關聯的PC-lint Plus消息號。在這種情況下,消息的實際文本是“函數‘calc’結束之前的return語句”,它標識了有問題的函數。違反MISRA規則或指令的消息包含在消息文本末尾的方括號中。接下來的兩行顯示了與消息關聯的上下文和位置。
《PC-lint Plus參考手冊》包含每條消息的說明,并經常提供可用于解決問題的其他指導。此信息也可以顯示在命令行上。例如,要顯示消息904的描述,請運行帶有選項-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.
(在函數定義結束之前找到了return語句。許多編程標準都要求函數必須包含位于函數末尾的單個出口點。這可以增強可讀性,并且可以使后續修改的出錯率降低。)
有關指定規則或指令的信息,可查閱MISRA C 2012指南文檔。
重寫上面的calc函數以解決所報告的違規的一種方法是:
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; }
偏差是指源代碼中的違反規則或指令的情況已被檢查并視為可接受的實例。 MISRA C 2012文檔包含咨詢,必需和強制性準則。合規項目可能不會偏離強制性準則,但可能包含批準的要求和咨詢準則偏差。雖然偏差過程因項目而異,但可以使用非常靈活的抑制機制在PC-lint Plus中配置偏差。當引用特定的符號或類型時,或在特定的行上時,可以通過多種方式來抑制大多數消息,例如在文件、函數或語句中。某些類型的禁止操作要求在源代碼中添加注釋,但大多數不需要。
例如,MISRA 2012規則15.5(上面由消息904報告)是一項建議性規則,建議為每個功能使用一個退出點。可以使用選項-efunc(904,calc)對函數calc禁止顯示此消息,該選項可以作為棉絨注釋放置在項目配置文件或源代碼中。可以以相同的方式抑制來自此功能內的其他消息。評論可以遵循抑制選項,其中可能包括基本原理或形式偏差信息。
PC-lint Plus區分庫代碼(默認情況下包括外部和系統頭,但可以自定義以包括頭和模塊的任何子集)和項目代碼。默認情況下,PC-lint Plus將同時檢查庫代碼和項目代碼是否符合MISRA C2012。通常希望將檢查范圍限制為項目代碼,這很容易在引用au-misra3.lnt文件后,通過使用選項-wlib=4 -wlib=1來重置庫警告級別來完成。也可以使用-elib和+elib選項輕松地為庫代碼啟用或禁用單個消息。
基本類型計算
PC-lint Plus可以解釋使用+f12選項計算復合表達式的最終基本類型所涉及的步驟。消息9903將在完整的表達式之后發出,以說明表達式中涉及的MISRA C 2012基本類型以及它們如何組合形成新的基本類型。
例如,給定:
int32_t f(int32_t a, int16_t b) {
return (b + b) + (a + b);
}
PC-lint Plus發出:
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);
~~~~~~~ ^
為什么左側是signed16,右側是signed32?
用+f12和+e9903處理示例會逐步評估表達式,并在每個步驟中涉及相應的基本類型:
info 9903: (signed16 + signed16) + (signed32 + signed16)
info 9903: (signed16) + (signed32)
info 9903: signed32
說明在右操作數中,sign3232 + signed16產生了與左操作數沖突的sign32。
相關推薦:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn