Limpiar el código / Funciones no usadas

Discusión original: http://www.gamedev.net/community/forums/topic.asp?topic_id=375841



 Unused function detection
Post New Topic  Post Reply 
I started a project thinking I would need certain functions. At one point I thought of an alternative way to implement the functionality needed. I left the original functions in and wrote the new ones.

Today I found that nothing uses the old functions and that it was safe to remove them. While I know that the gcc/g++ compiler will detect unused variables and warn me about them, it didn't seem to notice that the functions went unused. Of course, it shouldn't actually warn me since it would be annoying to be warned about not using each and every function available. Still, I would like a tool that would let me check for such things once in awhile.

Is there a tool that would basically analyze my code and say, "Hey, this function is declared and defined, but it isn't actually used anywhere"?



-------------------------
GBGames' Blog: An Indie Game Developer's Somewhat Interesting Thoughts
Staff Reviewer for Game Tunnel



-Wunused-function, which is also enabled when you do -Wall.

Your programs should probably always be compiled with -Wall, possibly -Wextra (or -W depending on GCC version) and maybe even -pedantic.




Hey cool someone else from Chicago :) anyhow I don't know of any tools that will do that for you but the way I've always done it was comment out what you think might not be used with /* */ compile it then check for errors during compile time and also inside the program itself. I will check around to see if there are any such tools.

Edit: hmm never knew about that compile flag for gcc :) might have to check it out sometime.




I have the following line in my Makefile:

CFLAGS := -Wall -Wno-unknown-pragmas -Wno-format -g -DDEBUG $(shell sdl-config --cflags)

I don't believe I ever saw any warning, and I can't find any instance of the function being called anywhere. I had a function called movePlayerX(), and nothing called it as far as I could see.


And I just checked the man page and found out why:

Quote:

Warn whenever a static function is declared but not defined or a non-inline static function is unused.


My functions aren't static.

-------------------------
GBGames' Blog: An Indie Game Developer's Somewhat Interesting Thoughts
Staff Reviewer for Game Tunnel



The make utility does not recompile an unchanged file if it already has a .o file for it. Maybe that is the reason.
You can modify the time stamp on the files with the command
$ touch *.h *.c

Just a thought




Quote:
Original post by pulpfist
The make utility does not recompile an unchanged file if it already has a .o file for it. Maybe that is the reason.
You can modify the time stamp on the files with the command
$ touch *.h *.c

Just a thought


I've done make clean and make veryclean on this project a number of times since the update I mentioned, so there was plenty of opportunity for it to warn me about unused functions. I believe it is just because the option only works for static functions. I'll need to find a tool that is a bit more general purpose.

-------------------------
GBGames' Blog: An Indie Game Developer's Somewhat Interesting Thoughts
Staff Reviewer for Game Tunnel




Was searching google and found this not sure if it will help but I figured I would post it anyhow.

http://unixhelp.ed.ac.uk/CGI/man-cgi?gprof

Hope this will maybe help

man gprof

Only other thing I can think of other then commenting out like I had said before is to maybe create some sort of logging system that logs each function called then run your program and if something wasn't called it won't be in the list that your log system created....this could also be handy for debugging as well.




You can always add profiler support with -pg. Whatever has blanks in the flat profile hasn't been called.

Or even better:

grep myFunction *.c




It's the linker that would know whether a function isn't included in the binary or not. It would be nice to get a list of functions that 'didn't make the cut', and I bet some source code analysis tools do just that. Don't know of any specifics, though.




Quote:
Original post by erissian
Or even better:

grep myFunction *.c


This was another thing I was going to mention to you. Using grep might be handy I know I used it a lot when searching for functions in projects that I worked on.




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]




Keep in mind that ld should automatically remove unused symbols from the executable anyways.




>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




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.




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





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 Thoughts
Staff Reviewer for Game Tunnel




Doesn't the 'strip' command do this for you??
Cleaning unsed functions from the file?




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




Strip does not strip any functions out of your binary.

Strip takes out debugging symbols and stuff - which aren't required to run the program anyway.

I don't know whether, or under what conditions ld does unused function elimination - but not always.

Maybe it depends whether they're compiled with pic or not - but in any case, ld won't necessarily do so.

But it won't link in any objects which aren't required.

Mark




If you want to do it automatically, you could scan your header files for function declarations, and compile these into a temp file. Then you go through the file line by line grepping for instances of that function, and compile a report of the number and location of any instances. Last, you would warn about functions that only show up once (or less) ie only the implementation, or lack thereof.

At least, it's a start!

[Edited by - erissian on February 16, 2006 7:20:28 PM]


    Editado por Daniel el 10/06/2010 a las 12:20:56h.