<div dir="ltr"><div><div><div><div><div><div><div><div><div>Really depends on what you want to achieve [in the big picture, not "I want a variable holding the content inside the while", but what you are actually planning to do beyond getting it into a variable - do you want to edit the source file to add or remove something, check that the body does/doesn't do something]<br><br>Something involving the ASTMatcher would be a starting point:<br><a href="http://clang.llvm.org/docs/LibASTMatchersReference.html">http://clang.llvm.org/docs/LibASTMatchersReference.html</a>?<br><br></div>If you want the actual source-code, then you'll also need to get out the source location, and use sourcemanager to get the "section of source code within the body into a string", but consider that you can have really "interesting" code:<br><br></div>    while( a > 3 )<br>    {<br></div>    #include "mycode.h"<br>    }<br><br></div><div>or:<br>    while( a > 3 )<br>     #include "mycode.h"<br><br></div><div>[where the content in mycode.h contains not just the loop body, but also further code that continues AFTER the loop.]<br><br></div><div>or:<br><br></div><div>    #define SOME_MACRO(x) while(a < (x)) <br><br></div><div>    SOME_MACRO(3)<br>    {<br>      ... <br>    }<br></div><div><br></div>or:<br></div>    while( a > 3 )<br>    {<br></div>        SOME_MACRO(foo);<br>    }<br><br></div>where SOME_MACRO expands to some rather large chunk of code - and knowing the "source" is not really helpful in either of these cases. And of course, like the #include sample, you can have a the loop body end part way through the macro, so you probably don't really want to rely on the "string contains the body of the loop" if you want to do something with the content of the loop that is of any importance. These are of course simple examples of "unusual programming", but I guarantee that if you look at enoug code, you'll find SOMETHING like that.<br><br><br></div>So, depending on what you actualy want to achieve, you may want to NOT try to deal with this as files/text strings, but as AST-code.<br><br>--<br></div>Mats<br></div><div class="gmail_extra"><br><div class="gmail_quote">On 17 November 2016 at 16:04, John Tan via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">




<div dir="ltr">
<div id="m_5554894071024647970divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Arial,Helvetica,sans-serif" dir="ltr">
<p>i need help to get out the body of a while statement. </p>
<p><br>
</p>
<p>While( a > 3) { </p>
<p><br>
</p>
<p>cout << "hello" <<endl;   << --  I wan to copy out this line and store into a variable.</p>
<p>}</p>
<p><br>
</p>
<p>This is a example , i want to take out whats inside of the while statement, and if its possible store it into a variable so i can print the result out somewhere. </p>
<p><br>
</p>
<p>Much appreciated </p><span class="HOEnZb"><font color="#888888">
<p>John Tan. </p>
</font></span></div>
</div>

<br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>