I want to find the Result of the AST Parsing

Hello list,

I am recently reading the source code of the Clang/LLVM with gdb.
Among others I read about the AST Parsing these days. But finding the
result of this parsing is difficult for me.

I'd like to know where I can find the result of AST Parsing.

I found where the AST Parsing is commited. It's shown below.

break clang::ParseAST
break clang::Parser::ParseTopLevelDecl
break clang::Parser::ParseExternalDeclaration
break clang::Parser::ParseDeclarationOrFunctionDefinition
break clang::Parser::ParseDeclOrFunctionDefInternal
break clang::Parser::ParseDeclarationSpecifiers
break clang::Parser::ConsumeToken
break clang::Parser::ParseDeclGroup
break clang::Parser::ParseDeclarator
break clang::Parser::ParseDeclaratorInternal
break clang::Parser::ParseDirectDeclarator
break clang::Parser::ParseFunctionDeclarator
break clang::Sema::ActOnStartFunctionDeclarator
break clang::Parser::ParseParameterDeclarationClause
break clang::Parser::ParseDeclarationSpecifiers
break clang::Parser::ParseDeclarator
break clang::Parser::ParseFunctionDefinition
break clang::Parser::ParseCompoundStatementBody
break clang::Parser::ParseStatementOrDeclaration
break clang::Parser::ParseDeclaration
break clang::Parser::ParseExprStatement

This is my break points used in gdb.

I was able to trace the parsing of the C source code,
BUT I couldn't find the result of this AST Parsing.

I'd like to know where I can find the result of this AST Parsing.
I'd like to find it using gdb.

Or, I wonder if there is any way for me to print out the Parsed AST.
Any utility or compiler options?

Thank you very much in advance.

Have a good day!

Hello list,

I am recently reading the source code of the Clang/LLVM with gdb.
Among others I read about the AST Parsing these days. But finding the
result of this parsing is difficult for me.

I’d like to know where I can find the result of AST Parsing.

I found where the AST Parsing is commited. It’s shown below.

break clang::ParseAST
break clang::Parser::ParseTopLevelDecl
break clang::Parser::ParseExternalDeclaration
break clang::Parser::ParseDeclarationOrFunctionDefinition
break clang::Parser::ParseDeclOrFunctionDefInternal
break clang::Parser::ParseDeclarationSpecifiers
break clang::Parser::ConsumeToken
break clang::Parser::ParseDeclGroup
break clang::Parser::ParseDeclarator
break clang::Parser::ParseDeclaratorInternal
break clang::Parser::ParseDirectDeclarator
break clang::Parser::ParseFunctionDeclarator
break clang::Sema::ActOnStartFunctionDeclarator
break clang::Parser::ParseParameterDeclarationClause
break clang::Parser::ParseDeclarationSpecifiers
break clang::Parser::ParseDeclarator
break clang::Parser::ParseFunctionDefinition
break clang::Parser::ParseCompoundStatementBody
break clang::Parser::ParseStatementOrDeclaration
break clang::Parser::ParseDeclaration
break clang::Parser::ParseExprStatement

This is my break points used in gdb.

I was able to trace the parsing of the C source code,
BUT I couldn’t find the result of this AST Parsing.

I’d like to know where I can find the result of this AST Parsing.
I’d like to find it using gdb.

Or, I wonder if there is any way for me to print out the Parsed AST.
Any utility or compiler options?

You can use

clang -cc1 -ast-dump filename.cpp [header search and other options]

or

clang-check -ast-dump filename.cpp – [header search and other options]

to dump the AST parsed from filename.cpp. You can also use -ast-dump-filter for both to filter dumped declarations by a certain substring in name.

Thank you very much Alexander Kornienko! You replied to me so fast!

This would be very helpful for me!

But I'd like to also know where I need to look to find the result of
AST Parsing in the source code.

I tried to find it from 'ADecl' after 'P.ParseTopLevelDecl(ADecl)' in
the global function ParseAST(). But I couldn't find it there using
ddd.

Could somebody let me know where I need to look? (with gdb or ddd)

Thank you very much!

Journeyer J. Joh

Hi.

http://clang.llvm.org/doxygen/classclang_1_1ASTContext.html
http://clang.llvm.org/doxygen/classclang_1_1DeclContext.html

You can get a TU declaration from ASTContext::getTranslationUnitDecl(). Then since it derives from DeclContext, you can use all the iterators available to get at the declarations contained inside it.

I think the part that you are looking for is a class called ASTContext
<http://clang.llvm.org/doxygen/classclang_1_1ASTContext.html>, which
basically holds the result of parsing a translation unit.

It is awkward in that there isn't really a single function that is
just "parse and give me the ASTContext", which seems like it would be
the most natural thing. Instead, the general mechanism for accessing
the AST is through the ASTConsumer abstract interface
<http://clang.llvm.org/doxygen/classclang_1_1ASTConsumer.html>.
ASTConsumer has a lot of overridable virtual methods, but the only one
you really need to worry about is `HandleTranslationUnit(ASTContext
&Ctx)`. HandleTranslationUnit() is called after all parsing is
completed, and gives you the complete ASTContext corresponding to the
translation unit. Once you have the ASTContext, you should call
ASTContext::getTranslationUnitDecl() which is the root of the AST, and
from which you can explore the entire AST.

In gdb, you should be able to directly call methods on the ASTContext
to play around with it; to find "the" ASTContext, you will probably
want reach into Sema with Sema::getASTContext().

It seems like you have already run into clang::ParseAST(), which is
kind of complicated, but also really important. I should probably
write up some documentation covering ParseAST step-by-step, since it
is really a very important function.

--Sean Silva

Hello Sean Silva

Thank yuo again!

Your previous diagram open via this mail list is very helpful.
And this time again you replied very helpful advice.

I am not that familiar with gdb but I can now try it as you taught me.

Thank you very much!

I hope you have a good day. ^^;

Sincerely
Journeyer J. Joh

Hi Alexander Kornienko

Thank you very much!
I will try to look there you pointed out.

Thank you and I hope you have a good!

Best regards
Journeyer J. Joh