Link to home
Start Free TrialLog in
Avatar of Ryan94114
Ryan94114

asked on

Simple Static Analysis of C++ Code to Eliminate Unused Functions

How can I eliminated functions which aren't called from my C++ Project?  I have a legacy code base where 70% of the source code is unused because people didn't bother to remove functions when they were no longer needed.

I've seen this question on the Internet, but people often reply with dynamic code coverage tools.

A dynamic tool won't help. During a particular program run, functions may not run which could be called under different circumstances.

A static tool could be very simplistic, merely collecting functions names from header files and .cpp files, and looking at the symbols within each function as possible function references. By creating and following a call tree, one can start from main and determine which functions could get called.  Any function not in that call tree could then be eliminated from the code base.

Does something like this exist?  Either free or including a free trial (I'd rather not spend the money until I see it work)?

Thanks for any help,

---Michael
Avatar of phoffric
phoffric

I see that you are a Microsoft user..

In Visual Studio, you can find all references on each function one at a time by right-clicking on the function (but slow process if many functions).

Link time optimization to remove unused functions and data:
http://msdn.microsoft.com/en-us/library/bxwfs976.aspx

I've read that this works well:
Reference Assistant helps to remove unused references from C#, F#, VB.NET or VC++/CLI projects in the Visual Studio 2010.
http://visualstudiogallery.msdn.microsoft.com/fc504cc6-5808-4da8-ae86-8d3f9ed81606
>> A dynamic tool won't help. During a particular program run, functions may not run which could be called under different circumstances.
If you did this, you now have a candidate list of potential unused functions. You could
1) search the project for each reference to determine if there are any references
2) macro out the functions and let the linker complain when a function is no longer defined
For your free trial, here is a great utility program I highly recommend (for much more that what you currently are asking):

http://www.scitools.com/

If you have a VS 2010 C++ project, it accepts it and builds the cross-reference quickly.

http://www.scitools.com/support/forum/discussion/158/feedback-highlighting-unused-entities/p1
Avatar of Ryan94114

ASKER

I very much appreciate the solutions which have been offered, but so far none of them provide a list of functions unreachable from main.

>Link time optimization to remove unused functions and data:
>http://msdn.microsoft.com/en-us/library/bxwfs976.aspx

I tried this and references were discarded only for libraries I was linking with.  None of the .obj's from my source files had any functions discarded.

>Reference Assistant helps to remove unused references from C#, F#, VB.NET or >VC++/CLI projects in the Visual Studio 2010.
>http://visualstudiogallery.msdn.microsoft.com/fc504cc6-5808-4da8-ae86-8d3f9ed81606

The web site describes and shows pictures of a tool which removes unneeded references to entire assemblies, but not individual functions.

>1) search the project for each reference to determine if there are any references

Many obsolete functions call other obsolete functions.  

> 2) macro out the functions and let the linker complain when a function is no longer defined

If I macro out all of the functions then no functions will call any others and the linker will issue no warnings.  I see no obvious method of macroing out the "right" functions without already knowing which functions are unreachable.

>Try http://www.scitools.com/

This tool, called Understand, offers function call graphs, which come close to solving my problem.  Starting from main, I could discover all of the functions which are called, all of the functions those functions call, and so forth.  

However, I need a list of functions which are unreachable from main, and I need it as text (not graphics).  I have asked Understand technical support whether or not they do this, but so far it looks like they do not.


Which brings me back to my original question.  Is there a free or trial program which identifies functions unreachable from main?
In 2006 I couldn't add three printf's without running out of memory in a microprocessor. Using Understand for C, I was able to identify the dead functions. Here are some quotes from Understand specs. You can also get into a chat session and confirm that Understand will do the job for you.

My other comments about using a dynamic profiler to provide a candidate list of dead functions would be more practical if, say 10% of your code base was dead. I just reread your OP and see that you believe 70% is inactive. Quite astounding if true. If you get the Understand trial, be sure to set aside time to learn all of the navigation features. You may not want to let the trial drop if you do. As other workers walked by seeing what I was able to do with Understand, we had to purchase 5 floating licenses for one project.
Root Calls: Lists only entities that call others, but are not called themselves. These are either high-level code (mains), code called by hardware (interrupt handlers), or dead (unused) code.
page 123 -    http://www.scitools.com/documents/manuals/pdf/understand.pdf

Program Unit Cross-Reference Report - The Program Unit Cross-Reference Report lists all program units (such as procedures and functions) analyzed in alphabetic order along with information about what they return (if anything), what parameters are used, and where they are used by other program units. The HTML version offers hyperlinks to the Data Dictionary report entry and to the source code where each reference occurs.
    https://www.scitools.com/features/crossReference.php
As you remove a set of obsolete functions, you then repeat the process so that a new set of obsolete functions show up, and repeat until none are left.
Here is what Understand Technical Support sent me:
Hi Paul,

There is a script included with Understand in SciTools\scripts\perl called acjf_dead_code.pl that will list the dead functions. More details on the Perl API and how to run scripts are available here: http://www.scitools.com/plugins/index.php

Let us know if we can help with anything else.

Regards,

 Kevin Groke
ASKER CERTIFIED SOLUTION
Avatar of Ryan94114
Ryan94114

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> If there is interest, I will release the source code for free.

That would be a nice gesture.


In the OP:
>> "Either free or including a free trial (I'd rather not spend the money until I see it work)?"

I not only provided links for a free trial, but even got written confirmation from the company that the product will do the job. (By the way, from personal experience, the learning curve is truly miniscule. And they will even help you if you need it - they want happy customers and hope you will buy it.)

So you should really split the points as the provided links will also serve others as they search the PAQ.

 Paul
>> If there is interest, I will release the source code for free.  Right now it doesn't handle full c++ syntax including classes, but works well enough for the pile of code I have to deal with.
    That is very good work you did, and thanks for your offer to release the code. This will contribute to the PAQ by making the code post a good accepted answer. I don't think the current accepted answer #a39570010 of your outline will help someone trying to do something similar.
Showed detailed knowledge of problem and solution.