我怎么能产生从C code的#define值的列表? [英] How can I generate a list of #define values from C code?
问题描述
我有code,有很多复杂的#define错误codeS的是不容易脱code,因为它们是通过嵌套几个层次。
I have code that has a lot of complicated #define error codes that are not easy to decode since they are nested through several levels.
有没有优雅的方式,我可以得到#定义的列表,它们的最终数值(也可能是任何其他)?
Is there any elegant way I can get a list of #defines with their final numerical values (or whatever else they may be)?
作为一个例子:
<header1.h>
#define CREATE_ERROR_CODE(class, sc, code) ((class << 16) & (sc << 8) & code)
#define EMI_MAX 16
<header2.h>
#define MI_1 EMI_MAX
<header3.h>
#define MODULE_ERROR_CLASS MI_1
#define MODULE_ERROR_SUBCLASS 1
#define ERROR_FOO CREATE_ERROR_CODE(MODULE_ERROR_CLASS, MODULE_ERROR_SUBCLASS, 1)
我将有大量的匹配误差类似的#define语句_ [\\ w _] +,我想一一列举,让我总是有错误codeS的最新列表,该方案可以输出。我需要的数值,因为这是所有的程序将打印出来(不,它不是打印出一个字符串的选项,而不是)。
I would have a large number of similar #defines matching ERROR_[\w_]+ that I'd like to enumerate so that I always have a current list of error codes that the program can output. I need the numerical value because that's all the program will print out (and no, it's not an option to print out a string instead).
有关海湾合作委员会或其他编译器的建议将是有益的。
Suggestions for gcc or any other compiler would be helpful.
推荐答案
我认为解决的办法是@nmichaels和@ aschepler的答案组合。
I think the solution is a combo of @nmichaels and @aschepler's answers.
使用gcc的-dM选项获取宏的列表。
使用Perl或awk的或任何从该名单中创建两个文件:
Use gcc's -dM option to get a list of the macros. Use perl or awk or whatever to create 2 files from this list:
1)Macros.h,只包含的#define。
1) Macros.h, containing just the #defines.
2)codes.c,其中包含
2) Codes.c, which contains
#include "Macros.h"
ERROR_FOO = "ERROR_FOO"
ERROR_BAR = "ERROR_BAR"
(即:提取每个的#define ERROR_x
与宏观和串线
现在运行 GCC-E codes.c
。与所有的宏展开这应该创建一个文件。输出应该看起来像
now run gcc -E Codes.c
. That should create a file with all the macros expanded. The output should look something like
1 = "ERROR_FOO"
2 = "ERROR_BAR"
的我没有GCC,得心应手,所以没有测试过这... 的
这篇关于我怎么能产生从C code的#define值的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!