如何在clang中从AST中排除标题?
我正在使用 clang 生成 AST.我有以下文件(lambda.cpp)要解析:
I'm generating AST using clang. I've got following file (lambda.cpp) to parse:
#include <iostream>
void my_lambda()
{
auto lambda = [](auto x, auto y) {return x + y;};
std::cout << "fabricati diem";
}
我正在使用以下命令解析它:
I'm parsing this using following command:
clang -Xclang -ast-dump -fsyntax-only lambda.cpp
问题在于,clang 还会解析标题内容.结果,我有相当大(约 3000 行)的文件,其中包含无用的(对我而言)内容.
The problem is that clang parses also headers content. As a result, I've got quite big (~3000 lines) file with useless (for me) content.
如何在生成 AST 时排除标题?
How to exclude headers when generating AST?
clang-check
可能对这个问题有用,clang-check
有选项 -ast-dump-filter=
记录如下
clang-check
might be useful on the matter, clang-check
has option -ast-dump-filter=<string>
documented as follow
-ast-dump-filter=- 与 -ast-dump 或 -ast-print 一起使用以转储/打印仅具有特定子字符串的 AST 声明节点限定名称.使用 -ast-list 列出所有可过滤的声明节点名字.
-ast-dump-filter=<string> - Use with -ast-dump or -ast-print to dump/print only AST declaration nodes having a certain substring in a qualified name. Use -ast-list to list all filterable declaration node names.
当 clang-check
在示例代码 (lambda.cpp) 上使用 -ast-dump-filter=my_lambda
运行时
when clang-check
run with -ast-dump-filter=my_lambda
on the sample code (lambda.cpp)
#include <iostream>
void my_lambda()
{
auto lambda = [](auto x, auto y) {return x + y;};
std::cout << "fabricati diem";
}
它只转储匹配的声明节点 FunctionDecl my_lambda 'void (void)'
It dumps only matched declaration node FunctionDecl my_lambda 'void (void)'
这是命令行参数和输出中的几行.
Here is the command line arguments and few lines from output.
$ clang-check -extra-arg=-std=c++1y -ast-dump -ast-dump-filter=my_lambda lambda.cpp --
FunctionDecl 0x2ddf630 <lambda.cpp:3:1, line:7:1> line:3:6 my_lambda 'void (void)'
`-CompoundStmt 0x2de1558 <line:4:1, line:7:1>
|-DeclStmt 0x2de0960 <line:5:9, col:57>