source traversing with clang......

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

source traversing with clang......

jignesh vasoya
Hi,
I want to traverse source code of "C" language.
i want to extract all the "for loops" in given C file. After extraction loop body i want to visit its body statements.
then i want to extract all the functions one by one in the given source file.

i want to do these using clang.
what will be better? writing clang-plugin or separate program like whole program analysis?
i may need to visit each and every statement of the function.

how can i write AST walker that can walk through all the node?
is there any sub type walker for loops and functions only(means that will visit only loops of functions)?

--
___________________________________________________________________________
-----------------------------------------------------------------------------------------------------------------------------
Jignesh Vasoya
CSE - IIT MADRAS
9962248684



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

Re: source traversing with clang......

Reid Kleckner-2
You probably want the RecursiveASTVisitor.  I think the best docs for
it are the comments at the top of the header file.  Someone should
turn that into a quick little HTML doc, but it's been too long since I
used it to be useful with it.

Reid

On Thu, Jan 13, 2011 at 8:13 AM, jignesh vasoya <[hidden email]> wrote:

> Hi,
> I want to traverse source code of "C" language.
> i want to extract all the "for loops" in given C file. After extraction loop
> body i want to visit its body statements.
> then i want to extract all the functions one by one in the given source
> file.
>
> i want to do these using clang.
> what will be better? writing clang-plugin or separate program like whole
> program analysis?
> i may need to visit each and every statement of the function.
>
> how can i write AST walker that can walk through all the node?
> is there any sub type walker for loops and functions only(means that will
> visit only loops of functions)?
>
> --
> ___________________________________________________________________________
> -----------------------------------------------------------------------------------------------------------------------------
> Jignesh Vasoya
> CSE - IIT MADRAS
> 9962248684
>
>
> _______________________________________________
> 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: source traversing with clang......

Douglas Gregor
In reply to this post by jignesh vasoya

On Jan 13, 2011, at 5:13 AM, jignesh vasoya wrote:

> Hi,
> I want to traverse source code of "C" language.
> i want to extract all the "for loops" in given C file. After extraction loop body i want to visit its body statements.
> then i want to extract all the functions one by one in the given source file.
>
> i want to do these using clang.
> what will be better? writing clang-plugin or separate program like whole program analysis?
> i may need to visit each and every statement of the function.

It's easier to write a clang-plugin or add a new action based on the -fsyntax-only action.

> how can i write AST walker that can walk through all the node?
> is there any sub type walker for loops and functions only(means that will visit only loops of functions)?

As suggested by Reid, RecursiveASTVisitor is your best bet.

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

Re: source traversing with clang......

bruce.r.stephens
Douglas Gregor <[hidden email]> writes:

> On Jan 13, 2011, at 5:13 AM, jignesh vasoya wrote:
>
>> Hi,
>> I want to traverse source code of "C" language.
>> i want to extract all the "for loops" in given C file. After extraction loop body i want to visit its body statements.
>> then i want to extract all the functions one by one in the given source file.
>>
>> i want to do these using clang.
>> what will be better? writing clang-plugin or separate program like whole program analysis?
>> i may need to visit each and every statement of the function.
>
> It's easier to write a clang-plugin or add a new action based on the -fsyntax-only action.

If someone wanted to produce a standalone example (like a blend of
clang-interpreter and PrintFunctionNames, I guess) that used
RecursiveASTVisitor I suspect that would be valued.

[...]

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

Re: source traversing with clang......

Douglas Gregor

On Jan 14, 2011, at 3:07 AM, Bruce Stephens wrote:

> Douglas Gregor <[hidden email]> writes:
>
>> On Jan 13, 2011, at 5:13 AM, jignesh vasoya wrote:
>>
>>> Hi,
>>> I want to traverse source code of "C" language.
>>> i want to extract all the "for loops" in given C file. After extraction loop body i want to visit its body statements.
>>> then i want to extract all the functions one by one in the given source file.
>>>
>>> i want to do these using clang.
>>> what will be better? writing clang-plugin or separate program like whole program analysis?
>>> i may need to visit each and every statement of the function.
>>
>> It's easier to write a clang-plugin or add a new action based on the -fsyntax-only action.
>
> If someone wanted to produce a standalone example (like a blend of
> clang-interpreter and PrintFunctionNames, I guess) that used
> RecursiveASTVisitor I suspect that would be valued.

Yes. Or if someone were to make it easy to build an plugin that let is drop new ASTConsumers into a compilation job, that would make this kind of extension really easy.

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