且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从c ++代码获取人类可读的AST

更新时间:2022-12-18 11:17:02

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>