misra 19.10 with array initialiser

时间:2014-06-19 14:43:31

标签: c misra

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 __参数不能有一个括号,因为它扩展为({...})(一个数组initialilser),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宏中有一个数组初始化器?

1 个答案:

答案 0 :(得分: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