Question

I'm using VS2008 for a C++ project. The code is quite old and has passed through many hands. There are several classes hierarchies, functions, enums and so on which are no longer being used.

Is there a way to get the compiler/linker to list out identifiers which have been declared or defined but are not being referred to anywhere?

Was it helpful?

Solution

PC-Lint "whole project" analysis (which analyses multiple files together) can do this. Please feel free to contact me if you need help setting it up.

OTHER TIPS

VS will warn about identifiers declared within a function and not used, you may need to move to warning level 4, but for global variables, and a hunt for many other potential problems, you would do well to try lint or visual lint

Not sure about Visual C++, but the g++ warns about unused variables. You may try to pass your code through the g++.

PC-Lint will do this. However do not run it with everything one. I did and had 1.2million errors - which equated to ~5 per line. This was pushed up greatly because nearly every UI class referenced resource.h and resource.h lists a lot of #defines to which lint says "replace defines with const int". For a good start point read this post on the Power of 2 blog.

For the most obvious unused declarations, a high warnlevel would take care of the matter.

Regarding the identification of unused pieces of code, the deadstripper (not being the center of a late night crime drama, but the mechanism which culls unused code during linking) will only get you part of the way - what you really want is a code coverage analysis tool. Plenty exist, unfortunately all of them are rather expensive.

A robust editor with good context tagging and call-graphing mechanism will also help.

With reference to Graham's post: FWIW I've never actually seen the "replace defines with const int" message with resource.h files - although some others (previously defined at same location) can be a pain.

Regardless, if a particular PC-Lint message is getting in the way of spotting what you are looking for it is a simple matter to turn it off using -e in your options.lnt file. It's also worth being aware of the -w option, which allows you to globally set the warning level (e.g. -w0 +e766 turns off everything except message 766).

For unused symbols and declarations, you'll want to look out for messages such as 755 (Information -- global macro not referenced), 758 (Information -- global enum not referenced), 769 (Information -- global enumeration constant not referenced) and 1714 (Information -- Member function not referenced). Remember that these are suppressed in single file analysis, so you'll need to run whole project analysis to see them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top