Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tool to parse C++ source and move in-header inline methods to the .cpp source file? [closed]

The source code of our application is hundreds of thousands of line, thousands of files, and in places very old - the app was first written in 1995 or 1996. Over the past few years my team has greatly improved the quality of the source, but one issue remains that particularly bugs me: a lot of classes have a lot of methods fully defined in their header file.

I have no problem with methods declared inline in a header in some cases - a struct's constructor, a simple method where inlining measurably makes it faster (we have some math functions like this), etc. But the liberal use of inlined methods for no apparent reason is:

  • Messy
  • Makes it hard to find the implementation of a method (especially searching through a tree of classes for a virtual function, only to find one class had its version declared in the header...)
  • Probably increases the compiled code size
  • Probably causes issues for our linker, which is notoriously flaky for large codebases. To be fair, it has got much better in the past few years, but it's not perfect.

That last reason may now be causing problems for us and it's a good reason to go through the codebase and move most definitions to the source file.

Our codebase is huge. Is there an automated tool that can do (most of) this for us?

Notes:

  • We use Embarcadero RAD Studio 2010. In other words, the dialect of C++ includes VCL and other extensions, etc.
  • A few headers are standalone, but most are paired with a corresponding .cpp file, as you normally would. Apart from the extension the filename is the same, i.e., if there are methods defined in X.h, they can be moved to X.cpp. This also means the tool doesn't have to handle parsing the whole project - it could probably just parse individual pairs of .cpp/.h files, ignore the includes, etc, so long as it could reliably recognise a method with a body defined in a class declaration and move it.
like image 773
David Avatar asked Jan 13 '12 15:01

David


2 Answers

You might try Lazy C++. I have not used it, but I believe it is a command line tool to do just what you want.

like image 129
Nathan Monteleone Avatar answered Oct 31 '22 23:10

Nathan Monteleone


If the code is working then I would vote against any major automated rewrite.
Lots of work could be involved fixing it up.

Small iterative improvements over time is a better technique as you will be able to test each change in isolation (and add unit tests). Anyway your major complaint about not being able to find the code is not a real problem and is already solved. There are already tools that will index your code base so your editor will jump to the correct function definition without you having to search for it. Take a look at ctags or the equivalent for your editor.

  • Messy

    Subjective

  • Makes it hard to find the implementation of a method (especially searching through a tree of classes for a virtual function, only to find one class had its version declared in the header...)

    There are already tools available for finding the function. ctags will make a file that allows you to jump directly to the function from any decent editor (vim/emacs). I am sure your editor if nto one of these has the equivalent tool.

  • Probably increases the compiled code size

    Unlikely. The compiler will choose to inline or not based on internal metrics not weather it is marked inline in the source.

  • Probably causes issues for our linker, which is notoriously flaky for large codebases. To be fair, it has got much better in the past few years, but it's not perfect.

    Unlikely. If your linker is flakey then it is flakey it is not going to make much difference where the functions are defined as this has no bearing on if they are inlined anyway.

like image 2
Martin York Avatar answered Oct 31 '22 21:10

Martin York