Clang as a scripted refactoring tool

The mozilla folks have two complementary tools, Dehydra/Treehydra,
that let them add new static checking features to C++ via javascript.
They write how an annotation should work in javascript and then the
tools enforce it. I'd like to see an extended version of that idea --
using a high level language like javascript or python to be able to
describe refactorings specific to the task at hand. Beyond simple
class renaming I'd like to be able to say, refactor classes that use a
visitor pattern to use signals, or write a simple script to find
classes that have virtual functions and update them to have a virtual
destructor if they don't already. I asked the mozilla guys about this
on IRC and they suggested Clang is already part way there. What's the
state of using Clang for refactoring and what parts of the code base
should I be looking at if I'm interested in trying to build something
like this?

Hi,

Clang is designed to be usable as the basis for a refactoring tool. Your best bet is to look at the Rewriter component. It allows you to create edits of the source code, based on location information stored in the AST. For an example, look at the Obj-C rewriter, a tool that rewrites Obj-C into pure C.

Sebastian

I asked the mozilla guys about this
on IRC and they suggested Clang is already part way there. What's the
state of using Clang for refactoring and what parts of the code base
should I be looking at if I'm interested in trying to build something
like this?

Hi,

Clang is designed to be usable as the basis for a refactoring tool. Your best bet is to look at the Rewriter component. It allows you to create edits of the source code, based on location information stored in the AST. For an example, look at the Obj-C rewriter, a tool that rewrites Obj-C into pure C.

Minor note. It rewrites into C++.

- Fariborz

Thanks guys I'll take a look :slight_smile: