有什么办法可以治愈预处理器的忧郁症吗?

发布于 2024-07-17 21:42:06 字数 929 浏览 4 评论 0原文

我知道在我的特定情况下,我可以使用 -E 选项让预处理器吐出输出。 对于生成的代码来说,这个预处理器的输出是致命的。 例如,我有一个 4gl 应用程序,Informix 将其转换为 C,而后者又变成了可怕而丑陋的混乱。

我想要的是一个编辑器,它允许我指定有效的预处理器值并仅显示相关代码。 我在 Vim 中进行了一些非常基本的工作,匹配 #ifdef#endif,但是代码中充满了更高级的结构,例如 #ifndef#if#else。 更糟糕的是,这些结构在逻辑上更加复杂,而且我认为我的 Vim 脚本编写技能不足以让我从中得到我想要的东西。 例如:

#if DLEVEL > 5
    #define SIGNAL  1
    #if STACKUSE == 1
        #define STACK   200
    #else
        #define STACK   100
    #endif
#else
    #define SIGNAL  0
    #if STACKUSE == 1
        #define STACK   100
    #else
        #define STACK   50
    #endif
#endif
#if DLEVEL == 0
    #define STACK 0
#elif DLEVEL == 1
    #define STACK 100
#elif DLEVEL > 5
    display( debugptr );
#else
    #define STACK 200
#endif

如果我想解决它,包括定义一个表达式计算器。 这必须是一个已解决的问题! 如果您有 Vim 建议或其他建议,请告诉我。

I know that I can kick the the preprocessor to spit out output with the -E option in my particular circumstance. For generated code this preprocessor output is murderous. For example I have a 4gl application and Informix converts this into C which in turn gets spit out to a horrible ugly mess.

What I want is an editor that will allow me to specify what preprocessor values are in effect and show me only the relevant code. I have something very basic working in Vim matching #ifdef and #endif, but the code is riddled with more advanced constructs such is #ifndef, #if, and #else. To make matters worse, the constructs are logically more complex, and I don't think my Vim scripting skills are adequate for me to get what I want out of it. For example:

#if DLEVEL > 5
    #define SIGNAL  1
    #if STACKUSE == 1
        #define STACK   200
    #else
        #define STACK   100
    #endif
#else
    #define SIGNAL  0
    #if STACKUSE == 1
        #define STACK   100
    #else
        #define STACK   50
    #endif
#endif
#if DLEVEL == 0
    #define STACK 0
#elif DLEVEL == 1
    #define STACK 100
#elif DLEVEL > 5
    display( debugptr );
#else
    #define STACK 200
#endif

Includes defining an expression evaluator if I want to tackle it. This has to be a solved problem! If you have Vim suggestions or other ones please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

裸钻 2024-07-24 21:42:06

Eclipse CDT 编辑器可以很好地根据您声明为活动的宏来突出显示代码。 您可能想查看一下。alt text

The Eclipse CDT editor does a pretty good job highlighting code based on the macros you declare to be active. You may want to check it out.alt text

白馒头 2024-07-24 21:42:06

对于编辑器来说,Eclipse CDT 效果很好。 它显示哪些代码处于活动状态以及哪些代码被#ifdef'ed out,它在#ifdef'ed out的代码中提供语法突出显示,因此您仍然可以轻松阅读它,并且如果您使用鼠标,它可以一次单步执行一个宏扩展超过一个宏。

从命令行,cpp -dM filename.c 处理文件并仅显示有效的#defines。 例如,在您的代码中,它可能会输出

#define DLEVEL 5
#define SIGNAL 1
#define STACK 200
#define STACKUSE 1

,而不会与其他预处理器指令或 C/C++ 代码混淆列表。

(与此相关的是,cpp -dM /dev/null 是查找适合您平台的预定义宏的便捷方法。)

For an editor, Eclipse CDT works quite well. It shows which code is active and which code is #ifdef'ed out, it provides syntax highlighting within code that's #ifdef'ed out so you can still easily read it, and it can step through macro expansions one at a time if you mouse over a macro.

From the command line, cpp -dM filename.c processes a file and shows only the #defines that are in effect. For example, in your code, it might spit out

#define DLEVEL 5
#define SIGNAL 1
#define STACK 200
#define STACKUSE 1

without cluttering the listing with other preprocessor directives or with C/C++ code.

(On a related note, cpp -dM /dev/null is a handy way to find predefined macros for your platform.)

吃不饱 2024-07-24 21:42:06

考虑看看“Son of Unifdef”,在是否有一个 C 预处理器可以根据已定义/未定义的值消除 #ifdef 块?。 这不是一个可视化编辑器 - 然而,它会采用 C 代码(或其中包含 C 预处理器指令的代码)并生成修改后的文件,然后您可以将其与原始文件进行比较。

我对提到的 Informix 4GL (I4GL) 很好奇。 AFAIK,它生成的 C 代码并未包含 #ifdef 结构。 至少,在我负责的时候,输出是不允许包含任何内容的。 (您使用的是哪个版本的 I4GL?)有很多 #line 条目; 这些允许您将生成的代码追踪回相应的 I4GL 源。 我有一个脚本,可以将它们转换为简单的 C 注释,以便我可以使用调试器(gdbdbx 或...破灭这个想法,sdb 或 adb) 在编译后的代码上。 我还看到过预处理器(cppm4)用于生成 I4GL 源代码以提交给 I4GL 编译器。

Consider looking at "Son of Unifdef", cited in the answer to Is there a C pre-processor which eliminates #ifdef blocks based on values defined/undefined?. This isn't a visual editor - it would, however, take C code (or code with C preprocessor directives in it) and generate a modified file which you could then compare with the original.

I'm curious about the mention of Informix 4GL (I4GL). The C code it generates is not, AFAIK, laced with #ifdef constructs. At least, the output was not allowed to contain any when I was in charge of it. (Which version of I4GL are you using?) There are lots of #line entries; those permit you to chase the generated code back to the corresponding I4GL source. I have a script that converts those into simple C comments so that I can use a debugger (gdb or dbx or ... perish the thought, sdb or adb) on the compiled code. And I have also seen preprocessors (both cpp and m4) used to generate I4GL source code for submission to the I4GL compiler.

淡墨 2024-07-24 21:42:06

如果您是 Linux 用户并且还使用 GNOME,那么我必须推荐 GEdit。 在安装了一些用于智能感知、文件浏览等的插件后,我真的很喜欢它......如果如果您没有运行 GNOME,但仍在使用 Linux (KDE),您也许可以使用 Kate。 我玩它的时间不长,所以没什么可说的。 GEdit 在 KDE 中可以工作,但看起来不太正确。

如果您使用的是 Windows 并且拥有一台非常好的 PC,那么请尝试 Netbeans (是的,它也适用于 C++,而不是只是Java)。 如果没有,总是有 Visual C++ 2008(不过仍然非常依赖 RAM)。

我不确定 Mac 是否适用,因为我买不起,但 Netbeans 也可以在 Mac 上运行(以及 Linux)。

If you're a Linux user and you also use GNOME then I would have to recommend GEdit. I really loved it after I installed some plugins for Intellisense, file browsing, etc... If you're not running GNOME but are still using Linux (KDE) you might be able to use Kate. I didn't play with it for too long so I don't have much to say about that. GEdit will work in KDE but it won't look right.

If you're using Windows and have a really good PC then try Netbeans (yeah it's for C++ also, not just Java). If not there's always Visual C++ 2008 (still pretty RAM dependent though).

I'm not sure about Mac as I can't afford one, but Netbeans works on it also (as well as Linux).

花开浅夏 2024-07-24 21:42:06

Emacs 有 hide-ifdef-mode (我自己从来没有使用过它,所以我不能保证它的质量)

Emacs has hide-ifdef-mode (I've never had to use it myself, so I can't vouch for its quality)

凉宸 2024-07-24 21:42:06

您还可以尝试 unifdef ,它比 sunifdef 简单得多。

You can also try unifdef which is rather simpler than sunifdef.

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