1

MISRA 19.10:

在类函数宏的定义中,参数的每个实例都应括在括号中,除非它用作# 或## 的操作数。

我有一个这样定义的结构:

typedef struct
{
  SUint_t        affValueIndex;
  const SFloat_t affLoSaturation;
  const SFloat_t affHiSaturation;
  const SFloat_t affCoeffTab[SDEF_ANALOG_FLOAT_FILTER_LEN];
  SFloat_t       affValueTab[SDEF_ANALOG_FLOAT_FILTER_LEN];
} SAnalogFloatFilter_t;

然后我做:

#define DEF_ANALOG_FILTER_COEFF_LIST {0.0287421759f, \
                                      0.1225311874f, \
                                      0.2123775504f, \
                                      0.2726981726f, \
                                      0.2123775504f, \
                                      0.1225311874f, \
                                      0.0287421759f}

#define SMK_ANALOG_FLOAT_FILTER_HANDLE(__name__, __coeff_list__, __hi_sat__, __low_sat__) \
SAnalogFloatFilter_t (__name__)[1] = {{0u, (__low_sat__), (__hi_sat__), __coeff_list__ , {(SFloat_t)0,}}}

static SMK_ANALOG_FLOAT_FILTER_HANDLE(CurrMonFilter,
                                      DEF_ANALOG_FILTER_COEFF_LIST,
                                      (SFloat_t)DEF_ANALOG_FILTER_HI_SAT,
                                      (SFloat_t)DEF_ANALOG_FILTER_LO_SAT);

问题是 __ coeff_list __ 参数不能有括号,因为它然后扩展为 ({...}) (一个数组初始化器),gcc 抱怨:

foo.c:45:9: error: statement-expressions are not allowed outside functions nor in template-argument lists

是否有一种聪明的方法来保持 MISRA 19.10 检查并在 SMK_ANALOG_FLOAT_FILTER_HANDLE 宏中有一个数组初始化程序?

4

1 回答 1

0

这为我编译:

typedef int SUint_t;
typedef float SFloat_t;

#define SDEF_ANALOG_FLOAT_FILTER_LEN 8

typedef struct
{
  SUint_t        affValueIndex;
  const SFloat_t affLoSaturation;
  const SFloat_t affHiSaturation;
  const SFloat_t affCoeffTab[SDEF_ANALOG_FLOAT_FILTER_LEN];
  SFloat_t       affValueTab[SDEF_ANALOG_FLOAT_FILTER_LEN];
} SAnalogFloatFilter_t;

#define DEF_ANALOG_FILTER_COEFF_LIST (const SFloat_t[SDEF_ANALOG_FLOAT_FILTER_LEN]) \
                                     {0.0287421759f, \
                                      0.1225311874f, \
                                      0.2123775504f, \
                                      0.2726981726f, \
                                      0.2123775504f, \
                                      0.1225311874f, \
                                      0.0287421759f}

#define SMK_ANALOG_FLOAT_FILTER_HANDLE(__name__, __coeff_list__, __hi_sat__, __low_sat__) \
SAnalogFloatFilter_t (__name__)[1] = {{0u, (__low_sat__), (__hi_sat__), (__coeff_list__) , {(SFloat_t)0,}}}

#define DEF_ANALOG_FILTER_HI_SAT 0.0f
#define DEF_ANALOG_FILTER_LO_SAT 0.0f

static SMK_ANALOG_FLOAT_FILTER_HANDLE(CurrMonFilter,
                                      DEF_ANALOG_FILTER_COEFF_LIST,
                                      (SFloat_t)DEF_ANALOG_FILTER_HI_SAT,
                                      (SFloat_t)DEF_ANALOG_FILTER_LO_SAT);

在 Ubuntu 12.04.03 LTS 上使用以下命令进行测试:

gcc-4.6 -c -o foo.o foo.c

gcc-4.8 -c -o foo.o foo.c
于 2014-07-01T04:37:54.147 回答