XTRAN Example — Eliminate Dead Code in C, C++, Java, or C#
The following example uses a rule set written in XTRAN's rules language ("meta-code") that finds and suppresses every statement that is "dead" (cannot execute). The rules comprise 147 non-comment lines of meta-code.
NOTE that these rules are not specific to C / C++ / Java / C#; they are language-independent, and are exactly the same rules used for similar examples for other languages.
Strategy
- Use statement navigation facilities provided by XTRAN's rules language to follow every execution path through the code, and mark each visited statement as "live".
- Use recursive iterator facilities provided by XTRAN's rules language to visit every statement. If is not marked "live", suppress its output.
Process Flowchart
Here is a flowchart for this process, in which the elements are color coded:
- BLUE for XTRAN versions (runnable programs)
- ORANGE for XTRAN rules (text files)
- RED for
code
Input to XTRAN:
void func(void) { int i; if (i == 0 || i == 1) goto lbl2; i = 1; goto lbl3; /* * The following code is dead and should therefore be eliminated: */ if (i > 1) goto lbl1; i = 2; lbl1: i = 3; goto lbl3; /* * The following statement is dead and should therefore be eliminated: */ i = 5; lbl2: i = 6; lbl3: i = 7; }
Output from XTRAN:
void func(void) { int i; if (i == 0 || i == 1) goto lbl2; i = 1; goto lbl3; lbl2: i = 6; lbl3: i = 7; }