如何在clang中从AST中排除标题? [英] How to exclude headers from AST in clang?

查看:37
本文介绍了如何在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>

这篇关于如何在clang中从AST中排除标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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