<div class="gmail_quote">On Mon, Jul 9, 2012 at 1:52 PM, Satya Prakash Prasad <span dir="ltr"><<a href="mailto:satyaprakash.prasad@gmail.com" target="_blank">satyaprakash.prasad@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I got one way to do that, not sure if correct:<br>
<div class="im"><br>
                        Stmt *i =  cast<Stmt>(*I);<br>
                        SourceManager &SourceMgr =<br>
TheCompInst.getSourceManager();<br>
                        SourceLocation ST = i->getSourceRange().getBegin();<br>
</div>                        SourceLocation ED = i->getLocEnd().getLocWithOffset(4);<br>
                        const char * scode = SourceMgr.getCharacterData(ST);<br>
                        const char * ecode = SourceMgr.getCharacterData(ED);<br>
                        char *code = (char *)calloc(ecode - scode + 1,<br>
sizeof(char));<br>
                        strncpy(code, scode, ecode - scode);<br>
<div class="im">                        std::cout << "STMT :" << code << "\n";<br>
<br>
</div>But I found that if, for or while etc which can have sub blocks the<br>
inside a main compound block - how do I traverse through them. The<br>
following code does not works:<br>
<br>
 if (isa<ForStmt>(i)) {<br></blockquote><div><br></div><div>You'll want</div><div>if (ForStmt *S = dyn_cast<ForStmt>(i)) {</div><div>... use S ...</div><div>}</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

                for (CompoundStmt::body_iterator  x = i->body_begin(),<br>
y = i->body_end(); x != y; ++x)<br>
                {<br>
<br>
                }<br>
   }<br>
Compilation Error:<br>
<br>
rewritersample.cpp:59: error: 'class clang::Stmt' has no member named<br>
'body_begin'<br>
rewritersample.cpp:59: error: 'class clang::Stmt' has no member named 'body_end'<br>
<br>
Please help and thanks in advance.<br>
<br>
Regards,<br>
Prakash<br>
<div class="HOEnZb"><div class="h5"><br>
On Mon, Jul 9, 2012 at 1:05 PM, Manuel Klimek <<a href="mailto:klimek@google.com">klimek@google.com</a>> wrote:<br>
> On Mon, Jul 9, 2012 at 9:32 AM, Satya Prakash Prasad<br>
> <<a href="mailto:satyaprakash.prasad@gmail.com">satyaprakash.prasad@gmail.com</a>> wrote:<br>
>><br>
>> Thanks once again. I am really thankful to Manuel and others in group<br>
>> who gave me more insight and confidence to work with CLANG.<br>
>><br>
>> However please note that when I used the below code to print the C++<br>
>> code (input as an input param : rewritecode test.cpp):<br>
>><br>
>>                         Stmt *i =  cast<Stmt>(*I);<br>
>>                         SourceManager &SourceMgr =<br>
>> TheCompInst.getSourceManager();<br>
>>                         SourceLocation ST =<br>
>> i->getSourceRange().getBegin();<br>
>>                         const char * code =<br>
>> SourceMgr.getCharacterData(ST);<br>
>>                         std::cout << "STMT  : " << code << "\n";<br>
><br>
><br>
> Do the same for getEnd(), then you have a char * for the end, and you can<br>
> print to that character...<br>
><br>
> Cheers,<br>
> /Manuel<br>
><br>
>><br>
>><br>
>> Ii prints additional line than what I want each time. Example if my<br>
>> test.cpp file is like below:<br>
>><br>
>> int inc(int &p)<br>
>> {<br>
>>         p++;<br>
>>         printf("%s %d",__FILE__,__LINE__);<br>
>>         printf("In inc [%d]\n", p);<br>
>>         return p;<br>
>> }<br>
>><br>
>><br>
>> I get output like:<br>
>><br>
>> STMT : p++;<br>
>>         printf("In inc [%d]\n", p);<br>
>>         return p;<br>
>> }<br>
>> ......................................................<br>
>><br>
>> How can I print only a single line of code at a time and not its<br>
>> consecutive one. I cannot tokenize it with '\n' as there are can<br>
>> multiple lines for a single statement. I would also really appreciate<br>
>> if I can get some reference of good books either offline / online to<br>
>> refer for beginners level.<br>
>><br>
>> Thanks in advance.<br>
>><br>
>> Regards,<br>
>> Prakash<br>
>><br>
>> On Fri, Jul 6, 2012 at 7:30 PM, Manuel Klimek <<a href="mailto:klimek@google.com">klimek@google.com</a>> wrote:<br>
>> > On Fri, Jul 6, 2012 at 2:33 PM, Satya Prakash Prasad<br>
>> > <<a href="mailto:satyaprakash.prasad@gmail.com">satyaprakash.prasad@gmail.com</a>> wrote:<br>
>> >><br>
>> >> Thanks for the information as provided, but I get output like below:<br>
>> >><br>
>> >> (CallExpr 0x1a85c480 'int'<br>
>> >>   (ImplicitCastExpr 0x1a85c468 'int (*)(const char *restrict, ...)'<br>
>> >> <FunctionToPointerDecay><br>
>> >>     (DeclRefExpr 0x1a85c3e8 'int (const char *restrict, ...)' lvalue<br>
>> >> Function 0x1a84e8e0 'printf' 'int (const char *restrict, ...)'))<br>
>> >>   (ImplicitCastExpr 0x1a85c4b8 'const char *' <ArrayToPointerDecay><br>
>> >>     (StringLiteral 0x1a85c388 'const char [13]' lvalue "In inc<br>
>> >> [%d]\n"))<br>
>> >>   (ImplicitCastExpr 0x1a85c4d0 'int' <LValueToRValue><br>
>> >>     (DeclRefExpr 0x1a85c3c0 'int' lvalue ParmVar 0x1a85c100 'p' 'int<br>
>> >> &')))<br>
>> >><br>
>> >> Kindly note that my requirement is to print the C++ code itself like<br>
>> >> dumpAll() method generated for above for below C++ code:<br>
>> ><br>
>> ><br>
>> > If you want to print the C++ code that produce the AST, you'll want to<br>
>> > get<br>
>> > the SourceRange, and use SourceManager's getCharacterData() method to<br>
>> > get<br>
>> > the underlying data - that will get you the C++ code as it was written<br>
>> > by<br>
>> > the user.<br>
>> ><br>
>> > Cheers,<br>
>> > /Manuel<br>
>> >><br>
>> >><br>
>> >> p++;<br>
>> >><br>
>> >> Similarly for other like:<br>
>> >><br>
>> >> dumpAll()<br>
>> >><br>
>> >> (CallExpr 0x1a85dec8 'int'<br>
>> >>   (ImplicitCastExpr 0x1a85deb0 'int (*)(const char *restrict, ...)'<br>
>> >> <FunctionToPointerDecay><br>
>> >>     (DeclRefExpr 0x1a85de88 'int (const char *restrict, ...)' lvalue<br>
>> >> Function 0x1a84e8e0 'printf' 'int (const char *restrict, ...)'))<br>
>> >>   (ImplicitCastExpr 0x1a85df08 'const char *' <ArrayToPointerDecay><br>
>> >>     (StringLiteral 0x1a85cdd8 'const char [19]' lvalue "y = [%d] z =<br>
>> >> [%d]\n"))<br>
>> >>   (ImplicitCastExpr 0x1a85df20 'int' <LValueToRValue><br>
>> >>     (DeclRefExpr 0x1a85ce18 'int' lvalue Var 0x1a85c6f0 'y' 'int'))<br>
>> >>   (ImplicitCastExpr 0x1a85df38 'int' <LValueToRValue><br>
>> >>     (DeclRefExpr 0x1a85de60 'int' lvalue Var 0x1a85c760 'z' 'int')))<br>
>> >><br>
>> >> While C++ Code:<br>
>> >><br>
>> >> printf("y = [%d] z = [%d]\n", y , z);<br>
>> >><br>
>> >> Thanks in advance.<br>
>> >><br>
>> >> Regards,<br>
>> >> Prakash<br>
>> >><br>
>> >><br>
>> >> On Fri, Jul 6, 2012 at 4:22 PM, Manuel Klimek <<a href="mailto:klimek@google.com">klimek@google.com</a>><br>
>> >> wrote:<br>
>> >> > On Fri, Jul 6, 2012 at 12:25 PM, Satya Prakash Prasad<br>
>> >> > <<a href="mailto:satyaprakash.prasad@gmail.com">satyaprakash.prasad@gmail.com</a>> wrote:<br>
>> >> >><br>
>> >> >> I made some progress with whatever support I received from this<br>
>> >> >> discussion forum. Thanks to all who responded to my request. My<br>
>> >> >> requirement now is to find a way I can print CLANG AST code to C++ /<br>
>> >> >> C<br>
>> >> >> human readable statements:<br>
>> >> >><br>
>> >> >>     bool VisitStmt(Stmt *s) {<br>
>> >> >>         if (isa<CompoundStmt>(s)) {<br>
>> >> >>                 CompoundStmt *S = cast<CompoundStmt>(s);<br>
>> >> >>                 for (CompoundStmt::body_iterator  I =<br>
>> >> >> S->body_begin(),<br>
>> >> >> E = S->body_end(); I != E; ++I)<br>
>> >> >>                 {<br>
>> >> >>                         Stmt *i =  cast<Stmt>(*I);<br>
>> >> >>                         //How can I print the C++ / C statement<br>
>> >> >> which<br>
>> >> >> CLANG is processing<br>
>> >> ><br>
>> >> ><br>
>> >> > You can use i->dumpAll() if you want to see what's going on.<br>
>> >> ><br>
>> >> > Cheers,<br>
>> >> > /Manuel<br>
>> >> ><br>
>> >> >><br>
>> >> >>                 }<br>
>> >> >>         }<br>
>> >> >><br>
>> >> >>         return true;<br>
>> >> >>     }<br>
>> >> >><br>
>> >> >> Regards,<br>
>> >> >> Prakash<br>
>> >> >><br>
>> >> >> _______________________________________________<br>
>> >> >> cfe-dev mailing list<br>
>> >> >> <a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
>> >> >> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
>> >> ><br>
>> >> ><br>
>> ><br>
>> ><br>
><br>
><br>
</div></div></blockquote></div><br>