Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate a list of C++ symbols for mass refactoring/renaming

Background

I have inherited a legacy 60kloc g++ project which I would like to refactor to enforce a consistent naming convention throughout the project.

Question

Is there a free/open-source static analysis tool which can generate a list of:

  • global symbols
  • class names
  • member methods (public/protected/private, if possible)
  • member variables
  • static methods
  • local symbols (will probably ignore these)
  • any other symbols I may have missed, but may impact a reader of code

Approach

My intention is to use vim to edit the generated list of symbols and then use a Ruby-script to do a very rough search-and-replace/mapping on symbols so that at the very least, naming conventions are consistent.

The procedure is a little ugly and I expect the initial compile to fail, but I don't mind going through and fix problems by hand if I can have a more readable set of code.

What kinds of tools do developers of large C++ code bases use to do this kind of refactoring?

like image 505
kfmfe04 Avatar asked Nov 13 '22 03:11

kfmfe04


1 Answers

Automatic refactoring of C++ is extremely hard, in part to do with the preprocessor (macros and file inclusion), but mostly to do with the interdependency between parsing, name lookup and the rest of the semantic analysis phase (template instantiation, constant expressions, overload resolution, etc, etc). On the very large C++ codebases I have worked on, automatic refactoring is simply not done, and because of the inherent difficulty, the quality of refactoring tools is poor.

Since the emergence of clang though, which specifically has a modular front-end so you can access the AST in a nicer way than other tools, there may be some better refactoring tools based on it - but I wouldn't hold my breath.

Take a look at the AST dump from clang, perhaps you can write a script on the XML to give you a dump that might form a starting point for refactoring it by hand.

like image 93
Andrew Tomazos Avatar answered Nov 15 '22 04:11

Andrew Tomazos