clang ASTのdump

2015年11月18日

clang libtooling

t f B! P L

ASTのdumpから

libtoolingで、ASTのtree走査を調べたりしてましたが、 そもそもASTのdumpがちゃんと出来ていないと、ASTの構造がどうなってるかも調べられないし、 なにも始まってないことに気がついたので、いまさら調べ。

以下のコマンドで出力、test.ccをdump

clang -Xclang -ast-dump -fsyntax-only test.cc

clang -cc1 --helpで ヘルプ
https://gist.github.com/masuidrive/5231110

-Xclang       :Pass <arg> to the clang compiler
-ast-dump     :Build ASTs and then debug dump them
-fsyntax-only :構文チェックのみ

以下のコードを試しにdumpした。

//test.cc
int func(int x) {
  int result = (x / 42)+2;
  return result;
}
clang -Xclang -ast-dump -fsyntax-only test.cc
TranslationUnitDecl 0x80083880 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x80083b70 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list 'char *'

`-FunctionDecl 0x80083c20 <test.cc:1:1, line:4:1> line:1:5 func 'int (int)'
  |-ParmVarDecl 0x80083bb0 <col:10, col:14> col:14 used x 'int'
  `-CompoundStmt 0x80083db8 <col:17, line:4:1>
    |-DeclStmt 0x80083d70 <line:2:3, col:26>
    | `-VarDecl 0x80083ca0 <col:3, col:25> col:7 used result 'int' cinit
    |   `-BinaryOperator 0x80083d58 <col:16, col:25> 'int' '+'
    |     |-ParenExpr 0x80083d28 <col:16, col:23> 'int'
    |     | `-BinaryOperator 0x80083d10 <col:17, col:21> 'int' '/'
    |     |   |-ImplicitCastExpr 0x80083d00 <col:17> 'int' <LValueToRValue>
    |     |   | `-DeclRefExpr 0x80083cd0 <col:17> 'int' lvalue ParmVar 0x80083bb0 'x' 'int'
    |     |   `-IntegerLiteral 0x80083ce8 <col:21> 'int' 42
    |     `-IntegerLiteral 0x80083d40 <col:25> 'int' 2
    `-ReturnStmt 0x80083da8 <line:3:3, col:10>
      `-ImplicitCastExpr 0x80083d98 <col:10> 'int' <LValueToRValue>
        `-DeclRefExpr 0x80083d80 <col:10> 'int' lvalue Var 0x80083ca0 'result' 'int'


参考


その他

-ast-list   :Build ASTs and print the list of declaration node qualified names
func
x
result
-ast-print  :Build ASTs and then pretty-print them
int func(int x) {
    int result = (x / 42) + 2;
    return result;
}
-ast-view   :Build ASTs and view them with GraphViz  
GraphVizないので

このブログを検索

QooQ