如何执行C预处理程序宏的逐步扩展? [英] How to perform step-wise expansion of a C preprocessor macro?

查看:42
本文介绍了如何执行C预处理程序宏的逐步扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gdb 有一个已记录但尚未实​​现的命令(从8.3版开始),称为 macro expand-once .其目的是执行单步宏扩展,而无需递归其他宏调用.从文档:

gdb has a documented, yet still unimplemented command (as of version 8.3) called macro expand-once. Its purpose is to perform a single-step macro expansion without recursing into other macro invocations. From the docs:

宏一次扩展 表达式
macro exp1 表达式

(此命令尚未实现.)显示扩展那些明确显示在 expression 中的预处理器宏调用的结果.在该扩展中出现的宏调用保持不变.此命令使您可以更清楚地看到特定宏的效果,而不会因进一步扩展而感到困惑.由于 GDB 仅扩展宏,但不解析结果,因此 expression 不必是有效的表达式;它可以是任何令牌字符串.

(This command is not yet implemented.) Show the results of expanding those preprocessor macro invocations that appear explicitly in expression. Macro invocations appearing in that expansion are left unchanged. This command allows you to see the effect of a particular macro more clearly, without being confused by further expansions. Since GDB simply expands macros, but does not parse the result, expression need not be a valid expression; it can be any string of tokens.

真是令人发指!这样的功能将为概念上简单的,迭代的 gdb 脚本奠定基础,以输出宏扩展的每个步骤,而这正是我所寻求的信息.它是否恰巧由 gdb 交付对我来说是次要的,但是我确实希望以某种方式将其自动化-—我厌倦了遍历代码并手工编写所有内容.

What an excruciating tease! Such a feature would lay the groundwork for a conceptually simple, iterative gdb script to output each step of a macro expansion, which is exactly the information I am seeking. Whether or not it happens to be delivered by gdb is secondary to me, but I do want this to be automated somehow — I am tired of digging through code and writing everything out by hand.

在实现 gdb macro expand-once 命令之前,是否还有其他编程方式来执行C预处理程序宏的逐步扩展?我想可能可以通过从 cpp 转储宏定义,解析输出并制作某种调用图"来实现,但也许我是天真的乐观主义者.

Until the macro expand-once command of gdb is implemented, is there some other programmatic way to perform step-wise expansion of a C preprocessor macro? I imagine it might be possible by dumping macro definitions from cpp, parsing the output, and making a sort of "call graph", but maybe I'm being naively optimistic.

注意:尽管赏金声明指出:仅提供对库函数的引用以构建潜在解决方案,就不会得到赏金的奖励."如果符合赏金条件,我仍然可以接受这样的答案悬赏期结束时尚未发布解决方案.

NOTE: Although the bounty note states, "Simply providing references to library functions for constructing a potential solution will not be rewarded the bounty," I may still accept such an answer if a bounty-qualifying solution has not been posted by the end of the bounty period.

推荐答案

您可以使用

You could use wave (part of the boost library):

来自教义的示例:

文件 test.cpp :


    // test.cpp
    #define X(x)          x
    #define Y()           2
    #define CONCAT_(x, y) x ## y
    #define CONCAT(x, y)  CONCAT_(x, y)
    #pragma wave trace(enable)
    // this macro expansion is to be traced
    CONCAT(X(1), Y())     // should expand to 12
    #pragma wave trace(disable)

运行 wave -t test.trace test.cpp 创建文件 test.trace :

Running wave -t test.trace test.cpp creates a file test.trace:


test.cpp:8:1: CONCAT(X(1), Y())
  test.cpp:5:9: see macro definition: CONCAT(x, y)
  invoked with
  [
    x = X(1)
    y = Y()
  ]
  [
    test.cpp:2:9: see macro definition: X(x)
    invoked with
    [
      x = 1
    ]
    [
      1
      rescanning
      [
        1
      ]
    ]
    test.cpp:3:9: see macro definition: Y()
    [
      2
      rescanning
      [
        2
      ]
    ]
    CONCAT_(1, 2)
    rescanning
    [
      test.cpp:4:9: see macro definition: CONCAT_(x, y)
      invoked with
      [
        x = 1
        y = 2
      ]
      [
        12
        rescanning
        [
          12
        ]
      ]
      12
    ]
  ]

这篇关于如何执行C预处理程序宏的逐步扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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