views:

1442

answers:

6

Is there a way to identify unused attributes/methods in Visual C++ 2008 Professional? If it's not possible by default, recommendations of 3rd-party tools are also much appreciated.

Thanks,
Florian

Edit: nDepend only works for .NET assemblies. I'm looking for something that can be used with native C++ applications.

A: 

nDepend will do it, along with cleaning your house and taking the dog for a walk. There's a nagware version available for free.

The following code query language statement will get you a list of unused methods

 WARN IF Count > 0 IN SELECT TOP 10 METHODS WHERE MethodCa == 0 AND 
   !IsPublic AND !IsEntryPoint AND !IsExplicitInterfaceImpl AND 
   !IsClassConstructor AND !IsFinalizer
Thanks, but nDepend seems to work with .NET assemblies only.
fhe
A: 

I have not personally used their productivity tools (I use their windows control suit), but it looks like DevExpress has a C++ refactor'er called Refactor! for C++. I didn't immediately spot the features that you are looking for, but maybe they have it?

FryHard
+6  A: 

Try PC-Lint. It's pretty good at finding redundant code. I haven't tried version 9 yet. Version 8 does take some time to configure. Try the online interactive demo.

finnw
This is exactly what I've been looking for. There is a VisualLint addin for Visual Studio which integrates PC-Lint. A less expensive solution would have been fine, but it seems that there is nothing out there.
fhe
A: 

The tricky bit is that many functions in C++ have to exist, even if they are not called. Boost especially will cause this, but even the regular STL code can do this. And your code has to play along. You might define a copy ctor because std::vector formally requires it. But if you don't instantiate any std::vector member that actually does copy a T, your copy ctor will remain unused.

Even if they don't have to, they often exist for safety. For example, declaring a private copy constructor can prevent an object from unintended copying. Without the private declaration, the compiler would define a public, memberwise copy ctor for you. Now, is this "unused" and do you want to be warned about them?

MSalters
A: 

Coverage Validator can show unused C++ code (but not attributes). It does it dynamically so you have to 'exersize' the app to get the results: http://successfulsoftware.net/2008/03/10/coverage-validator/

Andy Brice
A: 

PC-Lint is very powerful, but hard to lean. Of course that pretty well describes C and C++ doesn't it?

Another tool I think is excellent is Whole Tomato's Visual Assist X which integrates right into the IDE.

There are some big gotchas in C++ when searching for unreferenced code: templates, callbacks, and message handlers may be critical to your project but are never directly called. For example the handler for a thread is not called directly, but is a parameter when you create a new thread. The "On_buttonpress" type messages in MFC or WTL projects will also show up as un-called methods.

Once you find them you can configure PC-Lint to ignore these, but the first time through its a lot of work.

Doug Boone
Could you elaborate how Visual Assist X helps in finding unused code?
Ashwin