Search for "dead code" in a large legacy C ++ application

I am currently working on a large and old C ++ application, in which many developers are in front of me. There are many dead code in the project, classes and functions that are no longer used by anyone.

What tools are available for C ++ to analyze a large code base for detecting and reorganizing dead code? Note. I'm not talking about testing tools like gcov.

How did you find the dead code in your project?

+41
c ++ visual-c ++ legacy
Mar 04 '10 at 14:50
source share
8 answers

You will need a static analysis tool

The main problem that I ran into is that you have to be careful not to use any libraries from somewhere that you do not control / have. If you remove a function from the class that is used to reference the library in your project, you may break something that you did not know using the code.

+23
Mar 04 '10 at 14:54
source share

You can use Cppcheck for this purpose:

$ cppcheck --enable=unusedFunction . Checking 2380153.c... 1/2 files checked 0% done Checking main.c... 2/2 files checked 0% done [2380153.c:1]: (style) The function '2380153' is never used. 
+4
Jul 12 '16 at 13:20
source share

Caolán McNamara callcatcher is very effectively used in the LibreOffice project (~ 6 MLOC) to find dead code.

+3
May 29 '12 at 19:02
source share

One approach is to use the "Find All Links" context menu item in the names of classes and functions. If a class / function is mentioned only on its own, it is almost certainly dead code.

Another approach based on the same idea is to delete (comment out) the files / functions from the project and see what error messages you get.

+2
Mar 04 '10 at 14:56
source share

I think your best bet is likely to be a coverage tool. There are many for * nix and windows. If you have unit tests, it’s easy - if you have low test coverage, then the uncovered code is either dead or not yet verified (you want both pieces of this information anyway). If you do not have unit tests, create an application using the tools provided by one of these tools, run it through some (everything should be perfect) execution paths and view the report. You get the same information as with unit tests, this will require much more work.

Since you are using VisualStudio, I can provide you with a couple of links that you could use:

None of them are free, not even cheap, but the result is usually worth it.

On * nix-like platforms, gcov combined with tools like zcov or lcov is a really good choice.

+2
Mar 04
source share

Nothing beats code. Except, perhaps, hard cropping as one goes.

Sometimes what looks like a dead tree is used as linings for unit tests, etc., or it seems to be alive simply because it is used by outdated unit tests, but it is never used outside of tests. Some time ago, I removed more than 1000 LOCs that supported external CAD model translators, we had tests involving these external translators, but these translators were not supported for 8+ years, and there was no way for the application user, even if they wanted could ever refer to them.

If someone does not want to get rid of dead wood, you will find your team for many years.

+2
Mar 04 '10 at 15:35
source share

Although not specifically for dead code, I found Source Navigator

http://sourcenav.berlios.de/

quite useful, albeit cumbersome to configure, and a little buggy. That was a year ago on Linux (Fedora).

0
Mar 04 '10 at 14:54
source share

See our SD C ++ Test Coverage .

You need to do a lot of dynamic testing to implement the code to make sure you reach the maximum level of reach. The code “not applicable” may or may not be dead; perhaps you just don’t have a test case to implement it.

0
Mar 12 '10 at 0:15
source share



All Articles