续接(94条消息) MISRA-C_爱老虎呦的博客-CSDN博客 ,对于C++的规则不做详细分析
241 S: Use of reinterpret_cast.
class AClass { public: AClass(); explicit AClass(const AClass &ac); AClass& operator=(const AClass &ac); ~AClass(); protected: private: }; class BClass { public: BClass(); explicit BClass(const BClass &ac); BClass& operator=(const BClass &ac); ~BClass(); protected: private: }; static void static_241(void) { AClass *a1 = new AClass(); BClass *b1 = reinterpret_cast <BClass *> (a1); /* not compliant */ }
242 S: Use of const_cast.
static void static_242(void) { const char * c = "sample text"; char *new_c; new_c = const_cast<char *>(c); /* not compliant */ }
243 S: Included file not protected with #define.
Testbed允许的头文件保护形式:
#ifndef + 名称 / #if !defined + 名称 / #ifdef + 名称
#define + 名称 (如果是#ifdef,则添加在 #else中)
#endif
Static_243.h
/************************************************************ * Standard 243 S : Included file not protected with #define. ************************************************************/ /*** #ifndef STATIC_243_H #define STATIC_243_H ***/ /* All declarations and definitions - should be excluded by the preprocessor if the file is included more than once. */ /*** #endif ***/Static_243.c
#include "Static_243.h" /* not compliant */ /************************************************************ * Standard 243 S : Included file not protected with #define. ************************************************************/ static void static_243 ( void ) { ; }
244 S:new or delete overloaded.
new和delete操作符不能重载
class X { public: X(); ~X(); static void *operator new(const UINT_32 size); /* not compliant */ static void operator delete(void*); /* not compliant */ explicit X(const X &xx); X & operator=(const X &xx); protected: private: }; static void static_244(void) { /* ... */ }
245 S: Case statement in nested block.
case和default应在相同的范围内。此外,每个switch语句应至少有一个case子句
typedef enum Choice { first, second, third, fourth } Choice; INT_32 a1 = 1; INT_32 errorflag = 0; static void static_245(Choice chooser) { INT_32 b1 = 2; INT_32 c1 = 1; switch (chooser) { case first: a1 = b1; break; case second: /* flow through to next case label */ case third: a1 = c1; if (a1 == b1) { /* case is not allowed here - not compliant */ case fourth: } break; default: errorflag = 1; break; } }
246 S:Class has missing access specifiers.
class Person { public: Person(); explicit Person(const UINT_32 personNum); explicit Person(const Person &person); Person & operator=(const Person &person); ~Person(); }; /* not compliant */ void static_246(void) { // Statements... }
247 S: Macro definition calls itself.
宏定义不能调用自己(嵌套)
#define COMMON_MACRO(X) (ANOTHER_MACRO(X)) #define SOME_MACRO(X) (NESTED_MACRO1(X)) #define NESTED_MACRO1(X) (NESTED_MACRO2(X)) #define NESTED_MACRO2(X) (COMMON_MACRO(X)) #define ANOTHER_MACRO(X) (X) const SINT_32 s1 = 42; void static_247(void) { SINT_32 myInt = COMMON_MACRO(SOME_MACRO(s1)); /* not compliant */ }
248 S: Divide by zero in preprocessor directive.
预处理器指令有可能导致除零运行时失败
#define DEBUG2 2 #define DEBUG1 0 #define COMMON_MACRO(X,Y) ((X)/(Y)) #if (COMMON_MACRO(DEBUG2,DEBUG1)) == 1 /* not compliant */ /* .... */ #endif void static_248(void) { /* ... */ }
249 S: Operation not appropriate to boolean type.
对布尔类型执行的操作不合适。不适当的运算可参考136s, 389s和402s。这些运算包括 >>,<< 和 后缀++和 --
static void static_249 (void) { BOOL bvar = FALSE; bvar ++; /* not compliant */ }
250 S: Cyclic included file nesting found.
头文件出现循环包含。如 "a.h" 包含 "b.h","b.h" 又包含 "a.h"
251 S: Use of stdio.h instead of iostream.h.
c++程序中不应使用stdio.h(在C函数库中定义的)中的函数声明。应优先使用iostream.h标准头文件。
252 S: Lower case suffix to literal number.
文字后缀应使用大写字母,而不是小写字母,以方便阅读. 不过在Testbed 中这一规则也可以修改放宽为小写字母。
const SINT_64 fr1 = 64l; /* not compliant - looks too much like 641 */ const SINT_64 fr2 = 64L; /* compliant */
253 S: Opposite type operator missing.
254 S: Operator = doesn't return reference to *this.
255 S: Found #if, #ifdef, #else, #elif
一些权威机构不允许使用#if, #ifdef, #else和#elif。
256 S: Procedure exceeds *** source lines of code.
程序的"逻辑行数"超过了限制,Testbed默认为"200"。 修改文件可以计算"物理行数(排除空白行或注释行)"。
257 S: Filename in #include not in < > .
258 S: More than one class in header file.
259 S: Statement not on new line (added by Testbed).
每个语句应放在单独的新行中
void static_259(void) { UINT_32 x1; SINT_32 y1; x1 = 5U; y1 =7; /* not compliant. multiple expression statements on the same line. */ y1 = 3; /* compliant */ }
260 S: No default constructor declared for class.
262 S: Non virtual function redefined.
263 S: Namespace prohibited.
264 S: Class template prohibited.
265 S: Function template prohibited.
266 S: Constructor defined within class declaration.
267 S: Hexadecimal number with lower case char.
16进制尽量不使用小写字母(这个标准也可放宽)
268 S:Empty initialisation exprsn in for loop.
在for循环中不应该使用空的初始化表达式
void static_268(void) { UINT_32 loop = 0U; const UINT_32 max = 10U; for (; loop < max; loop++) /* not compliant */ { /* ... */ } }
269 S: Empty increment exprsn in for loop.
当第2子句中有测试时,for循环中的第3子句不应该为空,因为用户不清楚循环计数器是如何更改的。如果没有循环计数器,应该使用while循环而不是for循环
void static_269(void) { UINT_32 loop; UINT_32 myVar = 0U; const UINT_32 max = 10U; for (loop = 0U; loop < max;) /* not compliant */ { /* ... */ } }
270 S: For loop initialisation is not simple.
for循环中的初始化表达式除了初始化单个for循环参数的值外不会执行任何操作。(这个规则也可放宽)
void static_270(void) { UINT_32 loop; UINT_32 myVar = 0U; const UINT_32 max = 10U; for ( ++myVar, loop = 0U; loop < max; loop++ ) /* not compliant */ { /* ... */ } }
271 S: For loop incrementation is not simple.
for循环中的自增表达式除了使用++或--操作符更改单个循环参数外,不会执行任何操作。循环参数必须是一个简单整数或浮点类型的变量。
void static_271(void) { UINT_32 loop; UINT_32 myVar = 0U; const UINT_32 max = 10U; for (loop = 0U; loop < max; myVar++, loop++) /* not compliant - 2 loops parameters */ { /* ... */ } }
272 S: Found #ifndef (ref. removed).
#ifndef STATIC_272_H #define STATIC_272_H /************************************************ * Standard 272 S : Found #ifndef (ref. removed). ************************************************/ /* not compliant */ #ifndef REPORT_ERRORS #define REPORT_WARNINGS #endif /* All declarations and definitions - excluded by the preprocessor if the file is included more than once. */ #endif
273 S:Found #define.
#ifndef STATIC_273_H #define STATIC_273_H /************************************* * Standard 273 S : Found #define. *************************************/ /* not compliant */ #define MYSQMACRO ((X) * (X)) /* All declarations and definitions - excluded by the preprocessor if the file is included more than once. */ #endif
274 S: Name found with length less than ***.
标识符名称必须不短于指定的预先确定的最小长度。此限制由用户定义。
275 S: Tag missing in struct, union or enum.
在结构、联合或枚举中缺少标记。
省略标记的缺点是以后在程序代码中不能进一步定义结构、联合或枚举。如果没有标记,就不能引用声明,定义也不可能实现
struct /* not compliant - tag missing */ { UINT_32 x_value; UINT_32 y_value; } pos_var; struct Position /* compliant - struct tag included, allowing multiple definitions */ { UINT_32 anon_x_value; UINT_32 anon_y_Value; }; void static_275(void) { pos_var.x_value = 0U; pos_var.y_value = 0U; struct Position first_pos = {1U, 1U}; struct Position second_pos = {2U, 1U}; /* ... */ }
276 S:Case is not part of switch enumeration.
switch语句包含一个case常量表达式。Case值应该仅限于枚举中标识的那些值。
typedef enum Seasons { spring, summer, autumn, winter } Seasons; Seasons season; UINT_32 x1; void static_276(void) { switch (season) { case spring: x1 = 1U; break; case summer: x1 = 4U; break; case autumn: x1 = 7U; break; case 4: /* not compliant */ x1 = 10U; break; default: break; } }
277 S: Switch chooser is not an enumeration.
UINT_32 season; UINT_32 x1; void static_277(void) { switch (season) /* not compliant */ { case 1: x1 = 1U; break; case 2: x1 = 4U; break; case 3: x1 = 7U; break; case 4: x1 = 10U; break; default: break; } }
278 S:Switch has missing or extra cases.
定义的枚举值与switch语句所满足的情况之间不匹配。所有不打算测试每个枚举值的switch语句至少应该包含一个最终的default子句。
typedef enum Seasons { spring, summer, autumn, winter } Seasons; Seasons season; UINT_32 x1; void static_278(void) { switch (season) /* not compliant */ { case spring: x1 = 1U; break; case summer: x1 = 4U; break; case autumn: x1 = 7U; break; default: break; } }
279 S: Try keyword found.
280 S: Catch keyword found.