Is there a working C++ refactoring tool?

Viewed 56584

Does anybody know a fully featured refactoring tool for C++ that works reliably with large code bases (some 100.000 lines)?

I tried whatever i can find again and again over the last years: SlickEdit, Eclipse CDT. They all were not at all usable.

SUMMARY: I took time and evaluated "Visual Assist X" as well as "Refactor for C++". Both have some impressing features, but both as well are far from perfect. Extracting a large block of code usually is not done satisfying without manual modifications - and therefore does not pay off.

"Visual Assist X" has nice features such as much more complete autocompletition etc. But it leads to so much flickering and slows down much at certain points.

By my opinion therefore the answer is: "No, there is no production ready refactoring tool for C++"

UPDATE March 2015 As for hdoghmens reply today i tried Resharper for C++. His link https://www.jetbrains.com/resharper/ does not say anything about C++. But i found Resharper C++ that was announced more than a year ago here:

https://www.jetbrains.com/resharper/features/cpp.html

I gave it a try with VC2010 using a code base of 20MB.

Test 1: Extract Method: results in a Resharper exception. No source code changed.

Test 2: Extract Method with different source: Works fine

Test 3: Change signature of extracted function: Results in broken C++ code:

bool myclass::do_work123(<unknown long Color>int& Filled*&, long, int&)

Maybe thats why C++ its not listed on the main page.

By my opinion the answer to this question still is "NO".

19 Answers

I expect clang will significantly change the landscape of C++ refactoring tools out there over the next couple of years. It's an open-source, modular compiler that exposes an API for parsing and semantically analyzing C++ code. IDEs and other tools will be able to use this API rather than doing the difficult work of writing their own parser and semantic analyzer.

Google already made a large-scale refactoring tool using clang.

I recommend to try rtags if you use emacs and haven't tried it yet (there is also a package for vim available). It is a clang based client/server application that indexes C/C++ code, with these features included:

  • go to definition/declaration
  • find all references, go to next/previous
  • rename symbol
  • integration with clang’s “fixits”

I decided to give it a try after watching this talk which introduced rtags (and emacs) for me.

(I have to say that I went this far only after my QtCreator failed to rename some symbols properly, which is a show-stopper for my using this great IDE for now)

Besides what is supported by rtags, I also need some additional neat features, including:

  • create function definition/prototype
  • extract function
  • create getter/setter methods

For these, I recommend to use a semantic-refactor package for emacs (not sure if there are alternatives for vim)

Generally, clang based tools looks very promising. If you are interested in more information about clang tools for C++ refactoring, including for projects with large codebase, there are some great talks by Chandler Carruth.

The problem are C++ templates. As of 2019 I'm not aware of any refactoring tool that supports C++ templates. I've tried VS2019, VisualAssist, Clion, QtCreator.

Consider example:

#include <iostream>

struct foo { void print() {} };
struct bar { void print() {} };

template <typename T>
void call_print(T&& v) { v.print(); }

void print() {}

int main()
{
    call_print(foo{});
    call_print(bar{});
    return 0;
}

If I run Rename Refactoring on foo::print, bar::print should be also renamed automatically. Because they are linked through call_print function template instantiations.

One surely has to mention Klocwork as a commercial code refactoring suite. It does look very promising when you go through the demo video.

Sorry to only find this question so late. My students and assistants work on C++ refactoring since about 2006. Most of CDTs refactoring infrastrucure was built by my team at IFS institute of software. since a couple of years we provide Cevelop our version of CDT with support for C++ code modernization refactorings etc. Cevelop can work with large code bases, if workspace is set up correctly. Free available at https://cevelop.com

Related