INRIA / spoon

Spoon is a metaprogramming library to analyze and transform Java source code. :spoon: is made with :heart:, :beers: and :sparkles:. It parses source files to build a well-designed AST with powerful analysis and transformation API.
http://spoon.gforge.inria.fr/
Other
1.74k stars 348 forks source link

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

Open nashid opened 3 years ago

nashid commented 3 years ago

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 commented 3 years ago

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

slarse commented 3 years ago

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?

nashid commented 3 years ago

Thanks, @slarse. Looking forward to the feedback.

andrewbwogi commented 3 years ago

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.

slarse commented 3 years ago

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.

nashid commented 2 years ago

@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?

will-leeson commented 1 year ago

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?