views:

835

answers:

7

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?

+1  A: 

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

grigy
This will only show unused variables. If I read correctly Vulcan writes about eradicating whole unused classes and types.
Marcin Gil
+4  A: 

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

David Sykes
A: 

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.

Rune Braathen
"..., unfortunately all of them are rather expensive."PC-Lint costs ~$250. We use 7.5 here and that came on a 3.5" floppy disk, and has copyright 1998 on it. So equated over the 10 years we've had it, the cost is about $27 per year, which is peanuts really.
graham.reeds
+4  A: 

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.

Anna-Jayne Metcalfe
A: 

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.

graham.reeds
A: 

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.

Anna-Jayne Metcalfe