MISRA-C 2004 规则解读(1S-20S)

来源:互联网 发布:java poi 设置字体 编辑:程序博客网 时间:2024/05/17 18:28

1 S:Procedure name reused.函数名与变量名称重复。

2 S:Label name reused.局部变量重命名。虽然作用域不同能编译通过,但是容易造成混淆。

3 S:More than N executable reformatted lines in file.规定单个文件代码行数不能超过NN值可配置。

4 S:Procedure exceeds N reformatted lines.规定单个函数的代码行数不能超过NN值可配置。

5 S:Empty then clause.空语句有可能是开发人员忘记实现某个功能而留下的坑。所以在确实是空的地方这样写比较合适:

    UINT_32 value_x = 1u;    if (value_x == 0u){        value_x = value_x + 1u;    }     else{        /*do nothing*/    }

6 S:Procedure pointer declared.避免使用函数指针:

#include "c_standards.h"void foo(UINT_32 p_1, UINT_16 p_2){ /* ... */ }/******************************************************** * Standard 6 S : Procedure pointer declared. ********************************************************/ void static_6(void){  void (*proc_pointer) (UINT_32 p_1, UINT_16 p_2) = foo; /* 不建议使用函数指针 */  proc_pointer(1u, 1);}

7 S:Jump out of procedure. 避免使用非局部跳转语句,如setjmp和longjmp函数。

8 S:Empty else clause. 见【5S】。

9 S:Assignment operation in expression. 避免在表达式中使用赋值语句,举个栗子:

BOOL static_9(BOOL test){   BOOL result,flag;   result = ( flag = test ); /* not compliant */   return result;}

10 S:空

11 S:No brackets to loop body (added by Testbed). 避免省去大括号,举个栗子:

for (i = 0; i < 10; i = i + 1) j--; /* not compliant */if (i > 0){  i = i - 1;}else  i = i + 1;          /* not compliant */

12 S:No brackets to then/else (added by Testbed). 见【11S】。

13 S:goto detected.避免使用goto语句。

14 S:Procedural parameter declared. 避免使用函数作为函数的参数。

/******************************************************** * Standard 14 S : Procedural parameter declared. ********************************************************/ void static_14(void (*p_proc_pointer)(INT_32 p1) ){   p_proc_pointer = test_14s;}

15 S:Anonymous field to structure. 避免使用无名称的结构体,这通常出现在结构体嵌套使用的场景。

/******************************************************** * Standard 15 S : Anonymous field to structure. ********************************************************/ struct s_15 { UINT_32 xs;              struct {UCHAR ac, ab;};  /* not compliant */            };

16 S:Multiple labels declared. 避免在文件的同一处使用多个标签。

17 S:Code insert found. 避免在代码中嵌套出现汇编语句,大多数编译器是编译不通过的。

18 S:More than N parameters in procedure. 规定单个函数入参个数不能超过NN值可配置。

19 S:Procedural para used in an uncalled procedure. 避免函数作为参数的情况,即使该函数未被调用,同【14S】。

20 S:Parameter not declared explicitly. 避免入参无变量类型,大多数编译器是编译不通过的。

0 0
原创粉丝点击