Dici
Dici

Reputation: 25950

Parsing Clang AST - indentation level and starting symbol

I have to parse by hand a Clang AST, however I have some trouble understanding the indentation rules supposed to describe the links between the nodes. Let's take a simple example. For the following code,

int f(int x) {
  int result = (x / 42);
  return result;
}

the generated AST is

... cutting out internal declarations of clang ...
`-FunctionDecl 0x5aeab50 <test.cc:1:1, line:4:1> f 'int (int)'
  |-ParmVarDecl 0x5aeaa90 <line:1:7, col:11> x 'int'
  `-CompoundStmt 0x5aead88 <col:14, line:4:1>
    |-DeclStmt 0x5aead10 <line:2:3, col:24>
    | `-VarDecl 0x5aeac10 <col:3, col:23> result 'int'
    |   `-ParenExpr 0x5aeacf0 <col:16, col:23> 'int'
    |     `-BinaryOperator 0x5aeacc8 <col:17, col:21> 'int' '/'
    |       |-ImplicitCastExpr 0x5aeacb0 <col:17> 'int' <LValueToRValue>
    |       | `-DeclRefExpr 0x5aeac68 <col:17> 'int' lvalue ParmVar 0x5aeaa90 'x' 'int'
    |       `-IntegerLiteral 0x5aeac90 <col:21> 'int' 42
    `-ReturnStmt 0x5aead68 <line:3:3, col:10>
      `-ImplicitCastExpr 0x5aead50 <col:10> 'int' <LValueToRValue>
        `-DeclRefExpr 0x5aead28 <col:10> 'int' lvalue Var 0x5aeac10 'result' 'int'

Note : for avoiding formatting problems with the quote symbol, I will replace it with \

Could someone explain me the semantics of this starting symbols and indentation level ?

Upvotes: 2

Views: 359

Answers (1)

Deduplicator
Deduplicator

Reputation: 45654

Replace their corner-symbol (they restrict themselves to ASCII) with unicode U+2514 Box drawings light up and right , and you'll see it better:

... cutting out internal declarations of clang ...
└─FunctionDecl 0x5aeab50 <test.cc:1:1, line:4:1> f 'int (int)'
  ├─ParmVarDecl 0x5aeaa90 <line:1:7, col:11> x 'int'
  └─CompoundStmt 0x5aead88 <col:14, line:4:1>
    ├─DeclStmt 0x5aead10 <line:2:3, col:24>
    │ └─VarDecl 0x5aeac10 <col:3, col:23> result 'int'
    │   └─ParenExpr 0x5aeacf0 <col:16, col:23> 'int'
    │     └─BinaryOperator 0x5aeacc8 <col:17, col:21> 'int' '/'
    │       ├─ImplicitCastExpr 0x5aeacb0 <col:17> 'int' <LValueToRValue>
    │       │ └─DeclRefExpr 0x5aeac68 <col:17> 'int' lvalue ParmVar 0x5aeaa90 'x' 'int'
    │       └─IntegerLiteral 0x5aeac90 <col:21> 'int' 42
    └─ReturnStmt 0x5aead68 <line:3:3, col:10>
      └─ImplicitCastExpr 0x5aead50 <col:10> 'int' <LValueToRValue>
        └─DeclRefExpr 0x5aead28 <col:10> 'int' lvalue Var 0x5aeac10 'result' 'int'

Used some more box-drawing characters to prettify the lines...

Upvotes: 3

Related Questions