Skip to content

Navigation Menu

Sign up
This repository was archived by the owner on Jun 4, 2019. It is now read-only.
pad edited this page Jan 14, 2011 · 59 revisions

The goal of spatch is to allow programmers to express and perform complex refactorings while using a syntax they already are familiar with, the patch syntax. For instance to remove everywhere the second argument of a function foo one can write this syntactical patch:

 //remove_second_arg_foo.spatch
 foo(X
- ,Y
 )

and then apply it on a codebase with:

$ spatch -c remove_second_arg_foo.spatch *.php

This works even if the function call is splitted on multiple lines or has extra spaces between the comma and the second expression, because spatch works at the abstract syntax tree level, not at the token or string level like patch or sed.

One could also write it as:

// remove_second_arg_foo_alt.spatch
- foo(X,Y)
+ foo(X)

Most programming languages do not have refactoring tools and when they have, like Java with Eclipse, the programmer is often limited to a restricted set of refactorings such as "dropping an argument", "adding an argument", "move a function". Just like for Sgrep, we want to easily express complex code patterns but also source-to-source transformation on those patterns in a flexible way.

Spatch is domain specific language to express such refactorings.

The synopsis is:

$ spatch [-apply_patch] [-lang <lang>] -c <spatch_file> <files_or_dirs>

By default spatch generates a diff on stdout. Once you are confident that your syntactical patch is correct, you can then use the -apply_patch to actually modify the relevant files.

For now only PHP is supported

One can write any PHP expressions inside a syntactical patch and annotate subparts of it with - and + any way you want.

For instance with this spatch:

f(2,
- foo(1)
+ foo(2)
)

we want to replace every calls to foo(1) by foo(2) but only when the call is nested inside a specific kind of calls to f, the ones where the first argument of f is 2.

On this file:

<?php
f(2, foo(2));
f(1, foo(1));
f(2, foo(1));
f(2,
 foo(1));

spatch will generate:

$ ./spatch -c tests/php/spatch/foo.spatch tests/php/spatch/foo.php
--- tests/php/spatch/foo.php 2010年11月04日 22:58:16.000000000 -0700
+++ /tmp/trans-31284-13ff71.php 2010年11月04日 23:12:35.000000000 -0700
@@ -5,8 +5,8 @@
 f(1, foo(1));
- f(2, foo(1));
+ f(2, foo(2));
 f(2,
- foo(1));
+ foo(2));

Just like for Sgrep, spatch supports metavariables so you can write syntactical patches like:

// remove_second_arg_foo_alt.spatch
- foo(X,Y)
+ foo(X)

You can use metavariables in place of full PHP expressions.

You can also use metavariables for XHP attribute values as in:

 <ui:section-header
- border=X
 ></ui:section-header>

The principle of spatch is to take a pattern file, the spatch file, and match it over a source file. By using metavariables we get a more flexible pattern that can accomodate more source files. In the same way even if the spatch file contains extra spaces between tokens, or if an expression is split on multiple lines, it will still match source files using a different indentation style because spatch like sgrep works at the AST level.

Here are a few other tricks done by spatch called isomorphisms which allow the pattern to accomodate more source files:

In XHP, attributes can be given in any order but we actually don't care about that order. When we write a spatch like:

<x:frag border=1 foo=2></x:frag>

we want it to match even code like:

<x:frag foo=2 border=1></x:frag>

Actually we also want it by default to match code like:

<x:frag foo=2 bar=3 foobar=4></x:frag>

or code like:

<x:frag foo=2 border=1 foobar=3>this is a body</x:frag>

To accomodate those needs the spatch code matching engine has hardcoded a few equivalences (isomorphisms) regarding XHP.

spatch is significantly slower than tools like sed because it works on a more complex structure than a stream of characters, the abstract syntax tree. Nevertheless you can combine it with git grep piped to xargs to speedup things:

$ git grep -l foo |xargs spatch -c remove_second_arg_foo.spatch

If the syntactical patch notation is not expressive enough for your refactoring needs, you can still express the refactoring by using the internal pfff API that works on the ASTs of the source code.

Here is the content of pfff/demos/simple_refactoring.ml which explains how to use the internal OCaml pfff API to perform a simple refactoring: http://github.com/facebook/pfff/commit/c7b66cb5471e390a83fd0379754135224a1b34f0

Just Like for sgrep, generalizing spatch patterns to the full PHP language, not just PHP expressions, so one can refactor class definitions, function headers, statements, etc.

spatch is a continuation of the work I've done on coccinelle, an advanced refactoring tool for C http://coccinelle.lip6.fr/ I co-designed with Julia Lawall.

Related tools:

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /