Oink-based Patch Generation

For Mozilla 2, I proposed that we use Oink, or really Elsa, to build a tool that can help automate deCOMtamination, switching to C++ exceptions instead of nsresults, and similar tasks beyond the reach of sed or perl. The idea is to specify pattern and replacement abstract syntax trees (ASTs), and have the tool match the pattern against sub-tree instances, producing a version of the source with the replacement AST pretty-printed instead of the matching sub-tree.

Graydon came up with the insight of using source coordinates, which the Oink framework tracks accurately, to tell which lines in a given primary source (.cpp file, not .i file that results from the C pre-processor) correspond to a particular AST. The tool would read the source, counting lines until the range to delete is reached, then copy each such line matching the pattern to replace with a “- ” prepended. Then write lines pretty-printed from the replacement AST with “+ ” prepended. Add a bit more boilerplate and context lines, and you have a patch.

Graydon didn’t just make this breakthrough, he prototyped it, with a hardwired pattern to replace struct foo pointers with struct bar pointers in a test file:

$ cat test.cc
struct foo {};
struct holdsAFoo
{
char x;
static const foo * const y;
int *z;
};
$ ./staticprint -fs-print-rewrite test.cc
--- test.cc
+++ test.cc
@@ -5,1 +5,1 @@
-  static const foo * const y;
+static struct bar const * const y;
$ ./staticprint -fs-print-rewrite test.cc | patch -p0
patching file test.cc
$ cat test.cc
struct foo {};
struct holdsAFoo
{
char x;
static struct bar const * const y;
int *z;
};

All this wants is an Oink tool of its own (instead of a hack to staticprint), a little more indentation fidelity (bonus points for respecting the source file’s Emacs and vim modelines), C++ purity (no struct before bar needed), patch context and hunk coalescing, and generalized AST pattern/replacement options, and we’ll be able to make major systematic changes by generating patches. Neat!

/be

2 Replies to “Oink-based Patch Generation”

Comments are closed.