是否有一个C pre-处理器,消除基于定义的值#IFDEF块/未定义? [英] Is there a C pre-processor which eliminates #ifdef blocks based on values defined/undefined?

查看:124
本文介绍了是否有一个C pre-处理器,消除基于定义的值#IFDEF块/未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想是不是一个标准的C pre处理器,但是这会从什么地方接受它的变化 - 它的宏定义了一个规范 - 很可能是通过-DNAME1和-UNAME2选项命令行,并随后将消除死code。

What I'd like is not a standard C pre-processor, but a variation on it which would accept from somewhere - probably the command line via -DNAME1 and -UNAME2 options - a specification of which macros are defined, and would then eliminate dead code.

这可能是更容易理解了我一些例子是后:

It may be easier to understand what I'm after with some examples:

#ifdef NAME1
#define ALBUQUERQUE "ambidextrous"
#else
#define PHANTASMAGORIA "ghostly"
#endif

如果命令用'-DNAME1运行,输出将会是:

If the command were run with '-DNAME1', the output would be:

#define ALBUQUERQUE "ambidextrous"

如果命令用'-UNAME1运行,输出将会是:

If the command were run with '-UNAME1', the output would be:

#define PHANTASMAGORIA "ghostly"

如果该命令用既不选项运行时,输出将是相同的输入

If the command were run with neither option, the output would be the same as the input.

这是一个简单的例子 - 我会希望在code可以处理更加复杂的情况太

This is a simple case - I'd be hoping that the code could handle more complex cases too.

要说明一个真实的世界,但还是简单的例子:

To illustrate with a real-world but still simple example:

#ifdef USE_VOID
#ifdef PLATFORM1
#define VOID void
#else
#undef VOID
typedef void    VOID;
#endif /* PLATFORM1 */
typedef void *  VOIDPTR;
#else
typedef mint     VOID;
typedef char *  VOIDPTR;
#endif /* USE_VOID */

我想运行具有 -DUSE_VOID -UPLATFORM1 的命令,并得到输出:

#undef VOID
typedef void    VOID;
typedef void *  VOIDPTR;

另外一个例子:

#ifndef DOUBLEPAD
#if (defined NT) || (defined OLDUNIX)
#define DOUBLEPAD 8
#else
#define DOUBLEPAD 0
#endif /* NT */
#endif /* !DOUBLEPAD */

在理想情况下,我想与 -UOLDUNIX 运行,并得到输出:

#ifndef DOUBLEPAD
#if (defined NT)
#define DOUBLEPAD 8
#else
#define DOUBLEPAD 0
#endif /* NT */
#endif /* !DOUBLEPAD */

这可能会推我的运气!

动机:大,古code基地,有很多条件code的。许多条件不再适用 - 在OLDUNIX平台,例如,不再做,不再受支持,所以没有必要有在code对它的引用。其他条件总是如此。例如,功能被添加与条件编译使得code的单个版本可用于既较旧版本的软件,其中所述功能不可用和更新版本,其中它是可用(更多或更少)。最终,旧版本没有此功能不再支持 - 一切使用功能 - 这样的功能是present或不应该被删除的情况下,和当功能不存在'code应该太删除。我想有一个工具来自动完成这项工作,因为这将是比手动做(这是一叶知秋当code群包括21500源文件)更快,更可靠。

Motivation: large, ancient code base with lots of conditional code. Many of the conditions no longer apply - the OLDUNIX platform, for example, is no longer made and no longer supported, so there is no need to have references to it in the code. Other conditions are always true. For example, features are added with conditional compilation so that a single version of the code can be used for both older versions of the software where the feature is not available and newer versions where it is available (more or less). Eventually, the old versions without the feature are no longer supported - everything uses the feature - so the condition on whether the feature is present or not should be removed, and the 'when feature is absent' code should be removed too. I'd like to have a tool to do the job automatically because it will be faster and more reliable than doing it manually (which is rather critical when the code base includes 21,500 source files).

(该工具的真聪明版本可能会读的#include 'D文件,以确定是否控制宏 - 那些由-D或-U指定的命令行 - 在这些文件中定义我不知道这是否是除了作为后备诊断任何其他人这样做,虽然,伪pre-处理器不能扩展宏或逐字包括文件的输出必须真正有帮助的。是源相似,但通常比简单,输入code)。

(A really clever version of the tool might read #include'd files to determine whether the control macros - those specified by -D or -U on the command line - are defined in those files. I'm not sure whether that's truly helpful except as a backup diagnostic. Whatever else it does, though, the pseudo-pre-processor must not expand macros or include files verbatim. The output must be source similar to, but usually simpler than, the input code.)

经过一年的使用后,我很高兴与所选择的答案推荐的 sunifdef 。它没有搞错呢,我不希望它。我与它唯一的狡辩是文体。给定的输入,例如:

After a year of use, I am very happy with 'sunifdef' recommended by the selected answer. It hasn't made a mistake yet, and I don't expect it to. The only quibble I have with it is stylistic. Given an input such as:

#if (defined(A) && defined(B)) || defined(C) || (defined(D) && defined(E))

和与运行-uc(从未定义C),输出结果是:

and run with '-UC' (C is never defined), the output is:

#if defined(A) && defined(B) || defined(D) && defined(E)

这是因为技术上是正确的'和;&放大器;'结合比'||'更紧,但它是一个开放的邀请混乱。我更preFER它包括围绕套括号'和;&放大器;'条件,如原始

This is technically correct because '&&' binds tighter than '||', but it is an open invitation to confusion. I would much prefer it to include parentheses around the sets of '&&' conditions, as in the original:

#if (defined(A) && defined(B)) || (defined(D) && defined(E))

不过,鉴于一些code我有工作,因为这是最大的挑剔是一个强大的恭维的黑暗。它是有价值的工具给我。

However, given the obscurity of some of the code I have to work with, for that to be the biggest nit-pick is a strong compliment; it is valuable tool to me.

检查过的网址列入上述信息,我看到(如predicted)有一个新的方案称为科恩是继任者sunifdef。它可在SourceForge上,并已自2010年1月,我将在今年晚些时候检查出来...进一步的报告,或者明年的某个时候或,或从来没有。

Having checked the URL for inclusion in the information above, I see that (as predicted) there is an new program called Coan that is the successor to 'sunifdef'. It is available on SourceForge and has been since January 2010. I'll be checking it out...further reports later this year, or maybe next year, or sometime, or never.

推荐答案

我知道绝对没有关于C,但它听起来像你正在寻找类似的 unifdef 。请注意,它尚未从2000年开始更新,但有继任者称为unifdef的儿子(sunifdef)

I know absolutely nothing about C, but it sounds like you are looking for something like unifdef. Note that it hasn't been updated since 2000, but there is a successor called "Son of unifdef" (sunifdef).

这篇关于是否有一个C pre-处理器,消除基于定义的值#IFDEF块/未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆