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.75k stars 349 forks source link

feature: add support for Coccinelle-like semantic patches mixing matching (TemplateMatcher) and transformation (Template) together #1918

Open monperrus opened 6 years ago

monperrus commented 6 years ago

Imagine that you want to match a code location and transform it at the same time, this is sometimes called a "semantic patch" (as opposed to a line-based patch).

In the C world, there is an excellent tool for this, called Coccinelle, see http://coccinelle.lip6.fr/.

In Spoon, it would mean creating an abstraction that would gather a TemplateMatcher and Template in a same object. In other terms, we are really close to have "semantic patches" in Spoon.

@pvojtechovsky since you're extensively working on this part, what do you think?

pvojtechovsky commented 6 years ago

I am not sure if I understood you well. But have a look at https://github.com/pvojtechovsky/spoon/blob/5919ed784e5fde86e4b9246c0ea24c989f269a45/src/test/java/spoon/test/template/CodeReplaceTest.java testTemplateReplace

There are two patterns, first is matching (founds a code location and takes current parameters) and second generates new code (using own template and current parameters) and then old code is replaced by new code.

pvojtechovsky commented 6 years ago

Have a look at https://github.com/pvojtechovsky/spoon/blob/5919ed784e5fde86e4b9246c0ea24c989f269a45/src/main/java/spoon/pattern/node/RootNode.java too It is the core interface of Pattern node, which shows that Pattern can always do both matching and generating:

public interface RootNode extends Matchers {
/**
     * Generates zero, one or more target depending on kind of this {@link RootNode}, expected `result` and input `parameters`
     * @param generator {@link Generator} which drives generation process
     * @param result holder for the generated objects
     * @param parameters a {@link ParameterValueProvider} holding parameters
     */
    <T> void generateTargets(Generator generator, ResultHolder<T> result, ParameterValueProvider parameters);
/**
     * @param targets to be matched target nodes and input parameters
     * @param nextMatchers Chain of matchers which has to be processed after this {@link RootNode}
     * @return new parameters and container with remaining targets
     */
    TobeMatched matchTargets(TobeMatched targets, Matchers nextMatchers);
monperrus commented 6 years ago

Patterns (#1686) are almost this. The final step is to given then a Coccinelle look'n'feel.

monperrus commented 5 years ago

Documentation about SmPL: http://coccinelle.lip6.fr/documentation.php

monperrus commented 5 years ago

See also Automating Program Transformation for Java Using Semantic Patches https://hal.inria.fr/hal-02023368v1

monperrus commented 5 years ago

FYI new version of @JuliaLawall's paper: Semantic Patches for Java Program Transformation

monperrus commented 3 years ago

FYI, a paper on that topic "Semantic Patches for Adaptation of JavaScript Programs to Evolving Libraries." https://cs.au.dk/~amoeller/papers/jsfix/paper.pdf