MISRA-C 2004 规则解读(81S-100S)


81 S:Use of trigraphs. C语言不允许使用三字母词,编译不能通过:

void static_81(void)
??<   /* not compliant */
   /* ... */ 
}

82 S:Use of wide string literal. 避免使用宽字符类型:

void static_82(void)
{
   wchar_t WideChar = L'1'; /* not compliant */

   /* ... */ 
}

83 S:Octal number found. 由于8进制数以0开头,容易造成混淆,所以在代码中应当避免使用八进制计数:

code[1] = 109   /* set to decimal 109 */ 
code[2] = 100   /* set to decimal 100 */ 
code[3] = 052   /* incorrect - set to decimal 42 */ 
code[4] = 071   /* incorrect - set to decimal 57 */ 

84 S:Register variable declared. 避免使用Register关键字修饰变量:

void static_84(void)
{
   register SINT_32 ri = 0; /* not compliant */

   /* ... */ 
}

85 S:Incomplete initialisation of enumerator. 初始化枚举类型时候,或者全部默认,或者全部初始化,或者只初始化第一个值,其他的初始化方式是不妥的:

enum (x,y,z)        /*GOOD - no explicit initialisation*/ 
enum (x=1,y,z)      /*GOOD - first value only*/ 
enum (x=2,y=3,z=4)  /*GOOD - all values set*/ 
enum (x,y,z=1)      /*BAD*/ 

86 S:Attempt to define reserved word.避免define保留字段。

87 S:Use of pointer arithmetic. 避免对指针使用数学运算:

void static_87(void)
{
   UINT32_t w;
   UINT32_t array[5];
   UINT32_t * p1_ptr;

   p1_ptr = array;
   w = *(p1_ptr + 8);  /* not compliant */
}

88 S:Procedure is not pure assembler. 避免C语言与汇编语言混合使用。

89 S:char type not signed or unsigned. char类型建议定义为signed或者unsigned类型。

90 S:Basic type declaration used. 对于不同编译器基础类型的宽度可能不同,该建议在程序编写前做如下定义:

typedef char                            CHAR_t;
typedef unsigned char                   UCHAR_t;
typedef unsigned short                  UINT16_t;
typedef unsigned int                    UINT32_t;
typedef signed char                     SCHAR_t;
typedef signed short                    SINT16_t;
typedef signed int                      SINT_32_t;
typedef float                           FLOAT_32_t;
typedef double                          FLOAT_64_t;
typedef enum { FALSE = 0, TRUE  = 1}    BOOL_t;

91 S:Name redeclared in another C name space (MR). 尽管C语言允许在不同的作用域定义相同的变量名,为避免混淆建议定义不重名变量(在复杂系统中该条很难实现)。

扫描二维码关注公众号,回复: 4115755 查看本文章
struct s_91
{
  UINT32_t s_91;  /* not compliant */
  UINT32_t u_1;
};


UINT32_t static_91 (void)
{
   UINT32_t var_1;

   static_91:      /* not compliant */
   var_1 = 1;
   return (var_1);
}

92 S:Duplicate use of a name in an enumeration.针对枚举类型,建议不使变量重名:

void static_92 (void)
{
   enum Name_type { e1, duplicate } EnumVar;

   EnumVar = e1;
   /* ... */ 
}

93 S:Value is not of appropriate type.强制要求类型检查,不同类型之间转换需要显示转换:

void STATIC_93(void)
{
   FLOAT_32 fl;

   fl = 2.0; /* not compliant - Requires explicit assignment of 2.0F */ 

   /* ... */ 
}

94 S:Casting operation on a pointer. 避免对指针进行强制类型转换(通过修改测试软件配置,可以允许指针在同类型之间转换):

static void static_94(UINT_32 * p1_ptr)
{
   FLOAT_32 v_1;

   v_1 = (FLOAT_32) p1_ptr;
}

95 S:Casting operation to a pointer. 给指针赋值安全的方式是通过获取对象地址,强转赋值的方式有风险:

struct Astruct { UINT_32 a; };

void static_95 (UINT_32 *intptr)
{
  struct Astruct *Astructptr;
  Astructptr = (struct Astruct *) intptr; /* not compliant */
}

96 S:Use of mixed mode arithmetic. 不同类型的变量进行数学运算,需要进行强制转换:

static void static_96(void)
{
  INT32_t i32 = 10;
  FLOAT64_t f64 = 20.5;
  FLOAT32_t f32 = 2.0F;

  f64 = i32 + f64;  /* not compliant */
  f64 = f64 * f32;  /* compliant */
}

97 S:Use of redundant cast. 避免使用不必要的强制类型转换:

void static_97(void)
{
   SINT32_t sx,sy = -10;

   sx = (SINT32_t) sy + 1; /* not compliant */
}

98 S:Actual and formal parameters inconsistent (MR).函数调用时传入的参数类型与定义时不一致。

99 S:Function use is not a call.函数缺少声明。

100 S:#include filename is non conformant. include语句中禁止使用 字符’ \ /*。

#include <\ctype.h> /* not compliant */

猜你喜欢

转载自blog.csdn.net/u013992766/article/details/51172494
今日推荐