MISRA-C 2004 规则解读(141S-160S)


141 S:Incomplete structure or class declaration. 结构体或者类声明不完整:

struct atagname;/* not compliant */
struct atagname(UINT32_t A, UINT32_t B);/*compliant */

142 S:Parameter list declarations are inconsistent. 函数某些参数缺少类型或者名称:

void static_142(UINT32_t , UINT16_t p_2)  /* not compliant */
{
    /* ... */ 
}

143 S:Curly brackets used in expression. 表达式中使用大括号会对某些编译器造成干扰,应当避免使用:

UINT32_t static_143(UINT32_t p_1)
{
   UINT32_t result;
   result = p_1{};  /* not compliant */
   return result;
}

144 S:Floating point not permitted. 避免使用浮点类型,尤其是对浮点类型的操作,应为对浮点类型的操作可能使精度降低:

FLOAT32_t static_144(FLOAT32_t p_1,FLOAT32_t p_2)  /* not compliant */
{
   FLOAT32_t result;      /* not compliant */
   result = p_1 + p_2;
   return result;
}

145 S:#if has invalid expression. #if语句后应当为BOOL类型,如下的表达式写法对有些编译器会造成误解:

#if MACRO_NAME JUNK_NAME

146 S:#define with empty parameter list.注意使用#define定义表达式,参数为空的情况,如下给出了建议的写法:

#define Macro() some_macro_body /* not compliant */
#define Macro some_macro_body   /* compliant */

147 S:Spurious CHAR_tacters after preprocessor directive. 避免出现不必要的字符,注意下面情况中结尾的分号是不必要的:

#ifdef STATIC_147

  typedef unsigned CHAR_t UCHAR_t;

#endif ; /* not compliant */

148 S:No return type for function/procedure. 函数缺少返回类型,vs2008下编译失败:

static_148(UINT32_t p1);  /* not compliant */

static_148(UINT32_t p1)   /* not compliant */
{
     /* ... */ 
}

149 S:Reference parameter to procedure is reassigned. 传入函数的引用参数在函数内被重新赋值:

CHAR_t *static_149( CHAR_t *p1);

void test_149(void)
{
  CHAR_t *a = "Query";
  CHAR_t *b;
  b = static_149(a);
}

/********************************************************
 * Standard 149 S : Reference parameter to procedure is reassigned.
 ********************************************************/ 
CHAR_t *static_149( CHAR_t *p1 )
{
  static CHAR_t *newA = "Response";

  p1++;        /* not compliant */
  p1 = p1+1;   /* not compliant */
  p1 = newA;   /* not compliant */

  return p1;
}

150 S:Volatile or const used in function type. 对函数的返回值使用Volatile 或者 const修饰是无意义的:

volatile UINT32_t STATIC_150(void);  /* not compliant */

/***********************************************************
 * Standard 150 S : Volatile or const used in function type.
 ***********************************************************/ 

volatile UINT32_t STATIC_150(void)  /* not compliant */
{
  volatile UINT32_t r;
  /* ... */ 
  return r;
}

151 S:Use of global variable in macro definition. 避免在宏定义中使用全局变量:

UINT32_t globvar;

#define STATIC_151 globvar  /* not compliant */

152 S:Use of a comment in a macro definition.宏定义语句中不建议增加注释,注释内容可以写在语句上一行:

#define STATIC_152 3 /* not compliant comment */ 

153 S:Use of a comment in a pre-processor directive.同147S,在编译器不解析的地方不建议出现注释行:

#ifdef STATIC_153 /* A Comment  - not compliant */ 
  /* ... */ 
#endif /* Another Comment - not compliant */ 

154 S:Nested header files found. 避免嵌套包含头文件,需要的头文件建议直接包含:

Static_154_1.h
#include "c_standards.h"

Static_154.h
#include "Static_154_1.h"

Static_154.c
#include "Static_154.h"  /* not compliant,proposal to included c_standards.h directly*/

155 S:Comments between preprocessor directives. 避免用#ifdef语句来写注释,防止#ifdef语句中的变量被定义后,注释被编译报错:

#ifdef STATIC_155
  this acts as a comment, as STATIC_155 is undefined.
#endif

156 S:Use of ‘defined’ keyword in macro body. 宏定义中避免使用’defined’关键字:

#define STATIC_156(x) defined(x)  /* not compliant */

157 S:Modification of string literal. string类型存储在内存的只读块,改变string类型的字符是危险的:

void static_157(void)
{
  CHAR_t *c = "1234567890";

  c[7] = '0';   /* not compliant */
  *(c+6) = '1'; /* not compliant */
}

158 S:Expression with more than N subconditions.并列条件超过N时报警,N值是可配置的:

void static_158(void)
{
  INT32_t a1,b1,c1,d1,e1;
  INT32_t a2,b2,c2,d2,e2;
  INT32_t a3,b3,c3,d3,e3;

  if  ((a1==0) && (b1==0) && (c1==0) && (d1==0) && (e1==0)
    && (a2==0) && (b2==0) && (c2==0) && (d2==0) && (e2==0)
    && (a3==0) && (b3==0) && (c3==0) && (d3==0) && (e3==0)) /* not compliant */
    {
      /* ... */ 
    }
}

159 S:Comparing pointer with zero or NULL.指针与NULL比较时,建议对NULL进行类型转换与指针类型一致:

CHAR_t *message;
if (message != (CHAR_t *)(NULL) ) /* compliant */ 
if (message != NULL )           /* not compliant */ 

160 S:Loop in macro expansion. 避免在宏定义中出现循环,下面的写法除了增加代码行数外是无意义的:

#define A B
#define B A

#define C D
#define D E
#define E C

猜你喜欢

转载自blog.csdn.net/u013992766/article/details/51172507