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
Ryan94114Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffricCommented:
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
0
phoffricCommented:
>> 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
0
phoffricCommented:
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
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Ryan94114Author Commented:
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?
0
phoffricCommented:
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.
0
phoffricCommented:
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
0
Ryan94114Author Commented:
I finally ended up writing my own tool to do this.

First it strips constants, then it processes # directives, then it scans the c source code and header files and grabs the high-level definitions and treats the insides of functions as references to symbols and the insides of structs as symbols.  Finally, it creates a graph of all functions and data structures reachable from main, and prints excluded symbols.

It took me about 12 hours to write and debug this.  I think it would have taken more than that period of time to learn the functionality of the more advanced tools (and also to earn the money to pay for them).

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phoffricCommented:
>> 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
0
phoffricCommented:
>> 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.
0
Ryan94114Author Commented:
Showed detailed knowledge of problem and solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.