[cfe-dev] I want to find the Result of the AST Parsing

Sean Silva silvas at purdue.edu
Thu Aug 16 11:35:32 PDT 2012


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

On Thu, Aug 16, 2012 at 8:14 AM, Journeyer J. Joh
<oosaprogrammer at gmail.com> wrote:
> 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!
> --
> ----------------------------------------
> Journeyer J. Joh
> o o s a p r o g r a m m e r
> a t
> g m a i l  d o t  c o m
> ----------------------------------------
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list