如何从Clang AST中仅获取函数声明?

5
我想要将我的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.

令人惊讶的是,如果我们包含了头文件,clang -Xclang -ast-dump=json -fSyntax-only main.cpp 不会给出 AST。对于 C 程序,它可以正常工作。 - achala
1个回答

1
这是一个与clang前端clang驱动程序之间差异相关的众所周知的问题。甚至在文档中都有涉及。
因此,运行clang -### main.c,复制所有的-internal-isystem-internal-externc-isystem选项,并将它们添加到您运行以获取AST的命令中。
希望这些信息对您有所帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接