Is there a working C++ refactoring tool?

C++Refactoring

C++ Problem Overview


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".

C++ Solutions


Solution 1 - C++

Visual Assist and Visual Studio make dealing with large codebases much easier. Visual assist is good at tracking down how a class or member is used and is more effective at renaming it without false positives than search and replace.

Solution 2 - C++

I find Visual Assist X with Visual Studio very useful. Another choice is Refactor for C++.

Solution 3 - C++

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.

Solution 4 - C++

Mozilla has their own refactoring tool named Pork (Wiki, Developer Wiki). Here is the blog of the developer behind Pork. From what I've read Pork was successfully used in refactorings at Mozilla.

Pork should help if you come from *nix land, for Visual Studio I too recommend Visual Assist.

Solution 5 - C++

Our http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html">DMS Software Reengineering Toolkit is a transformation engine designed to carry out complex transforms over large bodies of code, including C++. It has been used to make reliable changes on systems of millions of lines of code. It operates by using compiler-accurate langauges analyzers and transformers.

It has a full C++ parser with name and type resolution, builds ASTs of code, can apply procedural or source-to-source transformations (with C++ surface syntax) to revise those trees, and regenerate compilable output with comments preserved. (Edit: 7/1/2011: Now does C++1X to the extent we understand the standard :)

It has been used on large scale reengineering projects, including C++ component re-architecting, and 100% fully automated translations between langauges. You can read about this at the website.

DMS is also used to build arbitrary source analysis tools. Examples include clone detection, test coverage, smart difference (comparision of source code structures and abstract editing operations rather than lines with simple insert and delete), etc.

What it is not (presently) is an interactive refactoring tool. We believe that to do most refactorings well, you need deep control and data fow analyses. DMS has generic machinery to support this, and that machinery is implemented for C, COBOL and Java at this point, with C++ being next in line. This is a tough job. You won't see a lot of serious C++ refactoring tools from anybody until this kind of problem has been solved well. First you need a full C++ parser :-}

EDIT 7/5/2011: Looks like we are going to take a run at the interactive version. We have won a Department of Energy Phase I SBIR to investigate how to do this. See http://science.energy.gov/sbir/awards-and-general-stats/fy-2011/phase-i-by-state/?p=1#tx (Look for Semantic Designs under "Texas"). Don't expect a result in a hurry; this is just the start of 3 phase multi-year program to get to a tool.

EDIT 8/11/2011: First progress... we now handle all of C++0x and OpenMP directives.

EDIT 1/4/2012: Does full control flow analysis of C++ code.

EDIT 9/15/2014: Now have C++14 front end parser/transformation engine well in hand. Even does rename pretty reliably at this point :-}

Solution 6 - C++

If you're using emacs, try Xrefactory . It supports method extraction, renaming of classes/functions/variables and insert/delete/move parameters.It also has very good/fast code completion engine.

Solution 7 - C++

Currently I can't recommend any refactoring tool for C++, certainly not for large code bases of 100k lines and above. I've been hoping this will change, like the OP, and I hope one day there will be something. I fear that the language itself might have to change significantly before we see any really good tools.

btw, has SlickEdit dropped its refactoring features?

Solution 8 - C++

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.

Solution 9 - C++

The http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html">DMS software rengineering toolkit does this I think. It is a code transformation engine, designed for large scale and handles C++. Have no idea how elegant the output is though.

Solution 10 - C++

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.

Solution 11 - C++

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

Solution 12 - C++

If you are using Visual C++ (Express Edition is free), you can use Visual Assist from www.wholetomato.com (link to the C++ refactoring features).

It has a 30 day trial period and we have found it to be faster and more feature-full that the built-in intellisense in the Visual C++ product itself.

Solution 13 - C++

If your looking to reengineer your codebase: MOOSE. But that's a large collection of analysis and reengineering tools, not an editor.

Solution 14 - C++

There is now a C++ refactoring extension for Visual Studio 2013 from Microsoft: http://visualstudiogallery.msdn.microsoft.com/164904b2-3b47-417f-9b6b-fdd35757d194

Solution 15 - C++

Definetely [Resharper Ultimate][1] is the way to go. Happiness guaranteed :)

In Beta version as of march 2015.

[1]: https://www.jetbrains.com/resharper/ "Resharper Ultimate"

Solution 16 - C++

CLion looks very promising.

Disclaimer: I've not tried it yet as I need to convert my projects to CMake format in order to use it.

Solution 17 - C++

I recommend you try Lattix. It allows you to analyze large C/C++ codebases to discover the archtecture, identify problematic dependencies, and re-engineer the code to improve modularity and reduce technical debt. Lattix also provides a number of algorithms to help in the refactoring process. These algorithms help you figure out how to move elements from one part of the hierarchy to another, to break cycles and to move subsystems so that the coupling and cohesion of subsystems can be improved. Here are the results of Lattix analyzing the Android Kernel (1.6 million LOC of C/C++). Full disclosure: I work for Lattix

Solution 18 - C++

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

Solution 19 - C++

I found the following plugin for Visual Studio 2013: Visual C++ Refactoring by Microsoft.

It is just a simple rename tool but it works flawlessy. It adds the following context menu after right-clicking on a symbol:

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRED SOFT ADAIRView Question on Stackoverflow
Solution 1 - C++Tom LeysView Answer on Stackoverflow
Solution 2 - C++GantView Answer on Stackoverflow
Solution 3 - C++HighCommander4View Answer on Stackoverflow
Solution 4 - C++Cristian AdamView Answer on Stackoverflow
Solution 5 - C++Ira BaxterView Answer on Stackoverflow
Solution 6 - C++bmericView Answer on Stackoverflow
Solution 7 - C++quamranaView Answer on Stackoverflow
Solution 8 - C++Nikolay BobovnikovView Answer on Stackoverflow
Solution 9 - C++Saltash MattView Answer on Stackoverflow
Solution 10 - C++randomView Answer on Stackoverflow
Solution 11 - C++muenalanView Answer on Stackoverflow
Solution 12 - C++JBRWilkinsonView Answer on Stackoverflow
Solution 13 - C++Stephan EggermontView Answer on Stackoverflow
Solution 14 - C++Robert HegnerView Answer on Stackoverflow
Solution 15 - C++hdoghmenView Answer on Stackoverflow
Solution 16 - C++AdamskiView Answer on Stackoverflow
Solution 17 - C++S BarowView Answer on Stackoverflow
Solution 18 - C++PeterSomView Answer on Stackoverflow
Solution 19 - C++ZacView Answer on Stackoverflow