<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Arial,Helvetica,sans-serif;" dir="ltr">
<div style="color: rgb(33, 33, 33); font-size: 15px;">while (a>3){</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;">goto label1;</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;">}</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;">label1:</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;">cout << "hello" << endl;<br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;">goto label2;<br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;">label2:</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"> break;</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;">This would be the final result. all the labels would be outside of the while loop. </div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;">i know while Stmt has a getBody(). But i am unsure on how to use to it together with the astmatcher</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;">i use the rewrite functionality to write to the body - i use this. </div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;">But now , i am stumped on how to use the rewriter functionality to get the body. </div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"> </div>
<div style="color: rgb(33, 33, 33); font-size: 15px;">i did this in my method thats binded to the AstMatcher for while loop : </div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><span>const WhileStmt *WS = Result.Nodes.getNodeAs<clang::WhileStmt>("whileStmt")</span><br>
</div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><span>stmt s1 = WS->getBody(); <-- i am not able to print out this with the rewriter functionlity.</span></div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><span><br>
</span></div>
<div style="color: rgb(33, 33, 33); font-size: 15px;"><span>my astMatcher: <span> Matcher.addMatcher(whileStmt(hasDescendant(compoundStmt())).bind("whileStmt"), &HandlerForWhile);</span></span></div>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> mats.o.petersson@googlemail.com <mats.o.petersson@googlemail.com> on behalf of mats petersson <mats@planetcatfish.com><br>
<b>Sent:</b> Friday, November 18, 2016 2:40:18 AM<br>
<b>To:</b> John Tan; Clang Dev<br>
<b>Subject:</b> Re: [cfe-dev] Getting out body of a while Statement</font>
<div> </div>
</div>
<div>
<div dir="ltr">Please: unless there are specific reasons to do so (e.g. discussing personal things), always reply to the mailing list and all personal participants taking part in the thread. It helps other people being able to chime in, if they have better/different
suggestions, as well as someone else seeing the thread understanding what the outcome was.<br>
<div class="gmail_extra"><br>
<div class="gmail_quote">On 17 November 2016 at 17:49, John Tan <span dir="ltr"><<a href="mailto:NewSelleron@hotmail.com" target="_blank">NewSelleron@hotmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
<div id="gmail-m_4324139592374128991divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:calibri,arial,helvetica,sans-serif" dir="ltr">
<p>What i want is really simple. </p>
<p>I just wan to replace the original content in the while loop body with into a goto statement which will point to a label outside the statement, reason for this my project wants to do control flow flattening so the main purpose is to make reverse engineering
harder.</p>
<p><br>
</p>
<p></p>
<div><br>
</div>
<div>while (a > 3) {</div>
<div><br>
</div>
<div>cout << "hello" << endl;</div>
<div><br>
</div>
<div>}</div>
<div><br>
</div>
<div>Will become</div>
<div><br>
</div>
<div>while (a>3){</div>
<div><br>
</div>
<div>goto label1;</div>
<div><br>
</div>
<div>}</div>
<div><br>
</div>
<div>label1:</div>
<div><br>
</div>
<div>cout << "hello" << endl;</div>
<br>
<p></p>
<p><br>
</p>
</div>
</div>
</blockquote>
<div>Surely you mean:<br>
<div>while (a>3){</div>
<div><br>
</div>
<div>goto label1;</div>
<div><br>
</div>
<div>label2:<br>
</div>
<div>}</div>
<div>goto label3;<br>
</div>
<div>label1:</div>
<div><br>
</div>
<div>cout << "hello" << endl;<br>
</div>
<div>goto label2;<br>
</div>
<div>label3:<br>
</div>
<div><br>
</div>
<div>And since LLVM is pretty decent at figuring out "useless jumps", I'm not at all sure that this will actually achieve anything useful - the "useless" goto will just be removed. For any sufficiently complex project, with a bit of inline code and a general
large code-base, the compiler is pretty good at obfuscating the code anyway. <br>
</div>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
<div id="gmail-m_4324139592374128991divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:calibri,arial,helvetica,sans-serif" dir="ltr">
<p></p>
<p>I am not sure how to use the ASTMatcher to get to the body of the while stmt . </p>
</div>
</div>
</blockquote>
<div>The ASTMatcher will give you the AST statement for the while-loop, WhileStmt, which has a "getBody", which gives you the statements inside the body - you can get the source-locaton for the first and last. Or you can perhaps use the Clang Rewriter functionality.<br>
<br>
</div>
<div>However, before you do that, do check that LLVM doesn't just remove your gotos and turn the code into the same as you had before adding goto's - I'd be very surprised if it doesn't optimise that away at some stage. At least my small exampls:<br>
<br>
</div>
<div>a.c:<br>
<br>
#include <stdio.h><br>
<br>
int main()<br>
{<br>
int a = 0;<br>
while (a < 3)<br>
{<br>
printf("a=%d\n", a);<br>
a++;<br>
}<br>
}<br>
<br>
</div>
<div>b.c:<br>
<br>
#include <stdio.h><br>
<br>
int main()<br>
{<br>
int a = 0;<br>
while (a < 3)<br>
{<br>
goto L1;<br>
L2:;<br>
}<br>
goto L3;<br>
<br>
L1:<br>
printf("a=%d\n", a);<br>
a++;<br>
goto L2;<br>
<br>
L3:<br>
<br>
return 0;<br>
}<br>
<br>
</div>
<div>clang -S -O2 a.c <br>
</div>
<div>clang -S -O2 b.c<br>
</div>
<div><br>
</div>
<div>compiles to identical assembly-code in clang 3.8 (aside from the .file line which obviously shows 'a.c' and 'b.c' respectively)<br>
<br>
</div>
<div>[And this is of course ignoring interesting effects of scoping in C++, which will have to be dealt with in your translator if you don't want the converted code to behave differently]<br>
<br>
</div>
<div>Modern compilers aren't very easy to trick into generating different code just by adding goto's.
<br>
<br>
--<br>
</div>
<div>Mats<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div>
<div id="gmail-m_4324139592374128991divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:calibri,arial,helvetica,sans-serif" dir="ltr">
<p>I can get the sourcelocation of both the start and end of the body of the while stmt , but there is not method for me to extract information by source location. i hope you can help with me the method and the ASTMatcher needed. </p>
</div>
<hr style="display:inline-block;width:98%">
<div id="gmail-m_4324139592374128991divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b>
<a href="mailto:mats.o.petersson@googlemail.com" target="_blank">mats.o.petersson@googlemail.<wbr>com</a> <<a href="mailto:mats.o.petersson@googlemail.com" target="_blank">mats.o.petersson@googlemail.<wbr>com</a>> on behalf of mats petersson <<a href="mailto:mats@planetcatfish.com" target="_blank">mats@planetcatfish.com</a>><br>
<b>Sent:</b> Friday, November 18, 2016 1:42:13 AM<br>
<b>To:</b> John Tan<br>
<b>Cc:</b> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<b>Subject:</b> Re: [cfe-dev] Getting out body of a while Statement</font>
<div> </div>
</div>
<div>
<div class="gmail-h5">
<div>
<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" target="_blank">http://clang.llvm.org/docs/<wbr>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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div dir="ltr">
<div id="gmail-m_4324139592374128991m_5554894071024647970divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);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="gmail-m_4324139592374128991HOEnZb"><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" target="_blank">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>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</body>
</html>