Is Clang my solution?

78 views
Skip to first unread message

Pedro Delgado

unread,
Dec 28, 2012, 3:32:38 PM12/28/12
to llvm4ec...@googlegroups.com
Hello,

I don't need any information about the plugin, but I would like to know if Clang is what I need and I wonder if you could help me because I'm a bit lost. Well, what I need is to parser a C++ program in order to ger some information about the code and, after that, to change the code regarding that information collected. For instance, if I have this function:

1 int addition (int a, int b){
2       return a + b;
3 };

I would need to know that there is a plus sign (+) and where it is located in the code. So i would need the compiler told me there is a + sign in the second line.
Once i have that information,the next step would be to parse the code again to turn that + sign into another sign, for instance, the multiplication sign:

int addition (int a, int b){
        return a * b;
};

Could I do what i want with Clang? I don't want to write a whole parser for C++, that would be really though and I'm looking for a solution. I would need to collect more information apart from arithmetic operators such as all the constant values or even the inheritance hierarchy among classes if a program uses object-oriented features.

I hope you could lend me a hand. Thanks in advance.

Petri Tuononen

unread,
Dec 29, 2012, 3:46:09 AM12/29/12
to llvm4ec...@googlegroups.com
Hello Pedro,

I am afraid I do not have exact answer to your question as I made the plugin not the compiler itself.
What I know is that you can use Clang to parse your program and build AST (Abstract syntax tree). Then you go through the AST, change + to * and finally convert back to C++.

[1] Abstract syntax tree: http://en.wikipedia.org/wiki/Abstract_syntax_tree
[2] Modifying AST: http://lists.cs.uiuc.edu/pipermail/cfe-dev/2009-February/004198.html
[3] Clang API Documentation: http://clang.llvm.org/doxygen/classclang_1_1Parser.html

Hope this helps,
Petri Tuononen

Pedro Delgado

unread,
Jan 2, 2013, 7:35:10 AM1/2/13
to llvm4ec...@googlegroups.com
Hi Petri,

Thank you so much for your answer. All the information I receive is good to make an overall idea of this tool. 

So, to use Clang in the way I want, i would need to implement a "kind of plugin"  for Clang to go through the AST and then to make whatever I wanted, right? That plugin would be like a visitor pattern? 

I would like to make one question more. How it is like using Clang? it is relativey easy or tough in your opinion? I need to know whether it is worth the effort and the time to learn how it works or not. 

Thanks again. Best regards, 

Pedro.

Petri Tuononen

unread,
Jan 2, 2013, 8:20:19 AM1/2/13
to llvm4ec...@googlegroups.com
Hi Pedro,

It is difficult for me to answer, because I have never used Clang in such a way so I don't know specifics how to implement such a task. Also I do not know if you have what level of background in compiler theory. I am sure the task is feasible, but not sure what level of effort is needed. However http://clang.llvm.org get's you started as Clang is quite well documented. There's also user base that can help you out more.

Petri

Pedro Delgado

unread,
Jan 3, 2013, 6:02:26 AM1/3/13
to llvm4ec...@googlegroups.com
Ok Petri, I'm already reading the tutorials in http://clang.llvm.org to get more information. Thanks anyway for everything. 
If I ended up working with clang, I will consider to use this plugin for eclipse.

Thanks, 

Pedro.
Reply all
Reply to author
Forward
0 new messages