How to get abstract syntax tree of `c` program in` GCC`

How can I get the abstract syntax tree of a c program in gcc?
I am trying to automatically insert OpenMP pragmas into c input program.
I need to analyze nested for loops to look for dependencies so that I can embed the appropriate OpenMP pragmas.
So basically what I want to do is go through and parse the abstract syntax tree of the input c program.
How do I achieve this?

+4
source share
2 answers

You need a full data stream to search for dependencies. Then you will need to actually insert OpenMP calls.

What you want is a program conversion system. GCC probably has dependency information, but for custom projects, working with them is quite difficult. Others mentioned Klang and Rose. Clang may be a worthy choice, but user analysis / transformation is not its main goal. Rose is designed to support custom tools, but IMHO is a fairly complex scheme, used in practice due to the use of the front end of the EDG, which is not intended to support conversion.

[NEXT TEXT DELETE BY MODERATOR. I HAVE THIS BACK, BECAUSE THIS IS ONE VALID TRANSFORMATION SYSTEM FOR THIS TASK. THE FACT THAT I AM RESPONSIBLE FOR THIS, UNDER ANY CIRCUMSTANCES WITHOUT LIMITING ITS VALUE AS A USEFUL ANSWER TO OP.]

Our DMS software reengineering toolkit with its front end C is clearly designed as a program conversion system. It has a complete data flow analysis (including point analysis, call schedule analysis and range analysis), linked to AST in a reasonable way. It provides initial rewriting rules that allow you to change AST expressed in the form of surface syntax; you can read conversions, not check a bunch of procedural code. With the modified AST, DMS can regenerate the source code, including comments in compiled form.

+1
source

Not exactly AST, but GCCXML can help http://linux.die.net/man/1/gccxml

edit: as indicated by Ira Baxter, gccxml does not display information about function bodies / methods. Here's a fork that seems to fix the flaw of http://sourceforge.net/projects/gccxml-bodies/

0
source

All Articles