解析 C++ 预处理器 #if 语句
我有一个带条件编译的 C/C++ 源文件。 在将其交付给客户之前,我想删除大部分 #if 语句,以便我的客户无需担心将正确的 -D 选项传递给编译器。
我已经在 Python 中实现并工作了,但它只能正确处理 #ifdef 和 #ifndef 语句。 我需要添加对 #if 语句的支持,但 #if 的语法要复杂得多。 (例如,您可以使用 &&、||、!、括号、关系运算符、算术等)。
是否有任何现有的开源代码可以解析和评估#if 语句? (最好是Python)。
我所知道的唯一实现是 GCC,对于这项任务来说它太复杂了。
I have a C/C++ source file with conditional compilation. Before I ship it to customers I want to remove most of the #if statements, so that my customers do not need to worry about passing the right -D options to the compiler.
I have this implemented and working in Python, but it only handles #ifdef and #ifndef statements properly. I need to add support for #if statements, but the syntax of #if is much more complex. (E.g. you can use &&, ||, !, brackets, relational operators, arithmetic, etc).
Is there any existing open-source code to parse and evaluate #if statements? (Preferably in Python).
The only implementation I know of is GCC, and that's much too complex for this task.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如KeithB所说,你可以让预处理器为你做这件事。
但是,如果您不想隐藏某些内容(即,条件编译的代码中可能存在您不希望或不允许提供给其他人的内容),则一种更简单的选择是直接将全局包含的标头中正确的
#define
指令。-D
选项As KeithB said, you could just let the preprocessor do this for you.
But if you're not trying to hide things (ie., there may be stuff in the conditionally compiled code that you don't want or aren't permitted to give to some one else) a much simpler option would be to just put the proper
#define
directives in a header that's globally included.-D
options直接通过 C 预处理器并让它完成工作怎么样? 它将摆脱所有这些,因此您可能需要有一个预处理器步骤和一个后预处理器步骤来保护您不想扩展的内容。
How about just passing through the C preprocessor, and letting that do the job. It will get rid of all of them, so you might need to have a pre-preprocessor step and a post pre-processor step to protect things you don't want to be expanded.
不要重新发明轮子,而是下载“unifdef”。 如果您使用某种版本的 Linux,您可能可以找到它的软件包,否则它位于 FreshMeat
Instead of reinventing the wheel, download "unifdef". If you're on some flavour of Linux, you can probably find a package for it, otherwise it's on FreshMeat
你看过 Boost.Wave 吗?
Have you looked at Boost.Wave?
GCC 预处理器通常是一个独立的程序,通常称为
cpp
。 当然,这也可能会剥夺您的评论。The GCC preprocessor is typicallly a stand-alone program, typically called
cpp
. That will probably also strip off your comments, of course.