4

I modified a huge C++ project with lots of files and functions. The problem is, that now there are tons of useless files, includes, global variables and functions. Removing them by hand would be a pain. Is there a tool that analyzes the code like a compiler does and deletes all unused stuff? I would prefer a tool for unix. Also a way to remove only one or a few of the useless components named above would help.

timakro
  • 1,739
  • 1
  • 15
  • 31
  • 1
    I'd imagine your best bet is ctrl-f + replace – mcraenich Jun 07 '15 at 18:18
  • 2
    As of includes, there is an `iwyu` (include-what-you-use) tool from Google. What it does, it tries to automatically detect which includes fo you need, and generates diff's to add and remove includes. In my experience, it is not ideal and requires manual post-checking, but it can make the process faster. – Ilya Popov Jun 07 '15 at 18:22
  • 1
    The term for what you want is 'dead code' identification. This is handled by static code analysis tools. The Clang Static Analyzer tool is free and open source, but in beta. Dead code identification may not be a feature yet. Not very many robust static code analysis tools that are free. – KeithSmith Jun 07 '15 at 18:38
  • This looks like a recommendation question. – Ryan Bemrose Jun 07 '15 at 18:53
  • Four upvotes for an off-topic question! Nice. – Lightness Races in Orbit Jun 07 '15 at 19:39

1 Answers1

2

There several posibilities of the GNU toolchain itself to optimize codesize, if you don't mind that the linker does this every time you build your system. And there is always the question in C++ what really is "unused code" (since working with pointers and casts can mislead any tool).

So your best bet for this is the Gold linker (Replacing ld with gold - any experience?) and the following options:

The "bigger" approach would be static code analyers/code refactoring tools (How can I know which parts in the code are never used?) and then certain libraries like Boost do come with their own tools to reduce the number of files.

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
  • Some supplemental links: [Is unreachable code safe to remove? (-Wunreachable-code)](http://stackoverflow.com/questions/12811586/is-unreachable-code-safe-to-remove-wunreachable-code), [Dead code detection in legacy C/C++ project](http://stackoverflow.com/questions/229069/dead-code-detection-in-legacy-c-c-project) and [Finding “dead code” in a large C++ legacy application](http://stackoverflow.com/questions/2380153/finding-dead-code-in-a-large-c-legacy-application) – Florian Jun 07 '15 at 19:24