I have legacy C++ code that I'm supposed to remove unused code from. The problem is that the code base is large.
How can I find out which code is never called/never used?
I have legacy C++ code that I'm supposed to remove unused code from. The problem is that the code base is large.
How can I find out which code is never called/never used?
One way is use a debugger and the compiler feature of eliminating unused machine code during compilation.
Once some machine code is eliminated the debugger won't let you put a breakpojnt on corresponding line of source code. So you put breakpoints everywhere and start the program and inspect the breakpoints - those which are in "no code loaded for this source" state correspond to eliminated code - either that code is never called or it has been inlined and you have to perform some minimum analysis to find which of those two happened.
At least that's how it works in Visual Studio and I guess other toolsets also can do that.
That's lots of work, but I guess faster than manually analyzing all code.
Well if you using g++ you can use this flag
-Wunused
According documentation:
http://docs.freebsd.org/info/gcc/gcc.info.Warning_Options.html
Edit: Here is other useful flag
-Wunreachable-code
According documentation:Update: I found similar topic Dead code detection in legacy C/C++ project
If you are on Linux, you may want to look into
callgrind
, a C/C++ program analysis tool that is part of thevalgrind
suite, which also contains tools that check for memory leaks and other memory errors (which you should be using as well). It analyzes a running instance of your program, and produces data about its call graph, and about the performance costs of nodes on the call graph. It is usually used for performance analysis, but it also produces a call graph for your applications, so you can see what functions are called, as well as their callers.This is obviously complementary to the static methods mentioned elsewhere on the page, and it will only be helpful for eliminating wholly unused classes, methods, and functions - it well not help find dead code inside methods which are actually called.
I had a friend ask me this very question today, and I looked around at some promising Clang developments, e.g. ASTMatchers and the Static Analyzer that might have sufficient visibility in the goings-on during compiling to determine the dead code sections, but then I found this:
https://blog.flameeyes.eu/2008/01/today-how-to-identify-unused-exported-functions-and-variables
It's pretty much a complete description of how to use a few GCC flags that are seemingly designed for the purpose of identifying unreferenced symbols!
Well if you using g++ you can use this flag -Wunused
According documentation:
http://docs.freebsd.org/info/gcc/gcc.info.Warning_Options.html
Edit: Here is other usefull flag -Wunreachable-code According documentation:
You could try using PC-lint/FlexeLint from Gimple Software. It claims to
I've used it for static analysis and found it very good but I have to admit that I have not used it to specifically find dead code.