內容
修改歷史
內容
1 overview
1.1 目的
2 QAC基礎知識
2.1 introduction
2.2 message等級
2.2.1 Level 0: information
2.2.2 Level 1: Obsolete Messages
2.2.3 Level2: Minor
2.2.4 Level 3: Major
2.2.5 Level 4: Local Standards
2.2.6 Level 5: Dataflow Analysis
2.2.7 Level 6: portability
2.2.8 Level7: Undefined behavior
3 meesage help
4 QAC 報issue的點
4.1 LEVEL6: [C99] Trailing comma at the end ofan enumerator-list.
4.1.1 修改的代碼
4.2 level 7: The identifier is reservedfor use by the library.
4.2.1 例子
4.2.2 修改的代碼
4.3 level3: An expression of ‘essentially signed’ type is being used as the operand ofthis bitwise operator.
4.3.1 例子
4.3.2 對應代碼中的message
4.3.3 修改的代碼
4.4 level3: An expression of ‘essentiallyBoolean’ type is being converted to unsigned type on assignment.
4.4.1 修改的代碼
4.5 level2: Loop control variable in this ‘for’ statement is modified in the body of theloop.
4.5.1 例子
4.5.2 對應的代碼中的message
4.5.3 修改的代碼
4.6 level2: This statement has no side-effect – it can be removed.
4.6.1 例子
4.6.2 對應的代碼中的message
4.6.3 修改的代碼
4.7 level2: function with internal linkage, is being defined without a previousdeclaration.
4.7.1 對應的代碼中的message
4.7.2 修改的代碼
4.8 level 2: A function counld probablybe used instead of this function-like macro.
4.8.1 例子
4.9 level 8: initializer pointer to amore heavily qualified type.
4.9.1 例子
4.9.2 對應的代碼中的message
4.9.3 修改的代碼
4.10 level 3: A non-constant expression of‘essentially signed’ type is being converted to unsigned type on assignment.
4.10.1 對應的代碼中的message
4.10.2 修改的代碼
4.11 level 2:Array declared with unknown size.
4.11.1 例子
4.11.2 修改的代碼
4.12 Macro parameter not enclosed in ().
4.12.1 更改的代碼
5 MISRA C 2004
5.1 MISARC introduction
5.2 Rule10.1 The value of an expression of integer type shall not be implicitlyconverted to a different underlying type if: a) it is not a conversion to awider integer type of the same signedness, or b) the expression is complex, orc) the expression is not constant and is a function argument, or d) theexpression is not constant and is a return expression.
5.2.1 例子
5.2.2 根據該條rule修改的代碼
5.3 Rule20.2 The names of standard librarymacros, objects and functions shall not be reused. & Rule 1.2:No reliance shall be placed on undefined or unspecified behaviour.
5.3.1 根據該條rule修改的代碼
5.4 Rule When an array is declared withexternal linkage, its size shall be stated explicitly or defined implicitly byinitialisation.
5.4.1 例子
5.4.2 根據該條rule修改的代碼
5.5 All if … else constructs shall beterminated with an else clause.
5.5.1 例子
5.5.2 根據該條rule修改的代碼
5.6 rule 12.7Bitwise operators shall not be applied to operands whose underlying typeis signed.
5.6.1 例子
5.6.2 根據該條rule修改的代碼
5.7 Rule14.2 All non-null statements shall either have at least one side effect howeverexecuted, or cause control flow to change.
5.7.1 根據該條rule修改的代碼
5.8 Rule 8.1 functions shall haveprototype declaration and the prototype shall be visible at both the functiondefinition and call.
5.9 Rule19.7 A function should be used in preference to a function-like macro.
5.9.1 例子
5.10 Rule 19.4: C macros shall only expandto a braced initialiser, a constant, a string literal, aparenthesisedexpression, a strorage class specifier, or a do-while-zero construct.
5.10.1 例子
5.11 Rule13.6: Numeric variables being used within a for loop for iteration countingshall not be modified in the body of the loop.
5.11.1 例子
5.12 Rule 19.10 in the definition of afunction-like macro each instance of a parameter shall be enclosed inparentheses unless it is used as the operand of # or ##.
5.13 Rule 8.12: When an array is declared with externallinkage, its size shall bestated explicitly or defined implicitly by initialisation.
6 MISAR C 2012

根據QAC檢測報告修改的最新代碼下載鏈接:
https://download.csdn.net/download/Sure_gengjia/12987470
修改后的代碼的QAC報告下載鏈接:
https://download.csdn.net/download/Sure_gengjia/12987399
未修改的代碼的QAC報告下載鏈接:
https://download.csdn.net/download/Sure_gengjia/12987383
關注激活未來公眾號,后臺回復“MISAR C 2012”即可免費獲取“MISRA C_2012 Guidelines for the use of the C language in critical systems.pdf
”檔案下載鏈接,回復“QAC”即可免費獲取QAC-8.1-Win-UsersGuide.pdf使用手冊下載鏈接,

1 overview
1.1 目的
本檔案用于起點開發板的軟體優化說明,
不局限于硬體功能的實作,著眼于實作高質量、優美的軟體,

2 QAC基礎知識
2.1 introduction
QA·C是用于C代碼的深度靜態分析器, QA·C旨在幫助提高軟體開發的質量,
QA·C在逐個檔案和完整專案的基礎上分析源代碼,以識別C語言的危險用法,包含1300多個警告訊息的庫用于突出顯示那些不可移植的、難以維護的、過于復雜或可能引起問題的方式撰寫的原始碼,
該工具還可以識別不符合ISO C90標準(ISO / IEC 9899:1990)或依賴于未指定、未定義的行為的語言用法,該工具會在使用ISO C99標準的特定功能時發出警告,但確實支持其中許多功能的語意,
整個專案的源代碼的警告訊息通過瀏覽器顯示,該瀏覽器對所有源檔案中出現的訊息進行分類和分組,
該工具的其他功能包括:
?提供可量化衡量代碼屬性的代碼度量:基于函式的33個,基于檔案的30個和專案級別的4個;
?功能結構圖,可深入了解控制流程;
?關系圖,用于演示函式呼叫,全域參考和檔案樹;
?關系統計分析,根據行業基準對代碼質量進行全面評估;
?跨模塊分析(CMA)功能,分析功能遞回和各種全域識別符號問題;
?一個基準模塊,可簡化對遺留代碼的修改,
2.2 message等級
QAC中將message分為10個等級,
2.2.1 Level0:information
information等級的警告重要性最低,
| annotations 注釋 | 注釋中的語法錯誤 |
| CMA 資訊 | 跨模塊分析的產生的問題, |
| parsing recovery 分析恢復 | 從決議錯誤恢復的程序中生成的資訊, |
| dataflow recovery | 從內部資料流分析故障中恢復的程序中生成的資訊, |
| sub-message | 額外的背景關系子訊息,用于澄清主要資訊, |
2.2.2 Level 1: Obsolete Messages
保留標記為過時的訊息是為了向后兼容2個產品版本,并將隨后將其洗掉,
2.2.3 Level 2: Minor
級別2資訊識別那些可能會違反編碼標準要求的問題,但這些問題不一定是嚴重的錯誤,級別2資訊組與C語言的不同方面相關聯,
2.2.4 Level 3: Major
Major資訊是那些可能會識別出重大編碼錯誤,例外或問題的資訊,通常認為這些資訊(如7、8和9級)太重要了,以至于沒有充分的理由就無法忽略, 3級資訊組與C語言的不同方面相關聯,
2.2.5 Level 4: Local Standards
級別4資訊識別那些不符合您的編程標準的功能,按照慣例,在安裝QA·C模塊時,會將其他資訊組添加到級別4,其中將包含與編碼標準中特定規則相對應的資訊,
2.2.6 Level5: Dataflow Analysis
此級別的資訊對運行時行為進行分析,確定問題,從嚴重的問題(如未定義的行為)到經常與編碼和邏輯錯誤相關的條件,
| conversion to signed 轉換成有符號 | 轉化成不足夠大以表示期望值的帶符號型別, |
| conversion to unsigned 轉化成無符號 | 轉化為無符號的值,被轉化的值為負數或者是太大無法表示的值, |
| shift operations 移位操作 | 結果不確定或者導致非零位丟失的移位操作, |
| overflow and wraparound 溢位和環繞 | 導致有符號算術值溢位(未定義行為)或無符號算識訓繞的操作 |
| Array 陣列 | 對陣列或指標型別的物件進行的操作導致未定義的行為, |
| Pointer 指標 | 比較或減去指向不同物件的指標, |
| NULL pointer 空指標 | 對NULL指標進行算識訓取消參考操作, |
| Unset data 未設定的資料 | 訪問未設定資料的值, |
| Redundancy 冗余 | 冗余的初始化,運算式或賦值,通常與錯誤的邏輯關聯 |
| Invariant operations 不變的操作 | 總是評估為true或false的運算式,經常與錯誤的邏輯相關聯, |
| control flow 控制流 | 無法訪問的代碼,無限回圈,錯誤的回傳陳述句, |
overflow and wraparound:
當整數值增加到太大而無法存盤在關聯表示中時,就會發生整數溢位或環繞,發生這種情況時,該值可能會變成非常小的值或負數,當結果用于控制回圈,做出安全性決定或確定行為的偏移量或大小(例如記憶體分配,復制,串聯等)時,這對安全性至關重要,
2.2.7 Level 6: portability
| ISO-C90 conformance limits ISO-C90一致性限制 | 識別違反C90標準中定義的轉換限制的資訊,超出這些限制的代碼可能無法移植到符合C90標準的實作中, |
| ISO-C99 conformance limits ISO-C99 一致性限制 | 識別違反C99標準中定義的轉換限制的訊息,超出這些限制的代碼可能無法移植到符合C99標準的實作中, |
| Implementation defined | 代碼結構的行為方式可能會因不同的編譯器而不同, |
| Language extensions | QA·C能夠決議不符合C90或C99標準的代碼結構, |
| ISO C99 language features | 不屬于C90語言定義的C99代碼結構, |
2.2.8 Level 7: Undefined behavior
| CMA undefined behavior | 其行為在語言標準中很明顯地是未定義的且已通過跨模塊分析定義的構造, |
| explicitly undefined | 在語言標準中其行為被明顯地描述為“未定義”的構造(不歸類為依賴資料流) |
| implicitly undefined | 其行為未在語言標準中描述的構造,因此其行為未通過隱式定義(僅), |
3 meesage help
| function-like macro | 用帶括號的引數串列定義的宏,該串列由零個或多個引陣列成, |
4 QAC 報issue的點
4.1 LEVEL6: [C99]Trailing comma at the end of an enumerator-list.
列舉串列末尾的逗號,
ISO C99language features.
在列舉常量串列的末尾有逗號,ISO:C90無法識別此語法,但是ISO:C99允許使用此語法,
forexample:
enum ED { ZERO=0, TWO=2, FOUR=4, };
對應的MISARC 2004 rule:1.1,
4.1.1 修改的代碼
4.1.1.1 drio.h
等,
4.1.1.2 mdio_cfg.h

等,
4.2 level 7: The identifier is reserved for use by the library.
識別符號保留給庫使用,
使用保留名稱去定義一個識別符號不可取,因為該名稱可能與系統庫的現有版本或將來版本中的識別符號沖突,以下識別符號保留:
a) 以2個下劃線開頭;
b) 以1個下劃線緊跟著一個大寫字母開頭;
c) 以1個下劃線和一個其他的識別符號開頭;
4.2.1 例子
int __glob = 0; /* 不符合 */
int _Glob = 1; /* 不符合 */
對應的MISAR C 2004 rule:20.2,
4.2.2 修改的代碼
4.2.2.1 apl_key.h

等,
4.3 level 3: An expression of ‘essentially signed’ type is being used asthe operand of this bitwise operator.
原始有符號型別的運算式用作位運算的運算元,
4.3.1 例子
void foo(int a)
{
a & 1u; /* 不符合 */
a | 1u; /* 不符合 */
~a; /* 不符合 */
a >> 1u; /* 不符合 */
1u << a; /* 不符合 */
}
對應的MISARrule 12.7,
4.3.2 對應代碼中的message
4.3.2.1 drint.c

4.3.3 修改的代碼
4.3.3.1 drint.c

4.4 level3: An expression of ‘essentially Boolean’ type is being converted tounsigned type on assignment.
分配時,“基本布爾”型別的運算式將轉換為無符號型別,
相關的MISARCrule 10.1,
例子:
void foo (unsigned b);
unsigned test (int a)
{
unsigned uia = (a != 0); /*不符合*/
foo(a < 0); /*不符合*/
uia = (a < 0); /*不符合*/
return (a == 0); /*不符合*/
}
對應的MISAR C 2004rule 10.1,
4.4.1 修改的代碼
4.4.1.1 bitop.h

BYTEOP_BIT_READ原宏定義:
#defineBYTEOP_BIT_READ_AT(data, bitidx) (((uint8)(data) & BYTEOP_POWER_2(bitidx)) != 0U)
回傳的值為bool型,true or false
改為:
#defineBYTEOP_BIT_READ_AT(data, bitidx) ((((uint8)(data) & BYTEOP_POWER_2(bitidx)) != 0U) ? (uint8)1U :(uint8)0U)
回傳的值為1U或0U

4.5 level 2: Loop control variable in this ‘for’ statement is modifiedin the body of the loop.
for陳述句的回圈控制變數在回圈體中被修改了,
在此“ for”回圈中,被識別為控制變數的變數在回圈體內進行了修改,
QAC通過檢查 “ for-loop”中的3個運算式的來識別控制變數,通常,它是第二個運算式中的變數,并且是關系運算子的運算元,
在常規的for回圈中,僅在for陳述句的第三個運算式中修改回圈控制變數,回圈中其他位置的修改可能會引起混亂,
4.5.1 例子
extern void foo(void)
{
int j;
for (j = 0; j < 10; j++)
{
++j;
}
}
對應的MISARC2004rule: 13.6,
4.5.2 對應的代碼中的message

4.5.3 修改的代碼
4.5.3.1 drint.c

4.6 level 2: This statement has no side-effect – it can be removed.
陳述句沒有side-effect,可以移除,
可以被優化掉的代碼,
sideeffects: 更改執行環境的動作,訪問易失性物件,修改物件,修改檔案或呼叫執行任何這些操作的函式都是副作用,
4.6.1 例子
void foo( int x )
{
int n = 0;
n == x; /*報警告*/
}
對應的MISAR2004Rule 14.2,
4.6.2 對應的代碼中的message

4.6.3 修改的代碼
4.6.3.1 mdio.c

等,
4.7 level 2: function with internal linkage, is being defined without aprevious declaration.
一個被內部參考的函式在定義時沒有提前的宣告,
在檔案中定義的所有的函式都應在檔案頂部加原型宣告,這很有幫助,這樣可以避免在函式被宣告之前被呼叫的可能性,在檔案開始處的一個位置記錄函式介面的方式也很有用,
例子:
static voidfoo(void) /*沒有提前宣告*/
{
}
對應的MISAR 2004rule 8.1
4.7.1 對應的代碼中的message
4.7.1.1 mdio.c

4.7.2 修改的代碼
4.7.2.1 mdio.c
在檔案頭部增加static uint8 mdio_find_in_input_cfg(uint8port, uint8 pin)函式宣告,
4.8 level 2:A function counld probably be used instead of this function-like macro.
類似函式的宏應當用函式替換,
該宏是運算式的形式,因此可以用一個函式代替,用函式替換類似函式的宏并不總是可行或方便,但是函式通常比宏更安全,因為可以對引數執行型別檢查,
下面這些類似函式的宏會導致該條資訊的產生:
· 有至少一個引數;
· 不包含’#’ or ‘##’運算子;
· 不包含’ {}; ‘或任何關鍵字;
對應的MISARC2004rule: 19.7,
4.8.1 例子
#define M1(a, b, c) ( (a) + (b) + (c)) /* 不符合 */
#define M6(A, B) ((A)[1] +(B)[2]) /* 不符合 */
對應的MISAR C2004 rule 19.7,
需要寫成do-while(0)格式,
為什么寫成do-while(0)格式,請看MISARC2004rule: 19.4 和 19.10,
4.9 level 8:initializer pointer to a more heavily qualified type.
初始化程式指向更嚴格的型別,
指標已使用運算式進行了初始化,該運算式指向更嚴格的型別,
ISO: C要求被初始化物件的型別必須具有初始化者指向的型別的所有的qualifier,因此,不允許使用型別為“ const TYPE *”(指向const TYPE的指標)或“ volatile TYPE *”(指向volatile TYPE的指標)的運算式來初始化型別“ TYPE *”(指向TYPE的指標)的物件;這樣做將違反型別資格限制,
另一方面,使用型別“ TYPE *”的運算式初始化“ const TYPE *”或“ volatile TYPE *”甚至“ const volatile TYPE *”型別的物件是完全合法的,
原文:
A pointer has been initialized with anexpression which points to a more heavily qualified type.
ISO:Crequires that the type pointed to by the initialised object must have all thequalifiers of the type pointed to by the initializer. It is therefore notpermissible to initialize an object of type "TYPE *" (pointer toTYPE) with an expression of type "const TYPE *" (pointer to constTYPE) or "volatile TYPE *" (pointer to volatile TYPE); to do so wouldpermit type qualification restrictions to be violated.
On theother hand, it is perfectly legitimate to initialize an object of type"const TYPE *" or "volatile TYPE *" or even "constvolatile TYPE *" with an expression of type "TYPE *".
4.9.1 例子
extern int *gpi;
extern const int *gpci;
extern volatile int *gpvi;
extern const volatile int * gpcvi;
void test (void)
{
int *xpia = gpi; /* no message */
int *xpb = gpci; /* 不符合 */
int *xpc = gpvi; /* 不符合 */
int *xpd = gpcvi; /* 不符合 */
const int *xpcia = gpi; /* no message */
const int *xpcib = gpci; /* no message */
constint *xpcic = gpvi; /* 不符合 */
constint *xpcid = gpcvi; /* 不符合 */
volatile int *xpvia = gpi; /* no message */
volatile int *xpvib = gpci; /* 不符合 */
volatile int *xpvic = gpvi; /* no message */
volatile int *xpvod = gpcvi; /* 不符合 */
const volatile int *xpcvia = gpi; /* no message */
const volatile int *xpcvib = gpci; /* no message */
const volatile int *xpcvic = gpvi; /* no message */
const volatile int *xpcvid = gpcvi; /* no message */
}
4.9.2 對應的代碼中的message
4.9.2.1 drio_cfg.c

4.9.3 修改的代碼
4.9.3.1 drio_cfg.h
typedef uint8 * DrRegType;
改為
typedef uint8 * DrRegType;

因為DDRE的定義和使用為:


4.10 level 3: A non-constantexpression of ‘essentially signed’ type is being converted to unsigned type onassignment.
“原始有符號的”型別的非常量的運算式轉換為無符號型別,
void foo(unsigned b);
unsignedtest(short a)
{
unsigned uia = a; /* 不符合 */
foo( a + 1 ); /* 不符合 */
uia = (a – 1); /* 不符合 */
return a; /* 不符合 */
}
4.10.1 對應的代碼中的message

4.10.2 修改的代碼
4.10.2.1 drio.c

4.11 level2: Array declared with unknown size.
大小未知的陣列宣告,
陣列宣告時型別不完整,
僅當陣列具有外部連接時才允許這樣做,一些編碼標準不贊成這種做法,因為它阻止工具檢查陣列下標操作的有效性,
4.11.1 例子
extern int xa[10];
extern int xb[]; /* 警告 */
extern void foo(void)
{
xa[10] = 0; /* 警告,越界 */
xb[10] = 0; /* 沒有警告,陣列大小未知 */
}
對應的MISAR C2004 rule: 8.12,
4.11.2 修改的代碼
4.11.2.1 drio_cfg.h

4.12 Macroparameter not enclosed in ().
宏引數未包含在()中,
在某些宏中,將引數括在替換串列的括號中這一操作很重要,并非所有的宏或所有引數都是如此,這取決于宏中如何使用引數,在某些型別的運算式中,將宏引數用作運算元時就會出現問題,考慮以下示例:
#define SQUARE(A) (A * A) /* 報警告 */
extern int foo(int n)
{
int r;
r = SQUARE(n);
r = SQUARE(n + 2); /* 報警告 */
return r;
}
對應MISARC2004rule19.10,
4.12.1 更改的代碼
4.12.1.1 typedef.h

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/188476.html
標籤:其他
