As I've pointed out - here - it seems clang's libclang should be great for implementing the hard task that is C/C++ code analysis and modifications (check out video presentation and slides).
Do you know of any C/C++ refactoring tool based on libclang ?
"Any" includes even simple alpha state project, with support of one refactoristation technique. It can be without preprocessor support. As an example of the functionally about which I'm talking: changing method names, whether it supports multiple files or only one file at a time. You might be wondering what the goal is of asking for even small working examples My thought is that creating a list of code examples and small tools that are in one place will provide a better resource to learn how to implement refactorisation with libclang. I believe that from simple projects might grow bigger projects - in a proper opensource manner :).
Clang contains a library called "CIndex" which was developed, I believe, for doing code completion in IDEs. It can also be used for parsing C++ and walking the AST, but doesn't have anything in the way of refactoring. See Eli Bendersky's article here.
I have started such a project recently: cmonster. It's a Python-based API for parsing C++ (using libclang), analyzing the AST, with an interface for "rewriting" (i.e. inserting/removing/modifying source ranges). There's no nice way (yet) for doing things like modifying function names and having that translated into source-modifications, but it wouldn't be terribly difficult to do that.
I have not yet created a release with this functionality (although it's in the github repo), as I'm waiting for llvm/clang 3.0 to be released.
Also, I should point out a couple of things:
Adjust expectations appropriately.
Update: cmonster 0.2 has been released, which includes the described features. Check it out on Github.
Google have been working on a tooling library for Clang. In since the 3.2 release. It includes a ASTMatchers library so you can just build up a query and don't have to walk the AST.
There is a great video talk on the subject that walks through a simple rename example. (This is from the same guy as the MapReduce talk posted above but is newer and more about a simple practical implementation rather than the internal design and enterprise scale stuff Google have going on).
The source for that example that renames a method is available in the tooling branch. It may be somewhere in the trunk but I can't find it. Also Rename the getDeclAs function to getNodesAs as the other is apparently deprecated.). There is a more advanced example that removes duplicated c_str calls (which is in trunk and someone posted above).
Here is documentation for LibASTMatchers and LibTooling.
EDIT: Some better docs for ASTMatcher. Here and here.
EDIT: Google are now working on something called Clangd which aims to be some kind of Clang server for refactoring.
Google made a Clang based refactoring tool for their C++ code base and plans to release it. I don't know the current state of the project, but you can see this demo presented on the 2011 LLVM Developers Meeting: https://www.youtube.com/watch?v=mVbDzTM21BQ.
Also, XCode's (4+) built-in auto-completion and refactoring functions are based on libclang.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With