FindPrim locates a control flow primitive in the provided control flow graph and merges its nodes into a single node.
(g graph.Directed, dom cfg.DominatorTree)
| 13 | // FindPrim locates a control flow primitive in the provided control flow graph |
| 14 | // and merges its nodes into a single node. |
| 15 | func FindPrim(g graph.Directed, dom cfg.DominatorTree) (*primitive.Primitive, error) { |
| 16 | // Locate pre-test loops. |
| 17 | if prim, ok := FindPreLoop(g, dom); ok { |
| 18 | return prim.Prim(), nil |
| 19 | } |
| 20 | |
| 21 | // Locate post-test loops. |
| 22 | if prim, ok := FindPostLoop(g, dom); ok { |
| 23 | return prim.Prim(), nil |
| 24 | } |
| 25 | |
| 26 | // Locate 1-way conditionals. |
| 27 | if prim, ok := FindIf(g, dom); ok { |
| 28 | return prim.Prim(), nil |
| 29 | } |
| 30 | |
| 31 | // Locate 1-way conditionals with a body return statements. |
| 32 | if prim, ok := FindIfReturn(g, dom); ok { |
| 33 | return prim.Prim(), nil |
| 34 | } |
| 35 | |
| 36 | // Locate 2-way conditionals. |
| 37 | if prim, ok := FindIfElse(g, dom); ok { |
| 38 | return prim.Prim(), nil |
| 39 | } |
| 40 | |
| 41 | // TODO: Locate n-way conditionals. |
| 42 | //if prim, ok := FindSwitch(g, dom); ok { |
| 43 | // return prim.Prim(), nil |
| 44 | //} |
| 45 | |
| 46 | // Locate sequences of two statements. |
| 47 | if prim, ok := FindSeq(g, dom); ok { |
| 48 | return prim.Prim(), nil |
| 49 | } |
| 50 | |
| 51 | return nil, errors.New("unable to locate control flow primitive") |
| 52 | } |
| 53 | |
| 54 | // Merge merges the nodes of the primitive into a single node, which is assigned |
| 55 | // the basic block label of the entry node. |
nothing calls this directly
no test coverage detected