Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically refactor C++ classes into separate files

I've inherited a fairly big project with tens-of-thousands of lines, but the previous developers packed them into 6 files only (OMG!)...

So for the sake of readability I would like to automatically split all the classes into their own cpp/h files.

Is there any tool capable of such a thing?

like image 711
er453r Avatar asked Nov 27 '22 19:11

er453r


1 Answers

What you need in order to refactor your C++ code base is a tool that can parse C++, retaining the headers files, yet follow the #includes and process them, too. You have to construct an "a-depends-on-b" mapping for every pair of dependent items, and then you have to cluster them. Finally, you have to be able to regenerate the source code according to the clustering.

And you want to do this over a complete code base, not just one compilation unit.

This is a pretty hard tool to find :-} I don't think you'll find it off the shelf.

Our DMS Software Reengineering Toolkit has much of this. It is a general program analysis and transformation system, has a full C++ front end, and has a controllable preprocessor, so it can expand/not expand the #includes as necessary. It can capture a-depends-on-b relations (we've done this on a C system of 26 million lines of code), and it has the proven capability to reason about preprocessor conditions symbolically; there's the potential to capture "a-depends-on-b if ".

With that information, you need clustering. I think to do this, you need some kind of generic clustering algorithm, and some user input to override it ("I insist, leave this #include file alone, and these two apparantly unrelated things go together"). The program ransformation part of DMS can then be used to mechanically split code and include files into grouped, dependent declarations.

DMS is a complex tool; after all, it has to deal with nasty things like C++. Configuring it to do the above tasks isn't trivial, but I think it within technical reach.

However, none of this will help you with your immediate issue of acquiring a tool to help you solve your problem today.

like image 98
Ira Baxter Avatar answered Dec 21 '22 11:12

Ira Baxter