Finding unused source code
I have an application with several tens of execution scenarios (like 'load a file', 'print', etc.). There is a lot of code that supports this functionality. Some pieces of the code are used for just one scenario, others are shared between many scenarios.
With time, some scenarios became obsolete and was removed. I suspect that as a result of that, there is now a lot of code that is not used by any of the active scenarios. Does anyone have an idea about how this code could be identified? I'd like to remove it to reduce the executable size and compilation time. There are several megabytes of code, so manual inspection is not an option.
One idea that I had was to create a list of all functions and classes in the code, then run a profiler and see which ones are listed in the profiler output. These ones are used, and the remaining can be removed. But it would be much easier if there was a tool that could do it.
I would also appreciate other suggestions.
The code is in C++ under linux.
[1055 byte] By [
Yunnat] at [2007-11-20 0:41:57]

# 2 Re: Finding unused source code
I am not sure if the profiler can provide code coverage for identifying compiled code that is not linked into your executable. Anyway, I think it's about time to refactor your code, including your makefile. You can change your makefile so that you can use a compile flag to "turn-on" the feature or scenario when necessary. In this way, code that is not needed will not be compiled at all.
Kheun at 2007-11-9 1:07:38 >

# 3 Re: Finding unused source code
I've an idea, but you'll probably have to use an existing C++ code parser for that or an other existing tool.
The idea is to put a stub such as:
static int x=fprintf(stderr, "%s\n", __func__);
At the top of each function definition.
If you use a particular indentation style, it might be easy to detect function definitions, and add that stub to all your functions (with awk sed or Perl).
In addition, you've to extract all file names to a file that you would name "all_functions"
Then, launch your program in a large number of scenario, redirecting standard error output to a file, or if stderr is used for other things, use the file descriptor #3 or simply, open a log file at program startup.
Then, compare the names in that file to the names in the all_functions file.
Then, try to manually remove these "unused" functions, and cancel that, if it does a linker error (meaning that it is used but never called in your test).
Tools such as sort, uniq, and diff should help you.
I don't know whether a profiler might do a part of this work (i.e. displaying which functions are entered). You should seek.