4

I know that it will never be fully accurate without the headers because C++ isn't context free.

Using the classic example of 'A B(C);', it means that it can be recognized as a function declaration or an object definition. Either is fine for me. I just need the file totally parsed.

I am not interested in the semantic analysis of the code, just in the syntactic one and AFAIK the grammar of clang is one of the best.

The problem is that in some scenarios clang is avoiding some declarations when it doesn't know the types although I guess it can correctly parse it.

See the following case. Content of class.cpp:

  A::A() { }

  A::~A() { }

  void A::B() { }

  A::C() { }

Executing the clang command line application:

$ clang -Xclang -ast-dump -fsyntax-only class.cpp

it's just recognizing as AST nodes the constructor and the last method.

 typedef char *__builtin_va_list;

 int A() (CompoundStmt 0x9a6a570 <class.cpp:3:8, col:10>)

 int C() (CompoundStmt 0x9a6a600 <class.cpp:9:8, col:10>)

Is there any way to get the complete AST tree?

Thanks!

rubarax
  • 158
  • 1
  • 8

1 Answers1

1

I'm not sure if this is what you need:

test.cpp:

class A {
    A();
    ~A();
    void B();
    void C();
};

A::A() {}
A::~A() {}
void A::B() {}
void A::C() {}

To dump the AST use: clang -Xclang -ast-dump -fsyntax-only test.cpp

TranslationUnitDecl 0x204f150 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x204f690 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
|-TypedefDecl 0x204f6f0 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
|-TypedefDecl 0x204fab0 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list '__va_list_tag [1]'
|-CXXRecordDecl 0x204fb00 <test.cpp:1:1, line:6:1> line:1:7 class A definition
| |-CXXRecordDecl 0x204fc10 <col:1, col:7> col:7 implicit class A
| |-CXXConstructorDecl 0x204fd10 <line:2:5, col:7> col:5 A 'void (void)'
| |-CXXDestructorDecl 0x2088cc0 <line:3:5, col:8> col:5 ~A 'void (void)'
| |-CXXMethodDecl 0x2088d90 <line:4:5, col:12> col:10 B 'void (void)'
| |-CXXMethodDecl 0x2088e50 <line:5:5, col:12> col:10 C 'void (void)'
| `-CXXConstructorDecl 0x2088f80 <line:1:7> col:7 implicit A 'void (const class A &)' inline noexcept-unevaluated 0x2088f80
|   `-ParmVarDecl 0x20890c0 <col:7> col:7 'const class A &'
|-CXXConstructorDecl 0x2089120 parent 0x204fb00 prev 0x204fd10 <line:8:1, col:9> col:4 A 'void (void)'
| `-CompoundStmt 0x2089218 <col:8, col:9>
|-CXXDestructorDecl 0x2089280 parent 0x204fb00 prev 0x2088cc0 <line:9:1, col:10> col:4 ~A 'void (void)'
| `-CompoundStmt 0x2089368 <col:9, col:10>
|-CXXMethodDecl 0x20893c0 parent 0x204fb00 prev 0x2088d90 <line:10:1, col:14> col:9 B 'void (void)'
| `-CompoundStmt 0x2089498 <col:13, col:14>
`-CXXMethodDecl 0x20894f0 parent 0x204fb00 prev 0x2088e50 <line:11:1, col:14> col:9 C 'void (void)'
  `-CompoundStmt 0x20895c8 <col:13, col:14>
Konrad Kleine
  • 4,275
  • 3
  • 27
  • 35