spoon icon indicating copy to clipboard operation
spoon copied to clipboard

Can Spoon be used to extract control-flow and basic data-flow graphs?

Open nashid opened this issue 4 years ago • 7 comments

I am new to Spoon and this library looks great. I need to extract control-flow and basic data-flow graphs i.e. def-use for intraprocedural analysis.

My intention is to feed the enclosing function body of the java source code.

I am wondering whether Spoon is a good fit to extract control-flow and data-flow edges?

nashid avatar Jun 30 '21 02:06 nashid

Also, can I extract control-flow and data-flow graphs to a dot or JSON files?

nashid avatar Jun 30 '21 06:06 nashid

Hi @nashid, welcome to Spoon!

@andrewbwogi you've been digging around the spoon-control-flow lately, do you know the answer to these questions off the top of your head?

slarse avatar Jul 01 '21 11:07 slarse

Thanks, @slarse. Looking forward to the feedback.

nashid avatar Jul 01 '21 19:07 nashid

Hello @nashid, spoon-control-flow seems too basic for interprocedural analysis. For instance, method calls are represented as single nodes and not full graphs. Analysis tools are few. The central purpose of this module is to generate diagrams for visualization, which it does in the DOT language.

The data-flow component is limited to extracting initialized variables from a sub-graph. It does not produce data-flow graphs.

I think that it is better to see this module as a prototype demonstrating how Spoon can be used for control-flow and data-flow purposes.

andrewbwogi avatar Jul 02 '21 07:07 andrewbwogi

Thanks @andrewbwogi!

So, @nashid, I think the short answer to your question is that yes, you can do these forms of analyses with Spoon, but the tools we have available off-the-shelf aren't quite sophisticated enough.

slarse avatar Jul 02 '21 07:07 slarse

@andrewbwogi @slarse do you think in the near future Spoon would address these gaps? There are so many tools in the JVM ecosystem and yet there is no tool that is easy to use and works.

Alternately, could you point me to any tool/library resource that can generate control flow and data flow reliably for JDK8+ code?

nashid avatar Nov 11 '21 05:11 nashid

Sorry to dredge up an old issues, but I'm looking for a way to take a given program and convert it to a CFG in dot format. This thread makes me believe this is possible, but I am confused on how to do so. I tried something like this,

        Launcher launcher = new Launcher();

        launcher.addInputResource([/path/to/file.java]); 
        launcher.getEnvironment().setComplianceLevel(17);
        List<CtElement> myModel = launcher.getModel().getElements(new TypeFilter<CtElement>(CtElement.class));

        for(CtElement element : myModel ){
            ControlFlowBuilder builder = new ControlFlowBuilder();
            GraphVisPrettyPrinter myPrettyPrinter = new GraphVisPrettyPrinter(builder.build(element));

            System.out.println(myPrettyPrinter.print());
        }

but it only return two small, graphs with only a begin and end node. Could you clarify?

will-leeson avatar Feb 24 '23 19:02 will-leeson