【问题标题】:How to get only function declaration from clang AST?如何仅从 clang AST 获取函数声明?
【发布时间】:2020-03-21 19:38:20
【问题描述】:

我想要 AST 用于我的 C 程序并希望以 json 格式表示。为此,我使用了clang -Xclang -ast-dump=json -fSyntax-only main.c 命令。它给出了 AST。但 AST 包含 typeDecl, Value declaration 等以及 function declaration。

我只想要 JSON 格式的代码中的函数声明。如何做到这一点?

这是一个替代clang-check -ast-dump -ast-dump-filter=main main.c,但这不能以 JSON 形式给出结果。当我执行此操作时,会收到一些错误消息以及此简单代码的输出

#include <stdio.h>
int main() {
    printf("Hello from C!");
        return 0;
}
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "main.c"
No compilation database found in /home/..../src or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.

【问题讨论】:

  • 令人惊讶的是,clang -Xclang -ast-dump=json -fSyntax-only main.cpp 不提供 AST 如果我们包含了标头。对于C 程序,它工作正常。

标签: c clang abstract-syntax-tree


【解决方案1】:

这是一个众所周知的问题,与clang frontend 和clang driver 之间的差异有关。它甚至被 docs 覆盖。

因此,运行 clang -### main.c,复制所有 -internal-isystem 和 -internal-externc-isystem 选项并将它们添加到您运行以获取 AST 的命令中。

我希望这些信息会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 2015-02-03
    相关资源
    最近更新 更多