<div class="gmail_quote">On Mon, Jul 9, 2012 at 9:32 AM, 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">
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>
<div class="im"><br>
                        Stmt *i =  cast<Stmt>(*I);<br>
</div>                        SourceManager &SourceMgr =<br>
TheCompInst.getSourceManager();<br>
                        SourceLocation ST = i->getSourceRange().getBegin();<br>
                        const char * code = SourceMgr.getCharacterData(ST);<br>
                        std::cout << "STMT  : " << code << "\n";<br></blockquote><div><br></div><div>Do the same for getEnd(), then you have a char * for the end, and you can print to that character...</div>
<div><br></div><div>Cheers,</div><div>/Manuel</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Ii prints additional line than what I want each time. Example if my<br>
test.cpp file is like below:<br>
<div class="im"><br>
int inc(int &p)<br>
{<br>
        p++;<br>
        printf("%s %d",__FILE__,__LINE__);<br>
</div><div class="im">        printf("In inc [%d]\n", p);<br>
        return p;<br>
}<br>
<br>
<br>
</div>I get output like:<br>
<br>
STMT : p++;<br>
<div class="im">        printf("In inc [%d]\n", p);<br>
        return p;<br>
}<br>
</div>......................................................<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>
<div class="im HOEnZb"><br>
Thanks in advance.<br>
<br>
Regards,<br>
Prakash<br>
<br>
</div><div class="HOEnZb"><div class="h5">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 [%d]\n"))<br>
>>   (ImplicitCastExpr 0x1a85c4d0 'int' <LValueToRValue><br>
>>     (DeclRefExpr 0x1a85c3c0 'int' lvalue ParmVar 0x1a85c100 'p' 'int &')))<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 get<br>
> the SourceRange, and use SourceManager's getCharacterData() method to get<br>
> the underlying data - that will get you the C++ code as it was written 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>> 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++ / 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 = 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 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>
</div></div></blockquote></div><br>