[cfe-dev] Control Flow Flattening with libtooling

mats petersson via cfe-dev cfe-dev at lists.llvm.org
Mon Nov 21 05:30:04 PST 2016


You do realize that to do this, you'd have to basically build a full parser
that understand the C-code, and can analyze and reason about the code's
flow, and then rebuild the same flow using the state-machine you want. It
has to manage things like `break;`, `goto` and `continue` in the loops.
It's really simple to do for the case you proposed, by try to do it for a a
REAL piece of code that actually does something more complex than produce a
constant of 5050 in `s` (your first example will compile to "nothing" if
you don't add a something that uses `s`, and if you add `printf("%d\n",
s);`, it will calculate 5050 as a compile-time constant, because clang
understands "calculate the sum of all numbers from 1..n and performas
n*(n-1)/2 as a constant calculation - even if you use a unknown value to
the compiler, it just does that simple math).

And for C++ with scoping and destruction, you'd also have to deal with
keeping track of the life-time of variables with non-trivial
constructor/destructors (such as `std::lock`)

--
Mats

On 19 November 2016 at 07:01, John Tan via cfe-dev <cfe-dev at lists.llvm.org>
wrote:

> A example what i want to achieve, but i'm stucked.
>
>
> Any help would be appreciated.
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20161121/e3499ba2/attachment.html>


More information about the cfe-dev mailing list