2

Is there any API or reference library available to parse a java code or java method into control flow graph (CFG). I tried to convert AST into CFG but couldn't do it using AST Parser but didnt find any way to do so. I am working on Eclipse IDE JAVA (J2SE).
Kindly help.

asad
  • 23
  • 7
  • Once you've got it parsed you're done with the hard part. Next you need to scan the statements and calculate layout parameters -- allocate diagram width to each element, etc. Then scan again and draw the diagram. Devising a layout algorithm requires thought, though. – Hot Licks Oct 16 '14 at 12:28
  • Are you looking for *just* control flow for *one* method, or do you want something that includes a call graph across methods? – Ira Baxter Oct 16 '14 at 13:00
  • @HotLicks: Fine, but I also want to save it in xml format. I want to know weather there is JAVA API available that can do so? – asad Oct 16 '14 at 13:29
  • @IraBaxter Actually I want to generate CFG for all methods in class as I need to apply Code coverage tests on it. Am i going in right direction? kindly help – asad Oct 16 '14 at 13:32
  • Why do you need CFGs to produce code coverage? – Ira Baxter Oct 16 '14 at 14:35
  • ... If the problem is to produce "Code coverage", I'd start by writing basic function unit tests, see the coverage you have, and then worry about what is uncovered. If what you want to do is *path* coverage, that's another matter, but CFGs won't give it to you. – Ira Baxter Oct 16 '14 at 14:36
  • OK, what kind of "graph" do you mean? "Control flow graph" can either be a viewable flowchart or a data structure. – Hot Licks Oct 16 '14 at 15:20
  • @IraBaxter: Actually, I am working on a project aiming to check Control Flow testing of java code. For this purpose I need to generate automatic test suit generation aiming to cover path coverage,statement coverage and branch coverage of code. I just want to apply those automatic generated test suites on cfg to test coverage tests. – asad Oct 16 '14 at 18:18
  • @HotLicks: In the first stage, I need Control flow graph as a data structure. Later on I also need to visualize it also. – asad Oct 16 '14 at 18:21
  • Well, you make the data structure by identifying the "basic blocks" in the code (sequences of sequential statements with only one entry point and only one exit point) and then drawing the "directed arcs" that connect the blocks together. If there are loops the graph will be "cyclic". – Hot Licks Oct 16 '14 at 18:24
  • @HotLicks - In what format I need to generate CFG in order to check control flow code coverage, for testing? – asad Oct 17 '14 at 11:32
  • Well, it's up to you to pick a basic block structure (class). And it depends to a degree on what your parser has spit out. As I said below it's mainly a list of statements and a list of arcs (references to other blocks), though you also need bits and flags for your coverage analysis, etc. And you need to learn how to "walk" a tree with cycles in it. (You may want to somehow identify "backwards" arcs -- those that take you in a loop.) – Hot Licks Oct 17 '14 at 11:36

2 Answers2

1
doSomethingA;
while(B) {
    doSomethingC;
    doSomethingD;
    if (E) {
        doSomethingF;
    }
    else {
        doSomethingG;
    }
    doSomethingH;
    doSomethingI;
}
doSomethingJ;

Basic blocks:

  1. doSomethingA;
  2. while test
  3. doSomethingC; doSomethingD;
  4. doSomethingF;
  5. doSomethingG;
  6. doSomethingH; doSomethingI;
  7. doSomethingJ;

Arcs:

  • entry -> 1
  • 1 -> 2
  • 2 -> 3
  • 2 -> 7
  • 3 -> 4
  • 3 -> 5
  • 4 -> 6
  • 5 -> 6
  • 6 -> 2
  • 7 -> exit

As a data structure, a basic block has a list of statements and a list of exit arcs. One may also keep a list of entry arcs, for certain types of analysis, and one may choose to have a data structure to represent each arc, or simply have blocks point to other blocks.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • In Java "basic blocks" are terminated by function calls and operators that can blow up (divide, string subscript), because of the possibility of exceptions at every call site. You can build one that is more granular as you have suggested here, but it isn't clear that OP doesn't need the exception branches in order to do thorough testing. There are additional complications with "finally" blocks which act as implied subroutine calls from the non-exception and exception paths. I suspect anonymous classes add interesting flow arcs due to initializations. – Ira Baxter Oct 17 '14 at 00:39
  • @IraBaxter - It is not necessary to terminate at every operation that can "blow up", and doing so destroys any chance at optimization (since possible exceptions are so frequent). It is only necessary to assume that the flow through the block can be terminated at any point, and proceed directly to the *catch* point. IIRC, this can be treated as special exit from the block that propagates a special "vague" bitmask rather than the standard one that exits the bottom of the block. (It's been about 17 years since I wrote the verifier, so I forget the details.) – Hot Licks Oct 17 '14 at 00:50
  • Fine grain control graph does not destroy optimization opportunities; it just means you have to do them over *traces* (see trace scheduling). Alternatively you could model it with a "vague" exit bitmask, but then it isn't clear OP could generate all the paths he claims to test. – Ira Baxter Oct 17 '14 at 05:47
  • @IraBaxter - In any event, it's only necessary to do this for blocks that are visibly within a `try` range. – Hot Licks Oct 17 '14 at 10:52
  • I think you have to do it for all blocks. Those not within a *try* range have exception control transfers to what amounts to a special execption exit for the method. You'd certainly model a "return" statement with a control flow arc; why wouldn't you model an "exception return" similarly? – Ira Baxter Oct 17 '14 at 13:42
  • None of this tells OP really *how* to build the CFG; just that his constructed CFG "must look like this". He still has to climb over the Eclipse data structures, find all these "block" places and construct the graph. – Ira Baxter Oct 17 '14 at 13:44
  • If you have the AST you should have the info necessary to build the CFG structures without much assistance. And since I have no idea what his AST representation looks like, so I can't offer anything to help, beyond the above. – Hot Licks Oct 17 '14 at 15:33
0

yes, the is " code visualizer" you can use it with Java Eclipse to automatically generate CFG for code: https://marketplace.eclipse.org/content/control-flow-graph-factory