Advertisement

Unused function detection

Started by February 15, 2006 11:33 AM
20 comments, last by erissian 18 years, 8 months ago
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 ThoughtsStaff 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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
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 ThoughtsStaff 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 ThoughtsStaff Reviewer for Game Tunnel
Advertisement
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
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
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.

This topic is closed to new replies.

Advertisement