Tool to detect dead code in C++ application

S

sanjay

Hi,

Is there any tool that would do static detection (doesnt execute the
application) of dead code for C++ application.

Few tools do show which code flow would never get executed, but i am
looking for some tool which would detect any functions that are never
used at all? or any classes that are created but not instantiated.

This is required for an application which includes hundreds of C++
files and many classes.

Any help will be appreciated.

Thanks
Sanjay Raghani
 
S

sanjay

just to make the requirement more clear..

Following has been taken from coverity help which does have DEADCode
checker:
"The DEADCODE checker finds many instances of code that can never be
reached due to branches whose condition will always evaluate the same
way whenever the program is executed. This checker does not warn about
function-level dead code such as static functions which are never
called."

so here the requirement is to actually detect the code that is never
called. i think coverity/similar tools wont be of help here...

the reason i am looking for a tool is because the application is
pretty huge and includes hundreds of cpp files. Also the files have
been added since long.. so there will surely be lot of code that is
not used any more and could be removed..
 
J

joseph cook

just to make the requirement more clear..

Following has been taken from coverity help which does have DEADCode
checker:
"The DEADCODE checker finds many instances of code that can never be
reached due to branches whose condition will always evaluate the same
way whenever the program is executed. This checker does not warn about
function-level dead code such as static functions which are never
called."

so here the requirement is to actually detect the code that is never
called. i think coverity/similar tools wont be of help here...

the reason i am looking for a tool is because the application is
pretty huge and includes hundreds of cpp files. Also the files have
been added since long.. so there will surely be lot of code that is
not used any more and could be removed..


I'm sorry, what you ask is impossible. No static analysis could tell
you which code may or may not be called during execution of the
program. You might as well as for a static analysis tool that tells
you whether a program will complete or not.

Joe C
 
K

kwikius

sanjay said:
Hi,

Is there any tool that would do static detection (doesnt execute the
application) of dead code for C++ application.

Few tools do show which code flow would never get executed, but i am
looking for some tool which would detect any functions that are never
used at all? or any classes that are created but not instantiated.

This is required for an application which includes hundreds of C++
files and many classes.

Any help will be appreciated.

Best tool is the compiler. Just create a version of app and hide ( comment
out or remove source from makefile) functions definitions you think arent
used. If App links then function is unused.

You can also work the other way , gradually putting in the function
definitions that are used, or a combination of the two methods


regards
Andy Little
 
A

AnonMail2005

Hi,

Is there any tool that would do static detection (doesnt execute the
application) of dead code for C++ application.

Few tools do show which code flow would never get executed, but i am
looking for some tool which would detect any functions that are never
used at all? or any classes that are created but not instantiated.

This is required for an application which includes hundreds of C++
files and many classes.

Any help will be appreciated.

Thanks
Sanjay Raghani

First, I suggest using Doxygen to get an overall feel of library
and file dependencies. Also, I'm pretty sure Doxygen will also
generate call graphs which I think is what your asking for.

Second, I suggest make a regression test suite *before* you
modify any code. With that, you can heavily modify your
project and have confidence that it still produces the same
results given the same inputs.

HTH
 
J

James Kanze

I'm sorry, what you ask is impossible. No static analysis
could tell you which code may or may not be called during
execution of the program.

Yes and no. If the function is never referenced, it won't be
called. One simple solution for starters is to put each
function in a separate source file in a library, and see which
ones the linker picks up.

Of course, with virtual functions, it's somewhat harder. But
not always impossible.
You might as well as for a static analysis tool that tells you
whether a program will complete or not.

And... It's usually considered impossible in the general case,
but there are lots of special cases where it can be done.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top