Advertisement

Unused function detection

Started by February 15, 2006 11:33 AM
20 comments, last by erissian 18 years, 8 months ago
Do the GNU compiler tools include some type of profiler? If so, you could use that and check which functions are being called. If the call count is 0, you would know that function is unused.
[Edit--mentioned above,I'am too slow]
0xa0000000
Keep in mind that ld should automatically remove unused symbols from the executable anyways.
Advertisement
>Keep in mind that ld should automatically remove unused symbols from the
>executable anyways.
True - but the point was to detect old, unused code so you can
remove it (and don't accidentally call a depricated function).

I usually do what one of the previous posters suggested, comment
out blocks I think are unused, and then recompile

/R
visit my website at www.kalmiya.com
Another way could be to declare the function deprecated:

// program.h

void myFunction(int,int) __attribute__ ((deprecated));

// program.c

#include "program.h"

void myFunction(int a,int b)
{
. . .
}

int main(int argc,char* argv[])
{
myFunction(5,6);
return 0;
}

// Make output
gcc program.c -o program
program.c: In function 'int main()'
program.c:10: warning: 'myFunction' is deprecated (declared at program.c:3)

This way, it wouldn't break the program, but it would tell you when it happens. Honestly though, if I write new versions, I usually just delete the old ones, or map them to then new ones. Then again, that won't work in a larger collaboration.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
I actually found everything using grep FunctionName *, but I was hoping to find a dedicated tool. My project isn't that big now, but eventually it would get cumbersome to manually look for each function I think is old.

Also, before removing the code in question, I commented it out and recompiled. Again, this worked great on the relatively small project, but as the project gets bigger, I think recompiling each time would take longer than necessary. Then again, the point is to get rid of code that I no longer needed, and I might come back to the codebase months later and would have forgotten what it was that I was intending to replace. I can't figure out what to comment until I do the grep thing to find out candidates for removal.

I'll look into some of the suggestions here.
-------------------------GBGames' Blog: An Indie Game Developer's Somewhat Interesting ThoughtsStaff Reviewer for Game Tunnel
hi.

check cscope or (kscope in KDE), it is not normally used this kind of checking but there is command 'Find functions calling this function' . It is not very easy to use , but with emacs binding it is easier, or use kscope.
Advertisement
Thanks, AP. Still, that tool requires that I already know the function name, and using grep already helps there. I'm looking for a tool that would tell me that function name so I won't have to look for it.
-------------------------GBGames' Blog: An Indie Game Developer's Somewhat Interesting ThoughtsStaff Reviewer for Game Tunnel
Doesn't the 'strip' command do this for you??
Cleaning unsed functions from the file?
if (*pYou == ASSHOLE) { pYou->Die(); delete pYou; };
strip works on the object files, but it doesn't help me keep my source code clean of usless functions. Perhaps it is just a matter of being vigilant with the code?
-------------------------GBGames' Blog: An Indie Game Developer's Somewhat Interesting ThoughtsStaff Reviewer for Game Tunnel
Quote: Original post by GBGames
strip works on the object files, but it doesn't help me keep my source code clean of usless functions. Perhaps it is just a matter of being vigilant with the code?


It is probably the best solution. It is what I do and most other people I know do.

This topic is closed to new replies.

Advertisement