Question about printing abstract syntax tree in Clang

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about printing abstract syntax tree in Clang

Yang Ye
Hello everyone,

This is Yang Ye, a new researcher working on Clang. I want to use
clang to print out the abstract syntax tree in a human readable, not
getting the binary bytecode one.
Can anyone show me how to use the API to do it?



Another question, I found some sample code on the website about
dumping the CFG, it uses:

  Stmt* FooBody = ...
  CFG* FooCFG = CFG::buildCFG(FooBody);
  FooCFG->dump():

This is also helpful, but how can I construct a statement from give code?


Thanks a lot

-----------------------
Yang Ye
646-417-0326

Ph.D.
Department of Computer Science
The Fu Foundation School of Engineering and Applied Science Columbia University
[hidden email]

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Question about printing abstract syntax tree in Clang

Yang Ye
Another question please, can anyone help describe, if I have a class
definition in C++, what will it be like in the parsed result ----
abstract syntax tree?

Say for the class definition:

class A{
   int a;
   int b;
   bool c;
}

what will it be like in the parsed resulted AST?


Thank you!


-----------------------
Yang Ye
646-417-0326

Ph.D.
Department of Computer Science
The Fu Foundation School of Engineering and Applied Science Columbia University
[hidden email]



On Sun, Jun 27, 2010 at 10:17 PM, Yang Ye <[hidden email]> wrote:

> Hello everyone,
>
> This is Yang Ye, a new researcher working on Clang. I want to use
> clang to print out the abstract syntax tree in a human readable, not
> getting the binary bytecode one.
> Can anyone show me how to use the API to do it?
>
>
>
> Another question, I found some sample code on the website about
> dumping the CFG, it uses:
>
>   Stmt* FooBody = ...
>   CFG* FooCFG = CFG::buildCFG(FooBody);
>  FooCFG->dump():
>
> This is also helpful, but how can I construct a statement from give code?
>
>
> Thanks a lot
>
> -----------------------
> Yang Ye
> 646-417-0326
>
> Ph.D.
> Department of Computer Science
> The Fu Foundation School of Engineering and Applied Science Columbia University
> [hidden email]
>

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Question about printing abstract syntax tree in Clang

Chandler Carruth
In reply to this post by Yang Ye
On Sun, Jun 27, 2010 at 10:17 PM, Yang Ye <[hidden email]> wrote:
Hello everyone,

This is Yang Ye, a new researcher working on Clang. I want to use
clang to print out the abstract syntax tree in a human readable, not
getting the binary bytecode one.
Can anyone show me how to use the API to do it?

Currently you can use 'clang -cc1 -ast-dump' to get this. The code is driven from ASTPrinter defined here: http://clang.llvm.org/doxygen/ASTConsumers_8cpp_source.html#l00034

However, if you're interested in this type of walk, I'd suggest you consider the RecursiveASTVisitor: http://clang.llvm.org/doxygen/classclang_1_1RecursiveASTVisitor.html

It's not complete yet, so it misses a few parts of the AST, but it's being actively developed by several folks who are interested in similar walks of the AST.

-Chandler

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Question about printing abstract syntax tree in Clang

Ted Kremenek
In reply to this post by Yang Ye
The CFG is just a layer on top of the ASTs.  The basic blocks consist of CFGElements, which is a discriminated union to represent references to statements, etc.  Currently CFGElements only wrap Stmt*, but in the near future they will also represent destructor calls, etc.  Going back to the AST is as simple as iterating over the CFG.

To pretty-print the AST, there are a variety of dumpers.  The Stmt class has a dumpPretty() and dumpMethod(), for example.  The dumpPretty() method is not intended, however, to reprint the original code as it was written.

On Jun 27, 2010, at 10:17 PM, Yang Ye wrote:

> Hello everyone,
>
> This is Yang Ye, a new researcher working on Clang. I want to use
> clang to print out the abstract syntax tree in a human readable, not
> getting the binary bytecode one.
> Can anyone show me how to use the API to do it?
>
>
>
> Another question, I found some sample code on the website about
> dumping the CFG, it uses:
>
>   Stmt* FooBody = ...
>   CFG* FooCFG = CFG::buildCFG(FooBody);
>  FooCFG->dump():
>
> This is also helpful, but how can I construct a statement from give code?
>
>
> Thanks a lot
>
> -----------------------
> Yang Ye
> 646-417-0326
>
> Ph.D.
> Department of Computer Science
> The Fu Foundation School of Engineering and Applied Science Columbia University
> [hidden email]
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Question about printing abstract syntax tree in Clang

Yang Ye
In reply to this post by Chandler Carruth
Thanks! So can anyone show me a full tool chain of functions using the
Clang API, and a c++ file as input, then get the output of AST or CFG?
(not using clang -cc1 -ast-dump xx.cpp)



-----------------------
Yang Ye
646-417-0326

Ph.D.
Department of Computer Science
The Fu Foundation School of Engineering and Applied Science Columbia University
[hidden email]



On Mon, Jun 28, 2010 at 12:53 AM, Chandler Carruth <[hidden email]> wrote:

> On Sun, Jun 27, 2010 at 10:17 PM, Yang Ye <[hidden email]> wrote:
>>
>> Hello everyone,
>>
>> This is Yang Ye, a new researcher working on Clang. I want to use
>> clang to print out the abstract syntax tree in a human readable, not
>> getting the binary bytecode one.
>> Can anyone show me how to use the API to do it?
>
> Currently you can use 'clang -cc1 -ast-dump' to get this. The code is driven
> from ASTPrinter defined
> here: http://clang.llvm.org/doxygen/ASTConsumers_8cpp_source.html#l00034
> However, if you're interested in this type of walk, I'd suggest you consider
> the
> RecursiveASTVisitor: http://clang.llvm.org/doxygen/classclang_1_1RecursiveASTVisitor.html
> It's not complete yet, so it misses a few parts of the AST, but it's being
> actively developed by several folks who are interested in similar walks of
> the AST.
> -Chandler

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev