Using Clang for a C Refactoring Tool

Additionally, I suspect we’d welcome code to do more in this area into the tree. I

think clang would be a good place to do this.
Also, because clang (and llvm) is built and designed as a set of
useful libraries for solving all sorts of problem, if there is a rough
area where the design could be improved to meet your specific needs, I
think we are interested in have that flexibility in clang.
Refactoring is my goal as well. I was originally attracted to clang because its design yields a much more expressive tree structure with detailed source locations, this makes refactoring easy in many situations. I had a brief list of refactorings that I had worked on, for example renaming functions and variables, moving sections of code to different files and listing #include’s which are not required in a given translation unit. If there was some community interest in this, I would very much like to contribute my work. One area which I have not spent enough time thinking about is what an interface for refactoring should look like. If people had some suggestions regarding what they would like to see and how the respective pieces should interact I would appreciate it.

This is very interesting, I’d love to see what you’ve been working on!


Same here! If only to see how easy/hard it is to build a refactoring
tool using Clang.

Ha ha, I’d like to chime in, that I too had a similar idea. I wanted to create a framework and a visual representation of source files for manual auditing and browsing of source. The idea is to better model code/data flow, abstract away the notion of files/modules and in general make code browsing akin to following symbolic execution statically.

Is anyone planning to make what you are talking about an open project or does anyone else share an interest for this?

I’d love to know.


UIs are hard and hard to test. If you take the approach of simple text in and out, a GUI is free to hook up a pipe to it and a testsuite is that much more manageable. Now, since clang/llvm is a library, the notion of hooking up a pipe is silly, but, conceptually you can think of the interface that way, and in practice, running the testsuite this way I think is worthwhile.

Any GUI code should be independent (lay on top of the library interface for refactoring).


I believe Patrick was asking about a refactoring API (not refactoring GUI).


You might want to look @ Rewriter. The Rewriter class provides a foundation for many types of refactoring. RewriteTest is a fairly sophisticated client of the Rewriter that "refactors" ObjC AST's to C source code (without disturbing white space, comments, preprocessor directives, macros, etc.). It operates on each module in isolation.

That said, we don't currently have an API for traditional refactoring operations that span multiple modules (like rename global variable from "this to that" for an entire project). Creating some higher level API's would be nice (but need to be driven by the feature/task).

Emerson might be able to give you some more info. He is becoming familiar with clang and understands the refactoring domain.