Tool to detect dead code in C++ application

  • Follow


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
0
Reply sanjay.raghani (8) 8/1/2008 11:33:44 AM

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

On Aug 1, 4:33 pm, sanjay <sanjay.ragh...@gmail.com> wrote:
> 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

0
Reply sanjay.raghani (8) 8/1/2008 11:45:02 AM


On Aug 1, 7:45=A0am, sanjay <sanjay.ragh...@gmail.com> wrote:
> 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
0
Reply joecook (150) 8/1/2008 12:24:49 PM

"sanjay" <sanjay.raghani@gmail.com> wrote in message 
news:c81eb7e5-6744-471b-b183-e96268d47653@56g2000hsm.googlegroups.com...
> 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





0
Reply andy199 (808) 8/1/2008 12:52:21 PM

In article
<2d3e769b-9fc7-43e3-a6ba-8a99d6988272@w7g2000hsa.googlegroups.com>, sanjay
<sanjay.raghani@gmail.com> wrote:
> ...
> 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..

Use a compiler/linker combination which can do "dead stripping". A decent
compiler will optimize out things like

    if ( 0 ) { /* ... */ }

and one that links only objects that are actually used will strip out
things like f() below.

    // foo.cpp
    static void f() { /* ... */ }
    // ... following code never calls f
    // ...

This sort of compiler/linker won't usually detect virtual functions never
called, though.

If you want code which could be called, but never is in practice, you'll
need a tool which keeps track of code that's used, then a data set which
exercises ALL functionality (including all possible errors).
0
Reply blargg.h4g (301) 8/1/2008 1:16:50 PM

On Aug 1, 7:33=A0am, sanjay <sanjay.ragh...@gmail.com> wrote:
> 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
0
Reply AnonMail2005 (245) 8/1/2008 1:36:33 PM

On Aug 1, 2:24 pm, joseph  cook <joec...@gmail.com> wrote:
> On Aug 1, 7:45 am, sanjay <sanjay.ragh...@gmail.com> wrote:
> > 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.

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.

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
0
Reply james.kanze (9620) 8/1/2008 3:58:47 PM

6 Replies
44 Views

(page loaded in 0.178 seconds)

Similiar Articles:













7/22/2012 4:02:41 PM


Reply: