Dead code identification (C++)

与世无争的帅哥 提交于 2019-11-27 11:45:51

You'll want something along the lines of QA-C++ (http://www.programmingresearch.com/QACPP_MAIN.html), also see http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis for similar products.

You're looking for a static code analysis tool that detects unreachable code; many coding guidelines (such as MISRA-C++, if I'm not mistaken) require that no unreachable code exists. An analysis tool geared specifically to enforce such a guideline would be your best bet.

And you'll like be able to find other uses for the tool as well.

I know that Gimpel's Lint products (PC-Lint and Flexelint) will identify unreachable code and unused / unreferenced modules.

They both fall in the category of static analysis tools.

I have no affiliation w/ Gimpel, just a satisfied long-term customer.

I dont know Visual C, and had also recommended the -Wunreachable-code specific coverage tools. As solution for your situation I would try the following:

  1. Make with ctags (or similar programm) a list of all your symbols in your source
  2. Enable in your compiler the dead code elimination (I would assume it defaults to on)
  3. Enable your whole-program/link time optimizations (so he knows that not used functions in your moduls are not required by other externals and get discarded)
  4. Take the symbols from your binary and compare them with the symbols from 1.

Another approach could be some call graph generating tool (e.g. doxygen).

I suggest you use a couple approaches: 1. GCC has some useful compilation flags:

-Wunused-function
-Wunused-label
-Wunused-value
-Wunused-variable
-Wunused-parameter
-Wunused-but-set-parameter

2. Cppcheck has some useful features like:

 --enable=unusedFunction

3. Use static analyzer as was suggest before.

One approach that works for me - with Delphi - is to enable debugging, and run your program under the debugger.

When a Delphi program is run under the debugger, the IDE shows in the margin which lines of code can be set as breakpoints. Code which is truly dead - i.e., has been stripped out by the linker/compiler is obvious as breakpoints can't be set there.

Some additional notes, as commenters seem to misunderstand this:

a: You don't need to try setting a breakpoint on each line. Just open up the source file in the IDE, and quickly scroll through it. Dead code is easily spotted.

b: This is NOT a 'code coverage' check. You don't need to run the application to see if it reaches the lines.

c: I'm not familiar enough VS2008 so can't say if this suggestion will work.

Either
1) MSVC's under-used in built static analysis tool.
2) The MSVC marketplace has lots of tools including support for most free tools, including CppCheck

You will need the latest version of Visual Studio for market place applications, but the free "Community Edition" has very lenient licencing.

Write a script that randomly deletes a function (from the source code) and recompiles everything from scratch. If it still compiles - that function was dead code.

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