有必要的部分来制定我的问题。下面是MyError.h
头文件的内容。
我的错误.h
###########################
# myError.h
###########################
1 typedef enum
2 {
3 MySuccess = 0x00000000,
4 MyError1 = 0x00000001,
5 MyError2 = 0x00000003,
6 MyForce32 = 0x7FFFFFFF
7 } MyError;
8 #define PROPAGATE_ERROR_FAIL_MY_1(_err) \
9 do { \
10 e = (_err); \
11 if (e != MySuccess) \
12 { \
13 MY_UTILS_LOG_ERROR(e, __FILE__, __FUNCTION__, __LINE__, true, 0); \
14 goto fail; \
15 } \
16 } while (0)
17 #define MY_UTILS_LOG_ERROR(_err, _file, _func, _line, _propagating, _format, ...) \
18 do { \
19 MyUtilsLogError(MY_UTILS_ERROR_TAG, MY_UTILS_ERROR_PATH, \
20 (_err), (_file), (_func), (_line), \
21 (_propagating), (_format), ##__VA_ARGS__); \
22 } \
23 while (0)
24 void MyUtilsLogError(const char* tag, const char* path, MyError e, const char* file, const char* func,
uint32_t line, bool propagating, const char* format, ...)
//Here MyError is passed just to print the String for Error for example if we pass MyError1 then string MyError1 will be printed in logs on console.
以下是MyError.c
文件中的必需部分,其中仅包含上述头文件并PROPAGATE_ERROR_FAIL_MY_1
在 API 中调用宏。
我的错误.c
#include "myerror.h"
static MyError foo(uint32_t x, uint32_t y) {
if (x==y) {
return MySuccess;
} else {
return MyError1;
}
}
static MyError fooCaller(void) {
MyError e = MySuccess;
uint32_t x = 1U, y = 1U;
PROPAGATE_ERROR_FAIL_MY_1(foo(x,y)); //This is where I get all kind of weird MISRA violation [1][2].
fail:
return e;
}
注意:仅供参考 MyUtilsLogError() 只是一个 API,它有助于在控制台上转储日志。
在myError.c
文件中,我看到以下 MISRA 2012 违规:
[1]:misra_c_2012_rule_10_4_violation:左侧操作数“e”(枚举)的基本类型与右侧操作数“MySuccess”(布尔值)的基本类型不同。
[2]:misra_c_2012_rule_11_9_violation:字面量“0”不得用作空指针常量。
我不明白为什么 MISRA 报告 10.4 违规,即使我在myErro.h
文件中的第 11 行比较相同的枚举类型?
任何线索为什么在这里报告 10.9 违规?宏不适用于 MISRA 吗?