163

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

RED SOFT ADAIR
  • 11,294
  • 10
  • 48
  • 83
  • 11
    Favorite'd this one. The problem with C++ is its very complex, context-sensitive syntax. Without actually parsing the full source, you cannot be sure what an identifier means. – DevSolar Sep 07 '09 at 09:44
  • See also: http://stackoverflow.com/questions/249827/are-there-c-free-and-fast-maybe-standalone-refactoring-tools-for-windows but it doesn't have much to offer – dmckee --- ex-moderator kitten Sep 07 '09 at 15:14
  • 4
    Eclipse actually works pretty well, you just need to allow it to use more memory by editing eclipse.ini – n0rd Sep 08 '09 at 10:46
  • 3
    If you're still monitoring refactoring tools for C++, please consider that we at JetBrains are currently working on [C++ support in ReSharper](http://blogs.jetbrains.com/dotnet/2013/06/resharper-to-support-c/) and a [separate cross-platform C++ IDE](http://www.jetbrains.com/objc/features/cpp.html) – Jura Gorohovsky Jul 02 '13 at 11:34
  • 1
    @RED SOFT ADAIR you are famous. Check this https://youtu.be/RT46MpK39rQ?t=1611 – George C. Sep 13 '19 at 05:49

19 Answers19

42

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.

Tom Leys
  • 17,070
  • 6
  • 38
  • 60
38

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

Gant
  • 28,894
  • 6
  • 44
  • 63
25

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.

Cœur
  • 32,421
  • 21
  • 173
  • 232
HighCommander4
  • 44,537
  • 22
  • 112
  • 180
17

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.

Cristian Adam
  • 4,589
  • 19
  • 18
9

Our 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 :-}

Ira Baxter
  • 88,629
  • 18
  • 158
  • 311
  • There doesn't appear to be a link for evaluating this tool on your download page http://www.semanticdesigns.com/Products/RegisterEval.html Is a trial version and pricing available at this time for VS2010 or VS2008? – SmacL Apr 11 '12 at 09:33
  • For a refactoring tool, "don't expect a result in a hurry". Our Phase I R&D task is over, and we're applying for funds for Phase II. To discuss DMS and means to evaluate it, contact the company. It isn't the kind of thing you "download to evaluate", in the same way that Enterprise Oracle isn't a download item. – Ira Baxter Apr 11 '12 at 14:20
  • ... DMS is able to transform VS2008/VS2010, but not as an interactive tool, and there are limitations with respect to preprocessor directives. (The good news is that we think we solved a major issue with these, and will be applying that in a Phase II R&D program). – Ira Baxter Apr 11 '12 at 14:31
  • Thanks for the feedback, but it doesn't look like the type of tool I'm after. I've no need for interactive, just something that can perform reasonably simple transformations on a large code base, without costing an arm and a leg, e.g. changing the number of parameters a call takes, changing a pointer parameter to a reference. VisualAssist is close but just needs to go a bit further. – SmacL Apr 11 '12 at 17:49
  • "reasonably simple transformations"? You may think these are simple. And you may think that "reasonable". There are many people that have tried to build C++ refactoring tools, and none of the ones out there do your "reasonably simple" thing. Perhaps... it is not so simple? How do you know that a particular function call is the one of the ones needing changing, without implementing the complete front end of a C++ compiler in terms of name lookup? (In fact, DMS can do this, modulo the preprocessing issues. I don't know about the other tools. It is not "simple"). – Ira Baxter Apr 12 '12 at 06:45
  • Well, for the simple transformations I've needed to do in the past I've written ad hoc programs that have worked fine, based having extensive test suites to verify the result. For example, replacing literal text with calls to read resources. Yes there are numerous edge cases where the transformation might fail, but it still beats doing things manually. I went into some more detail on the whole tomato forum here;http://www.wholetomato.com/forum/topic.asp?TOPIC_ID=9962 – SmacL Apr 12 '12 at 10:48
  • So, there's the issue. If you don't mind your refactoring steps to be unreliable, then almost any tool will do including a text editor. You do that and then use "extensive test suites". The definition of refactoring is "code changes that don't affect functionality"; you shouldn't need test suites to verify the refactoring actions. The promise of refactoring is worry-free code changes. That's hard. – Ira Baxter Apr 12 '12 at 11:29
  • My main reference for refactoring is Fowler's book on the subject, which includes a very similar definition. While I get the principal, refactoring techniques are also commonly used to make wholesale changes to a code base that sometimes do make subtle changes to the functionality. e.g. my previous example of replacing literal text with external resources. Fowler also has an entire chapter on testing which starts with the line 'If you want to refactor, the essential precondition is having solid tests'. I'd agree entirely. – SmacL Apr 12 '12 at 11:56
  • Now if you can come up with a tool that obviates the need for testing the results, that would be fantastic but I'd doubt that it would work. The reason being that you'd also have to ensure the refactored code had the same performance characteristics (speed / resource usage) as a change in performance can easily lead to a functional issue. Anyway, best of luck with it, it's certainly a worthy software engineering endeavour, and I look forward to the day when it is available to hacks like myself on a tight budget. – SmacL Apr 12 '12 at 12:03
  • I agree that "non-refactoring" transformations ("those that change the code functionality") are as interesting or more interesting than refactorings. You still want the transformations that do this, to do whatever they do reliably. Its clear that if your transformation is, "for calls to foo, add an extra argument bar, that if true causes foo to produce zero, and if not produce what old foo produces". Such changes are (arguably) clear to the author of the transformation, and shouldn't require testing to verify it was done right. Performance changes are sometimes an issue, as you point out. – Ira Baxter Apr 13 '12 at 23:24
  • 3
    ... the purpose of DMS is enable the coding and error-free application of code transformations, whether "refactoring" or "not". Yes, its hard and we aren't there yet. We're closer IMNSHO than any other tool I've seen for C++, and there's nothing wrong with our tool foundations. What's left are nasty problems with the preprocessor, code scale, and effort to code the semantics of C++ adqueately to allow such transformations. And I'll be the first to say this is a BIG project. – Ira Baxter Apr 13 '12 at 23:25
  • Thanks for all the feedback, and good luck on what is a major undertaking. That aside, I think there is a clear space in the market for a 'hackier' solution that allows major transformations of a code base to a new copy of that code base, even if they end up creating a frankenstiens monster that doesn't even compile on the first few iterations. VA is great, but also plays it safe and is quite limited as it is operating on the live code base. So until a decent solution such as yours becomes available to the likes of myself, home-made hacks combing various tools seem like the only solution. – SmacL Apr 14 '12 at 10:13
8

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.

bmeric
  • 1,090
  • 1
  • 17
  • 27
6

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?

quamrana
  • 27,260
  • 11
  • 50
  • 62
  • The language *did* change significantly, with the release of the C++ 2011 standard. The additional complications makes the problem harder, not easier, because all the users will want all the advanced language features, *and* a refactoring ability :-{ – Ira Baxter Jan 05 '12 at 08:24
  • 2
    Having recently read up on Slickedit's capabilities, it appears that it determines the definition corresponding to the use of a name by heuristic methods. It does not do a "correct" C++ name lookup. This means you can't trust its transformations to be correct. If you don't mind inspecting its work, this may be OK for you. On really big software packages, this is probably not good. My opinion. – Ira Baxter Apr 12 '12 at 06:50
4

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.

3

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

Ira Baxter
  • 88,629
  • 18
  • 158
  • 311
  • 1
    DMS has been used to automate the rearchitecting of large bodies of C++ code. See Akers, R., Baxter, I., Mehlich, M. , Ellis, B. , Luecke, K., Case Study: Re-engineering C++ Component Models Via Automatic Program Transformation, Information & Software Technology 49(3):275-291 2007. Available from publisher – Ira Baxter Oct 10 '09 at 17:10
  • Dunno what you mean by "elegant output". The output you get from DMS clearly depends on what you give it, and what transformations you apply. If the transformation don't touch part of the code, that part of the code is as elegant or not as it was in the first place. If it does touch the code, the elegance is determined by how clever the transformations are, just as a coder's manual changes would be. I don't think of this is elegance, but the *formatting* of the result can either be "fidelity" printing (the original format), or a defined prettyprinting style. – Ira Baxter Jul 07 '11 at 16:56
3

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.

random
  • 3,142
  • 1
  • 17
  • 32
2

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

muenalan
  • 500
  • 4
  • 7
1

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.

JBRWilkinson
  • 4,705
  • 1
  • 23
  • 31
  • 2
    I didn't think you could use plugins with the express edition – Yacoby Sep 07 '09 at 10:09
  • 3
    I've certainly tried to install VA with VS C++ express in the past and it didn't work. – quamrana Sep 07 '09 at 10:14
  • You cannot install Visual Studio plugins in isolated mode (the Express editions all run the IDE in isolated mode). If you're a student you *can* download full versions of Visual Studio though from [DreamSpark](http://bit.ly/dYpBTO) or MSDNAA. – Billy ONeal Feb 10 '11 at 06:53
1

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

Stephan Eggermont
  • 15,354
  • 1
  • 33
  • 64
1

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

Robert Hegner
  • 8,196
  • 6
  • 51
  • 89
  • Yes, i've seen this, but it is just a first try and covers only "rename". No Extract function etc. But i am looking forward for more to come... – RED SOFT ADAIR Dec 04 '13 at 10:07
1

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

In Beta version as of march 2015.

hdoghmen
  • 2,525
  • 4
  • 26
  • 28
1

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.

Adamski
  • 3,307
  • 2
  • 29
  • 72
1

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

S Barow
  • 11
  • 3
1

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

PeterSom
  • 1,939
  • 16
  • 16
0

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

Zac
  • 4,076
  • 3
  • 33
  • 40