<div dir="ltr"><div><div>That would very much depend on exactly what the code looks like inside the loop - for example, if there is a break or continue, you need to deal with that. If you have multiple loops inside each other, you need to handle that in some way, etc. And whilst clang may be able to HELP, it certainly doesn't have tools that does this. And since you mention flattening, I presume this means "remove nesting from loops", which brings a whole heap of trouble if anyone ever declares variables inside those nestings, and you need to bring this out to the outer level, you now need to rename any collisions (preferrably without causing new name collisions).<br><br></div>Of course, the LLVM toolchain will recognize just about every "does the same thing" loop construct, called loop normalization, and do the same IR code for any variant of the equivalent code [at least for any reasonable input code], so I'm not sure there is that much to be gained from this - whether you think you're going to get better code, or more unreadable code - there was a thread looking to "obfuscate the code", and I think the conclusion from me in that case was that this would be a lot of work, and lllvm generated almost identical code either way (because it figures out that the goto-loop is actually the same as the original while-loop).<br><br></div><div>Obviously, it's still possible to mess up the code enough to confound the compiler into generating extra code that isn't optimal, and eventually end up with something that is ultimately hard to understand - but things like LTO and other optimization tools will also do a good job of "mangling" the code into something rather hard to understand, with much less effort.<br><br></div><div>Of course, without understanding what you are ultimately trying to achieve, I'm only guessing on the "obfuscation" side of things - but my advice if you are going down that route is to experiment by hand on some medium-complex code, and see if the machine code generated is actually sufficiently different to make the whole project worth-while.<br></div><div><br>--<br></div>Mats<br></div><div class="gmail_extra"><br><div class="gmail_quote">On 20 March 2017 at 14:35, Rachel HaCohen (rahacohe) <span dir="ltr"><<a href="mailto:rahacohe@cisco.com" target="_blank">rahacohe@cisco.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div link="blue" vlink="purple" lang="EN-US">
<div class="m_1094581389822449689WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Thanks.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Let’s say I want to create a tool that simply replaces every “while” block with an “if” block (and a goto in the end – to make it work the same).<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Is it possible to create such a tool with clang, that will work on any compilable c file?<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Rachel<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> <a href="mailto:mats.o.petersson@googlemail.com" target="_blank">mats.o.petersson@googlemail.<wbr>com</a> [mailto:<a href="mailto:mats.o.petersson@googlemail.com" target="_blank">mats.o.petersson@<wbr>googlemail.com</a>]
<b>On Behalf Of </b>mats petersson<br>
<b>Sent:</b> Monday, March 20, 2017 16:28<br>
<b>To:</b> Rachel HaCohen (rahacohe) <<a href="mailto:rahacohe@cisco.com" target="_blank">rahacohe@cisco.com</a>><br>
<b>Cc:</b> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>; Alon Shaltiel (ashaltie) <<a href="mailto:ashaltie@cisco.com" target="_blank">ashaltie@cisco.com</a>><br>
<b>Subject:</b> Re: [cfe-dev] Using clang for c code control flow manipulations<u></u><u></u></span></p><div><div class="h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">With this sort of thing, "the devil is in the detail". Yes, clang has tools to do things to source-code, and there are other tools to do things with the intermediate form in llvm.<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">But without fully understanding more in detail what changes you are wanting to do, and how those are meant to interact with the overall source code, it's hard to say exactly what is the best approach.<u></u><u></u></p>
</div>
<p class="MsoNormal">(I'm far from sure I can give any appropriate advice, but I know enough about the subject to know that there can be difficulties, and the exact details is what makes the difference between "approach A is good", "approach B" is good and
 "it's impossible no matter what".)<br>
<br>
--<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Mats<u></u><u></u></p>
</div>
</div>
</div>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On 19 March 2017 at 13:07, Rachel HaCohen (rahacohe) via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal">Hello all,<u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#24292e;background:white">In a project we’re working on, we wish to build a tool that automatically manipulates
 c code.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#24292e;background:white">We</span><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#24292e">
 wish to be able to track the control flow of the program and manipulate it (flattening</span><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#002060">, adding blocks</span><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#24292e">
</span><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#002060">of code
</span><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#24292e">etc.).</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#24292e;background:white">We want the tool to be very general and automatic an</span><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#002060;background:white">d</span><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#24292e;background:white">
 be able to work properly for every c file</span><i><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#002060;background:white">
</span></i><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#002060;background:white">and generate compilable code</span><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#24292e;background:white">.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#24292e;background:white"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#24292e;background:white">Do you think this is achievable using clang? Do you have any tips for implementing
 this tool?</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#24292e;background:white">Many thanks,</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Segoe UI",sans-serif;color:#24292e;background:white">Rachel.</span><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><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" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div></div></div>
</div>

</blockquote></div><br></div>