从 C++ 代码中获取人类可读的 AST [英] get human readable AST from c++ code

查看:42
本文介绍了从 C++ 代码中获取人类可读的 AST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了更好地理解 C++ 语言和语法的一些细节,我希望能够编写一个小的 C++ 程序,并查看编译器从中生成的 AST.

In order to get a better understanding of some of the details of the C++ language and grammer, I would love to be able to write a small C++ program, and see the AST that a compiler generates from that.

看起来 clang 过去有这个功能(-emit-asm),但它已经删除了.

It looks like clang had this feature in the past (-emit-asm), but it has removed.

今天有什么简单的方法可以做到这一点吗?

Is there an easy way to do this today?

推荐答案

clang 仍然具有该功能:

clang still has that functionality:

命令是 -ast-dump-ast-dump-xml

注意:-ast-dump-xml 仅在您在调试模式下构建 clang 时有效.

Note: -ast-dump-xml will only work when you build clang in debug mode.

http://clang.llvm.org/docs/IntroductionToTheClangAST.html

例如:

## cat test.cpp 
int main()
{
return 0;
}

##clang++ -cc1 -ast-dump-xml test.cpp                                       
<TranslationUnit ptr="0x4e42660">
 <Typedef ptr="0x4e42bd0" name="__builtin_va_list" typeptr="0x0">
  <PointerType ptr="0x4e42b90" canonical="0x4e42b90">
   <BuiltinType ptr="0x4e426f0" canonical="0x4e426f0"/>
  </PointerType>
 </Typedef>
 <Function ptr="0x4e42c70" name="main" returnzero="true" prototype="true">
  <FunctionProtoType ptr="0x4e42c20" canonical="0x4e42c20">
   <BuiltinType ptr="0x4e42750" canonical="0x4e42750"/>
   <parameters/>
  </FunctionProtoType>
  <Stmt>
CompoundStmt 0x4e42d78 <test.cpp:2:1, line:4:1>
`-ReturnStmt 0x4e42d58 <line:3:1, col:8>
  `-IntegerLiteral 0x4e42d38 <col:8> 'int' 0

  </Stmt>
 </Function>
</TranslationUnit>

这篇关于从 C++ 代码中获取人类可读的 AST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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