cxx2flow 0.6.2

Convert your C/C++ code to control flow chart
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use crate::ast::{Ast, AstNode};
use crate::error::{Error, Result};
use hash_chain::ChainMap;
use itertools::{Itertools, Position};
use miette::NamedSource;
use petgraph::stable_graph::{NodeIndex, StableDiGraph};
use petgraph::visit::{EdgeRef, IntoNodeReferences};
use petgraph::EdgeDirection;
use std::collections::HashMap;
use std::{cell::RefCell, rc::Rc};
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum GraphNodeType {
    /// Dummy nodes will be removed eventually
    Dummy,
    Begin,
    End,
    Node(String),
    Choice(String),
}

#[derive(Debug, Clone, Copy)]
pub enum EdgeType {
    Normal,
    Branch(bool),
}

pub type Graph = StableDiGraph<GraphNodeType, EdgeType>;

struct GraphContext {
    pub graph: Graph,
    pub break_target: Option<NodeIndex>,
    pub continue_target: Option<NodeIndex>,
    pub goto_target: ChainMap<String, NodeIndex>,
    #[allow(dead_code)]
    pub global_begin: NodeIndex,
    pub global_end: NodeIndex,
    pub local_source: NodeIndex,
    pub local_sink: NodeIndex,
}

impl GraphContext {
    fn new() -> GraphContext {
        let mut graph = Graph::new();
        let begin = graph.add_node(GraphNodeType::Begin);
        let end = graph.add_node(GraphNodeType::End);
        GraphContext {
            graph,
            break_target: None,
            continue_target: None,
            goto_target: ChainMap::new(HashMap::new()),
            global_begin: begin,
            global_end: end,
            local_source: begin,
            local_sink: end,
        }
    }
}

fn build_graph(ast: &Ast, context: &mut GraphContext, source: &str, file_name: &str) -> Result<()> {
    // local_source -> [...current parsing...] -> local_sink
    let local_source = context.local_source;
    let local_sink = context.local_sink;
    let break_target = context.break_target;
    let continue_target = context.continue_target;
    if let Some(labels) = &ast.label {
        for i in labels {
            if let Some(v) = context.goto_target.get(i) {
                context.graph.add_edge(*v, local_source, EdgeType::Normal);
            } else {
                let v = context.graph.add_node(GraphNodeType::Dummy);
                context.goto_target.insert_at(0, i.clone(), v)?;
                // 0 is the global hashmap, goto labels should be put in hashmap 0
                context.graph.add_edge(v, local_source, EdgeType::Normal);
            }
        }
    }
    match &ast.node {
        AstNode::Dummy => {
            return Err(Error::UnexpectedDummyAstNode {
                src: NamedSource::new(file_name, source.to_string()),
                range: ast.range.clone().into(),
            })
        }
        AstNode::Compound(v) => {
            let mut sub_source = context.graph.add_node(GraphNodeType::Dummy);
            let mut sub_sink = context.graph.add_node(GraphNodeType::Dummy);
            context
                .graph
                .add_edge(local_source, sub_source, EdgeType::Normal);
            // what if v.is_empty()??
            if v.is_empty() {
                context
                    .graph
                    .add_edge(sub_source, sub_sink, EdgeType::Normal);
            } else {
                for (pos, i) in v.iter().with_position() {
                    context.local_source = sub_source;
                    context.local_sink = sub_sink;
                    build_graph(&i.borrow(), context, source, file_name)?;
                    match pos {
                        itertools::Position::First | itertools::Position::Middle => {
                            sub_source = sub_sink;
                            sub_sink = context.graph.add_node(GraphNodeType::Dummy);
                        }
                        _ => {}
                    }
                }
            }
            context
                .graph
                .add_edge(sub_sink, local_sink, EdgeType::Normal);
            context.local_source = local_source;
            context.local_sink = local_sink;
        }
        AstNode::Stat(s) => {
            // local_source -> current -> local_sink
            let current = context.graph.add_node(GraphNodeType::Node(s.clone()));
            context
                .graph
                .add_edge(local_source, current, EdgeType::Normal);
            context
                .graph
                .add_edge(current, local_sink, EdgeType::Normal);
        }
        AstNode::Continue(s) => {
            // local_source -> current -> continue_target
            let current = context.graph.add_node(GraphNodeType::Node(s.clone()));
            context
                .graph
                .add_edge(local_source, current, EdgeType::Normal);
            context.graph.add_edge(
                current,
                context.continue_target.ok_or(Error::UnexpectedContinue {
                    src: NamedSource::new(file_name, source.to_string()),
                    range: ast.range.clone().into(),
                })?,
                EdgeType::Normal,
            );
        }
        AstNode::Break(s) => {
            // local_source -> current -> break_target
            let current = context.graph.add_node(GraphNodeType::Node(s.clone()));
            context
                .graph
                .add_edge(local_source, current, EdgeType::Normal);
            context.graph.add_edge(
                current,
                context.break_target.ok_or(Error::UnexpectedBreak {
                    src: NamedSource::new(file_name, source.to_string()),
                    range: ast.range.clone().into(),
                })?,
                EdgeType::Normal,
            );
        }
        AstNode::Return(s) => {
            // local_source -> current -> global_end
            let current = context.graph.add_node(GraphNodeType::Node(s.clone()));
            context
                .graph
                .add_edge(local_source, current, EdgeType::Normal);
            context
                .graph
                .add_edge(current, context.global_end, EdgeType::Normal);
        }
        AstNode::If {
            cond,
            body,
            otherwise,
        } => {
            // local_source -> cond -> ---Y--> sub_source -> [...body...] -> sub_sink---------------v
            //                         ---N--> sub_source1 -> Option<[...otherwise...]> -> sub_sink -> local_sink
            let cond = context.graph.add_node(GraphNodeType::Choice(cond.clone()));
            let sub_source = context.graph.add_node(GraphNodeType::Dummy);
            let sub_sink = context.graph.add_node(GraphNodeType::Dummy);
            context.graph.add_edge(local_source, cond, EdgeType::Normal);
            context
                .graph
                .add_edge(cond, sub_source, EdgeType::Branch(true));
            context
                .graph
                .add_edge(sub_sink, local_sink, EdgeType::Normal);
            context.local_source = sub_source;
            context.local_sink = sub_sink;
            // context must be restored after calling this function
            // only graph should be changed
            // so it is OK to process the other branch directly
            build_graph(&body.borrow(), context, source, file_name)?;
            // restore context
            context.local_source = local_source;
            context.local_sink = local_sink;

            if let Some(t) = otherwise {
                let sub_source1 = context.graph.add_node(GraphNodeType::Dummy);
                context
                    .graph
                    .add_edge(cond, sub_source1, EdgeType::Branch(false));
                context.local_source = sub_source1;
                context.local_sink = sub_sink;
                build_graph(&t.borrow(), context, source, file_name)?;
                context.local_source = local_source;
                context.local_sink = local_sink;
            } else {
                context
                    .graph
                    .add_edge(cond, local_sink, EdgeType::Branch(false));
            }
        }
        AstNode::While { cond, body } => {
            // local_src -> cond ---Y--> sub_source -> [...body...] -> sub_sink
            //                |  \                                         /
            //                | N \_______________________________________/
            //                v                     <<<
            //           local_sink
            // continue: jump to cond
            // break: jump to local_sink
            let cond = context.graph.add_node(GraphNodeType::Choice(cond.clone()));
            let sub_source = context.graph.add_node(GraphNodeType::Dummy);
            let sub_sink = context.graph.add_node(GraphNodeType::Dummy);
            context.graph.add_edge(local_source, cond, EdgeType::Normal);
            context
                .graph
                .add_edge(cond, sub_source, EdgeType::Branch(true));
            context
                .graph
                .add_edge(cond, local_sink, EdgeType::Branch(false));
            context.graph.add_edge(sub_sink, cond, EdgeType::Normal);
            context.continue_target = Some(cond);
            context.break_target = Some(local_sink);
            context.local_source = sub_source;
            context.local_sink = sub_sink;
            build_graph(&body.borrow(), context, source, file_name)?;
            context.continue_target = continue_target;
            context.break_target = break_target;
            context.local_source = local_source;
            context.local_sink = local_sink;
        }
        AstNode::DoWhile { cond, body } => {
            // local_src -> sub_source -> [...body...] -> sub_sink -> cond ---N--> local_sink
            //                    \                                    /
            //                     <-----------------Y----------------<
            // continue: jump to cond
            // break: jump to local_sink
            let sub_source = context.graph.add_node(GraphNodeType::Dummy);
            let sub_sink = context.graph.add_node(GraphNodeType::Dummy);
            let cond = context.graph.add_node(GraphNodeType::Choice(cond.clone()));
            context
                .graph
                .add_edge(local_source, sub_source, EdgeType::Normal);
            context.graph.add_edge(sub_sink, cond, EdgeType::Normal);
            context
                .graph
                .add_edge(cond, sub_source, EdgeType::Branch(true));
            context
                .graph
                .add_edge(cond, local_sink, EdgeType::Branch(false));
            context.continue_target = Some(cond);
            context.break_target = Some(local_sink);
            context.local_source = sub_source;
            context.local_sink = sub_sink;
            build_graph(&body.borrow(), context, source, file_name)?;
            context.continue_target = continue_target;
            context.break_target = break_target;
            context.local_source = local_source;
            context.local_sink = local_sink;
        }
        AstNode::For {
            init,
            cond,
            upd,
            body,
        } => {
            // local_source -> init -> cond ---Y--> sub_source -> [...body...] -> sub_sink -> upd
            //                           |  \                                                  /
            //                           |   \----N--> local_sink                             /
            //                           |___________________________________________________/
            //                                              <<<
            // continue: jump to sub_sink
            // break: jump to local_sink
            let sub_source = context.graph.add_node(GraphNodeType::Dummy);
            let sub_sink = context.graph.add_node(GraphNodeType::Dummy);
            let cond = context.graph.add_node(GraphNodeType::Choice(cond.clone()));
            let init = context.graph.add_node(GraphNodeType::Node(init.clone()));
            let upd = context.graph.add_node(GraphNodeType::Node(upd.clone()));
            context.graph.add_edge(local_source, init, EdgeType::Normal);
            context.graph.add_edge(init, cond, EdgeType::Normal);
            context
                .graph
                .add_edge(cond, sub_source, EdgeType::Branch(true));
            context
                .graph
                .add_edge(cond, local_sink, EdgeType::Branch(false));
            context.graph.add_edge(sub_sink, upd, EdgeType::Normal);
            context.graph.add_edge(upd, cond, EdgeType::Normal);
            context.continue_target = Some(upd);
            context.break_target = Some(local_sink);
            context.local_source = sub_source;
            context.local_sink = sub_sink;
            build_graph(&body.borrow(), context, source, file_name)?;
            context.continue_target = continue_target;
            context.break_target = break_target;
            context.local_source = local_source;
            context.local_sink = local_sink;
        }
        AstNode::Switch { cond, body, cases } => {
            // local_src -> cond == case[0] ---Y-> goto case[0]
            //                              ---N-> cond == case[1] ....
            //                                                ---N--> goto default
            // sub_src -> [..body..] -> sub_sink -> local_sink
            // continue: None
            // break: local_sink
            let case_goto_targets: HashMap<String, NodeIndex> = cases
                .iter()
                .map(|c| (c.clone(), context.graph.add_node(GraphNodeType::Dummy)))
                .collect();
            let table_start = generate_jump_table(
                cond,
                &mut context.graph,
                &mut cases.iter().filter(|x| *x != "default").with_position(),
                &case_goto_targets,
                &cases.iter().any(|x| x == "default"),
                &local_sink,
            );
            context
                .graph
                .add_edge(local_source, table_start, EdgeType::Normal);
            let sub_source = context.graph.add_node(GraphNodeType::Dummy);
            let sub_sink = context.graph.add_node(GraphNodeType::Dummy);
            context.goto_target.new_child_with(case_goto_targets);
            context.local_source = sub_source;
            context.local_sink = sub_sink;
            context.break_target = Some(local_sink);
            context.continue_target = None;
            context
                .graph
                .add_edge(sub_sink, local_sink, EdgeType::Normal);
            build_graph(&body.borrow(), context, source, file_name)?;
            context.local_source = local_source;
            context.local_sink = local_sink;
            context.break_target = break_target;
            context.continue_target = continue_target;
            context.goto_target.remove_child();
        }
        AstNode::Goto(t) => {
            // local_source -> goto_target
            if let Some(target) = context.goto_target.get(t) {
                context
                    .graph
                    .add_edge(local_source, *target, EdgeType::Normal);
            } else {
                let v = context.graph.add_node(GraphNodeType::Dummy);
                context.goto_target.insert_at(0, t.clone(), v)?;
                context.graph.add_edge(local_source, v, EdgeType::Normal);
            }
        }
    }
    Ok(())
}

fn generate_jump_table<'a, I, R>(
    cond: &str,
    graph: &mut Graph,
    iter: &mut I,
    case_goto_targets: &HashMap<String, NodeIndex>,
    has_default: &bool,
    sink: &NodeIndex,
) -> NodeIndex
where
    I: Itertools<Item = (Position, R)>,
    R: AsRef<str>,
{
    if let Some((pos, i)) = iter.next() {
        // dbg!(i);
        let cur = graph.add_node(GraphNodeType::Choice(format!("{} == {}", cond, i.as_ref())));
        graph.add_edge(cur, case_goto_targets[i.as_ref()], EdgeType::Branch(true));
        match pos {
            itertools::Position::First | itertools::Position::Middle => {
                let idx =
                    generate_jump_table(cond, graph, iter, case_goto_targets, has_default, sink);
                graph.add_edge(cur, idx, EdgeType::Branch(false));
            }
            itertools::Position::Last | itertools::Position::Only => {
                if *has_default {
                    graph.add_edge(cur, case_goto_targets["default"], EdgeType::Branch(false));
                } else {
                    graph.add_edge(cur, *sink, EdgeType::Branch(false));
                }
            }
        };
        cur
    } else {
        let cur = graph.add_node(GraphNodeType::Dummy);
        if *has_default {
            graph.add_edge(cur, case_goto_targets["default"], EdgeType::Normal);
        } else {
            graph.add_edge(cur, *sink, EdgeType::Normal);
        }
        cur
    }
}

fn remove_zero_in_degree_nodes(graph: &mut Graph, _source: &str) -> bool {
    let nodes = graph
        .node_indices()
        .filter(|i| -> bool {
            *graph.node_weight(*i).unwrap() == GraphNodeType::Dummy
                && graph.edges_directed(*i, EdgeDirection::Incoming).count() == 0
        })
        .collect_vec();
    nodes
        .iter()
        .map(|x| graph.remove_node(*x))
        .any(|x| x.is_some())
}

// remove the first node which predicate(node) == True
// return Ok(true) if successfully remove a node
// return Ok(false) if no node is available
// return Err if there are more than one predecessors
fn remove_single_node<F>(graph: &mut Graph, _source: &str, predicate: F) -> Result<bool>
where
    F: Fn(NodeIndex, &GraphNodeType) -> bool,
{
    // take first dummy node
    if let Some(node_index) = graph
        .node_references()
        .filter(|(x, t)| predicate(*x, t))
        .map(|(x, _)| x)
        .take(1)
        .next()
    {
        let incoming_edges = graph
            .edges_directed(node_index, EdgeDirection::Incoming)
            .map(|x| (x.source(), *x.weight()))
            .collect_vec();
        let neighbors = graph
            .neighbors_directed(node_index, EdgeDirection::Outgoing)
            .collect_vec();
        if neighbors.len() != 1 {
            return Err(Error::UnexpectedOutgoingEdges {
                node_index,
                neighbors,
                graph: graph.clone(),
            });
        }
        let next_node = neighbors[0];
        for (src, edge_type) in incoming_edges {
            // add edge: i.src -> next_node
            graph.add_edge(src, next_node, edge_type);
        }
        graph.remove_node(node_index);
        Ok(true)
    } else {
        Ok(false)
    }
}

pub fn from_ast(ast: Rc<RefCell<Ast>>, source: &str, file_name: &str) -> Result<Graph> {
    let mut ctx = GraphContext::new();
    build_graph(&ast.borrow(), &mut ctx, source, file_name)?;
    // dbg!(petgraph::dot::Dot::new(&ctx.graph));
    while remove_zero_in_degree_nodes(&mut ctx.graph, source) {}
    while remove_single_node(&mut ctx.graph, source, |_, t| *t == GraphNodeType::Dummy)? {}
    let remove_empty_nodes: fn(NodeIndex, &GraphNodeType) -> bool = |_, t| match t {
        GraphNodeType::Node(t) => t.is_empty() || t.trim() == ";",
        _ => false,
    };
    while remove_single_node(&mut ctx.graph, source, remove_empty_nodes)? {}
    Ok(ctx.graph)
}