[][src]Module c2rust_transpile::cfg::multiples

This module contains stuff related to preserving and using information from the initial C source to better decide when to produce Multiple structures instead of Loop structures. By default, relooper always makes loops. This sometimes leads to some pretty ugly (but correct) translations.

For instance,

if (i > 5) {
    while (i > 0) {
        i -= 3;
    }
}

gets translated to

let mut current_block: &'static str;
if i > 5i32 { current_block = "s_7"; } else { current_block = "s_14"; }
loop  {
    match current_block {
        "s_7" => {
            if !(i > 0i32) { current_block = "s_14"; continue ; }
            i -= 3i32;
            current_block = "s_7";
        }
        _ => { return; }
    }
};

We work around this by keeping track of branching points in the initial C source, along with all of the labels that are encountered in the arms of these branches leading back to the join label. We can use this information to sometimes tell relooper to make a Multiple structure instead of a Loop one.

The example from above then can be translated into

if i > 5i32 {
    while i > 0i32 {
        i -= 3i32
    }
};

Structs

MultipleInfo

Information about branching in a CFG.