Jump to content
Site Stability Read more... ×
  • Advertisement
Sign in to follow this  
GBGames

Unused function detection

This topic is 4994 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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"?

Share this post


Link to post
Share on other sites
Advertisement
-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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!