MISRA-C++规则8-5-2要求用正确的大括号初始化C++结构。我有一个带有联合的结构,但是我找不到正确的大括号组合来满足这个规则。我不确定我的代码是不正确的还是来自静态代码分析器工具的错误警告。
结构是这样的:
typedef struct XMC_VADC_RESULT_CONFIG
{
union
{
struct
{
uint32_t : 16;
uint32_t data_reduction_control : 4; /**< Configures the data reduction stages */
uint32_t post_processing_mode : 2; /**< Result data processing mode. Uses @ref XMC_VADC_DMM_t
For normal operation select
XMC_VADC_DMM_t::XMC_VADC_DMM_REDUCTION_MODE
and data_reduction_control as 0*/
uint32_t : 2;
uint32_t wait_for_read_mode : 1; /**< Allow the conversion only after previous results are read*/
uint32_t part_of_fifo : 2; /**< Make the result register a part of Result FIFO? */
uint32_t : 4;
uint32_t event_gen_enable : 1; /**< Generates an event on availability of new result. */
};
uint32_t g_rcr;
};
} XMC_VADC_RESULT_CONFIG_t;这是我的初始化代码:
const XMC_VADC_RESULT_CONFIG_t resultConfig =
{
{
{
.data_reduction_control = 0U, // No Accumulation
.post_processing_mode = static_cast<uint32_t>(XMC_VADC_DMM_REDUCTION_MODE),
.wait_for_read_mode = 0U, // Disabled
.part_of_fifo = 0U, // No FIFO
.event_gen_enable = 0U // Disable Result event
}
}
};我还尝试删除了一组花括号,但没有帮助。花括号的正确数量是多少?
发布于 2020-09-29 17:16:28
正确的,符合规范的大括号位置应该是const XMC_VADC_RESULT_CONFIG_t resultConfig = { 0u };
.name语法)是一个只存在于C99或更高版本的C语言。它们在MISRA-C:2004中是不允许的,但在MISRA-C:2012中是不允许的(有一些特殊的规则)。在MISRA中,它们是最近才引入的,在不允许在MISRA中使用的C++版本中,符合MISRA的applications.
union类型双关通常在任何MISRA中都不允许,特别是在未定义行为的C++中。这条规则在C中有一些例外,但在C++中不存在。
很差
摘要:您不能在任何形式的MISRA应用程序中使用此代码。删除联合和位域,并将其替换为按位运算符和位掩码。
发布于 2020-09-29 16:59:23
C++没有designated initializers until C++20,所以你得去掉它们。
const XMC_VADC_RESULT_CONFIG_t resultConfig =
{
{
{
0U, // No Accumulation
static_cast<uint32_t>(XMC_VADC_DMM_REDUCTION_MODE),
0U, // Disabled
0U, // No FIFO
0U // Disable Result event
}
}
};https://stackoverflow.com/questions/64114113
复制相似问题