6

不久前,我为一个大型项目编写了一组X 宏。我需要维护字符串和枚举引用/哈希值/回调函数等的一致列表。这是函数回调的样子

#define LREF_LOOKUP_TABLE_TEXT_SIZE 32
#define _LREF_ENUM_LIST(_prefix,_ref,...) _prefix ## _ ## _ref,
#define _LREF_BASE_STRUCT_ENTRY(_prefix,_ref) .text= #_ref "\0", .position= _LREF_ENUM_LIST(_prefix, _ref)
#define _LREF_FUNCTION_STRUCT_LIST(_prefix,_ref,...) {_LREF_BASE_STRUCT_ENTRY(_prefix,_ref) _prefix ## _ ## _ref ## _callback},

#define _LREF_ENUM_TYPEDEF(_prefix)                                               \ 
    typedef enum _prefix                                                          \  
    {                                                                             \  
        _ ## _prefix ## s(_prefix,_LREF_ENUM_LIST)                                \ 
        _LREF_ENUM_LIST(_prefix,tblEnd)                                           \ 
    } e_ ## _prefix

#define _LREF_LOOKUP_TABLE_TYPEDEF(_prefix, _extras)                              \ 
    typedef struct _prefix ## _lookup                                             \ 
    {                                                                             \ 
        const char text[LREF_LOOKUP_TABLE_TEXT_SIZE];                             \ 
        e_ ## _prefix position;                                                   \ 
        _extras                                                                   \ 
    } _prefix ##_lookup_t

#define LREF_GENERIC_LOOKUP_TABLE(_prefix, _type, _tabledef, _listdef, _extras)   \ 
    _LREF_ENUM_TYPEDEF(_prefix);                                                  \ 
    _LREF_LOOKUP_TABLE_TYPEDEF(_prefix,_tabledef);                                \ 
    _extras                                                                       \ 
    _LREF_LOOKUP_TABLE_DECLARATION(_prefix,_listdef, _type)

#define LREF_FUNCTION_LOOKUP_TABLE(_prefix, _type)                                \ 
    _ ## _prefix ## s(_prefix, _LREF_FUNCTION_DEF )                               \ 
    LREF_GENERIC_LOOKUP_TABLE(    _prefix,                                        \ 
        _type,                                                                    \ 
        void* (*function) (void*);,                                               \ 
    _LREF_FUNCTION_STRUCT_LIST,  )

它位于头文件中,允许我编写如下内容:

#define _cl_tags(x,_)         \
    _(x, command_list)        \
    _(x, command)             \
    _(x, parameter)           \
    _(x, fixed_parameter)     \
    _(x, parameter_group)     \
    _(x, group)               \ 
    _(x, map)                 \
    _(x, transform)

LREF_FUNCTION_LOOKUP_TABLE(cl_tag, static);

这样可以缩短处理程序。例如,加载带有上述标签的配置文件很简单:

for (node_tag = cl_tag_lookup_table; node_tag->position != cl_tag_tblEnd; node_tag++)
{
    if (strcasecmp(test_string, node_tag->text) == 0)
    {
        func_return = node_tag->function((void*)m_parser);
    }
}

我的问题是:我讨厌我必须在我的#define. 我希望能够写#define _cl_tags(_)而不是#define _cl_tags(x,_). 如您所见,x仅用于向下传递前缀(cl_tag)。但这是多余的,因为前缀是初始宏的参数。

如果我的预处理器首先扩展最外层的宏,那么解决这个问题会很容易。不幸的是,GCC 的预处理器在展开最外层的宏之前通过最内层的宏(即参数值)工作。

我正在使用 gcc 4.4.5


澄清 根据 C89(和 C99)标准,以下定义

#define plus(x,y) add(y,x)
#define add(x,y) ((x)+(y))

随着调用

plus(plus(a,b),c)

应该产生

  1. plus(plus(a,b),c)
  2. add(c,plus(a,b))
  3. ((c)+(plus(a,b))
  4. ((c)+(add(b,a))
  5. ((c)+(((b)+(a))))

gcc 4.4.5 给出

  1. plus(plus(a,b),c)
  2. plus(add(b,a),c)
  3. plus(((b)+(a)),c)
  4. add(c,((b)+(a)))
  5. ((c)+(((b)+(a))))
4

2 回答 2

1

Here's what I would do (have done similarly):

Put these in a utility header file:

/*
 * Concatenate preprocessor tokens A and B without expanding macro definitions
 * (however, if invoked from a macro, macro arguments are expanded).
 */
#define PPCAT_NX(A, B) A ## B

/*
 * Concatenate preprocessor tokens A and B after macro-expanding them.
 */
#define PPCAT(A, B) PPCAT_NX(A, B)

Then define this before including your LREF macro header file:

#define LREF_TAG cl_tag

Then, in your LREF macro header file,

#define LREF_PFX(x) PPCAT(LREF_TAG, x)
#define LREF_SFX(x) PPCAT(x, LREF_TAG)

Then replace every instance of _prefix ## foo with LREF_PFX(foo) and foo ## _prefix with LREF_SFX(foo).

(When pasting more than two tokens together, just use nested PPCAT's.)

Your invocation would become

#define LREF_TAG cl_tag
#define _cl_tags(_)        \
    _(command_list)        \
    _(command)             \
    _(parameter)           \
    _(fixed_parameter)     \
    _(parameter_group)     \
    _(group)               \ 
    _(map)                 \
    _(transform)

LREF_FUNCTION_LOOKUP_TABLE(static);
于 2011-02-21T14:17:05.573 回答
0

这个答案只是解决了“澄清”问题。这是正确的行为:

#define plus(x,y) add(y,x)
#define add(x,y) ((x)+(y))

Initial:   plus(plus(a,b),c)
Pass 1a:   plus(add(b,a),c)
Pass 1b:   add(c,add(b,a))
Pass 2a:   add(c,((b)+(a)))
Pass 2b:   ((c)+(((b)+(a))))

规则是每个宏都被非递归地替换一次(嵌套时从最里面开始);然后一个新的通过(又名。“重新扫描”)发生重复相同的过程,这一直持续到一个通过不执行替换。

不过,我不确定您要表达什么观点,因为您对 GCC 和您期望发生的事情都给出了相同的最终结论。

于 2016-03-10T03:04:42.193 回答