Finding “dead code” in a large C++ legacy application [closed]

和自甴很熟 提交于 2019-11-26 11:59:19

问题


I\'m currently working on a large and old C++ application that has had many developers before me. There is a lot of \"dead code\" in the project, classes and functions that aren\'t used by anyone anymore.

What tools are available for C++ to make a analysis of large code base to detect and refactor dead code? Note: I\'m not talking about test coverage tool like gcov.

How do you find dead code in your project?


回答1:


You'll want to use a static analysis tool

  • StackOverflow: What open source C++ static analysis tools are available?
  • StackOverflow: C++ static code analysis tool on Windows
  • Wikipedia: List of tools for static code analysis

The main gotcha I've run into is that you have to be careful that any libraries aren't used from somewhere that you don't control/have. If you delete a function from a class that gets used by referencing a library in your project you can break something that you didn't know used the code.




回答2:


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.



回答3:


I think your best bet would probably be a coverage tool. There're plenty for both *nix and windows. If you have unit tests, it's easy - if you have a low test coverage, then the uncovered code is either dead or not tested yet (you want both pieces of this info anyway). If you don't have unit tests, build your app with instrumentation provided by one of those tools, run it through some (should be all ideally) execution paths, and see the report. You get the same info as with unit tests, it will only require a lot more work.

Since you're using VisualStudio, I could provide you couple of links which you could consider using:

  • Coverage meter
  • BullseyeCoverage

Neither of them is free, not even cheap, but the outcome is usually worth it.

On *nix-like platforms gcov coupled with tools like zcov or lcov is a really great choice.




回答4:


Nothing beats familiarity with the code. Except perhaps rigourous pruning as one goes along.

Sometimes what looks like deadwood is used as scaffolding for unit tests etc, or it appears to be alive simply because legacy unit tests exercise it, but it is never exercised outside of the tests. A short while ago I removed over 1000 LOC which was supporting external CAD model translators, we had tests invoking those external translators but those translators had been unsupported for 8+ years and there was no way that a user of the application even if they wanted to could ever invoke them.

Unless one is rigourous in getting rid of the dead wood, one will find your team maintaining the stuff for years.




回答5:


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




回答6:


One approach is to use "Find All References" context menu item on class and function names. If a class/function is only referenced in itself, it is almost certainly dead code.

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




回答7:


See our SD C++ Test Coverage.

You need to do a lot of dynamic testing to exercise the code, to make sure you hit the maximum amount of coverage. Code "not covered" may or may not be dead; perhaps you simply didn't have a test case to exercise it.




回答8:


Although not specifically for dead code, I found the Source Navigator

http://sourcenav.berlios.de/

quite useful, although cumbersome to set up and a bit buggy. That was a year ago on Linux (Fedora).



来源:https://stackoverflow.com/questions/2380153/finding-dead-code-in-a-large-c-legacy-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!