解析 C++ 预处理器 #if 语句

发布于 2024-07-09 01:07:55 字数 314 浏览 5 评论 0原文

我有一个带条件编译的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夕嗳→ 2024-07-16 01:07:55

正如KeithB所说,你可以让预处理器为你做这件事。

但是,如果您不想隐藏某些内容(即,条件编译的代码中可能存在您不希望或不允许提供给其他人的内容),则一种更简单的选择是直接将全局包含的标头中正确的 #define 指令。

  • 您的客户不需要担心 -D 选项
  • 您不必在构建过程中执行一些自定义步骤
  • 您提供给客户的代码不会是半混淆的
  • 您不需要引入错误,因为该工具的功能与 C 预处理器略有不同,
  • 您无需维护某些自定义工具

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.

  • your clients don't need to worry about -D options
  • you don't have to have some custom step in your build process
  • the code you give your clients isn't potentially semi-obfuscated
  • you don't introduce bugs because the tool does things subtly different from the C preprocessor
  • you don't have to maintain some custom tool
红颜悴 2024-07-16 01:07:55

直接通过 C 预处理器并让它完成工作怎么样? 它将摆脱所有这些,因此您可能需要有一个预处理器步骤和一个后预处理器步骤来保护您不想扩展的内容。

  1. 将所有 #include 更改为 @include
  2. 通过预处理器传递文件
  3. 将 @include 更改回 #include

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.

  1. Change all #include to @include
  2. Pass file through preprocessor
  3. Change @include back to #include
染年凉城似染瑾 2024-07-16 01:07:55

不要重新发明轮子,而是下载“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

秋心╮凉 2024-07-16 01:07:55

你看过 Boost.Wave 吗?

Have you looked at Boost.Wave?

熊抱啵儿 2024-07-16 01:07:55

GCC 预处理器通常是一个独立的程序,通常称为cpp。 当然,这也可能会剥夺您的评论。

The GCC preprocessor is typicallly a stand-alone program, typically called cpp. That will probably also strip off your comments, of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文