我有一个包含以下内容的文件:
#define str(s) s
#define xstr(s) #s
#define foo 4
#define first str(foo)
#define second xstr(foo)
当我将其传递给 boost::wave 时,宏映射包含以下内容:
first=str(foo)
foo=4
second=xstr(foo)
str(s)=#s
xstr(s)=str(s)
我怎样才能让宏完全展开,这样:
first="foo"
second=4
我认为 boost::wave 支持宏重新扫描和查找。是不是我遗漏了什么,或者我是否需要对处理 Hook 做一些事情才能进行扩展?我的意思是,我可以编写代码来下降树,但如果我错过了一些重要的事情而不必做这项工作当然会很好。
最佳答案
来自 n.m. 的线索很重要。宏可以随时定义和取消定义,因此只有使用实例才是相关的。
这比我预期的要容易得多。如果我在头文件中有类似这样的内容:
#define str(s) #s
#define xstr(s) str(s)
...
#define MAJORVER 1
#define MINORVER 0
#define MAJORBUILD 0
#define MINORBUILD 1
#define FULLBUILDSTRING xstr(MAJORVER) "." \
xstr(MINORVER) "." \
xstr(MAJORBUILD) "." \
xstr(MINORBUILD)
FULLBUILDSTRING 宏将只评估声明的字符串。但是,如果我有一些直接使用宏的代码,例如在
static char const full_build_string[] = FULLBUILDSTRING;
当 Wave 处理它时,它将评估为以下内容:
static char const full_build_string = "1" "." "0" "." "0" "." "1";
如果您需要在处理完成后查看宏是如何定义的,您总是可以这样做(这取自 Wave 驱动程序代码):
auto end = ctx.macro_names_end();
for (auto it = ctx.macro_names_begin(); it != end; ++it) {
typedef std::vector<context_type::token_type> parameters_type;
bool has_pars = false;
bool predef = false;
context_type::position_type pos;
parameters_type pars;
context_type::token_sequence_type def;
if (ctx.get_macro_definition(*it, has_pars, predef, pos, pars, def)) {
// if has_pars is true, you can iterate through pars to see
// parameters for function macros
// iterate through def to see the macro definition
}
}
当然,宏是以非扩展形式存储的。
(n.m.,如果你写一个答案,我会接受)
关于c++ - 我怎样才能从 boost::wave 获得完整的、扩展的、字符串化的宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34460707/