如何仅从cast AST获得函数声明?

我的C程序需要AST,并希望以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.
lizhekai 回答:如何仅从cast AST获得函数声明?

这是一个与clang frontendclang driver之间的差异有关的众所周知的问题。甚至在docs中也有介绍。

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

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

本文链接:https://www.f2er.com/3029550.html

大家都在问