Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find C functions without a prototype?

Company policy dictates that every function in C source code has a prototype. I inherited a project with its own make system (so I cannot test it on gcc or Visual Studio) and found that one of the files has some static functions declared without prototypes. Is there a way (not necessarily with a compiler) to list all functions without prototypes in all .c files?

like image 955
Gnubie Avatar asked Apr 12 '13 14:04

Gnubie


2 Answers

gcc has an option to warn you about this:

gcc -Wmissing-prototypes

You can turn this warning into an error to stop compilation and force people to fix it:

gcc -Werror=missing-prototypes

If you just want to list it you can compile with the gcc option -Wmissing-prototypes and grep for no previous prototype for in the log.

Update based on edit:

Since you now mention that you can't use gcc, you'll have to find a similar option for your current compiler. Most compilers have such an option. Start with the man page or the built in help output.

like image 137
jman Avatar answered Nov 15 '22 03:11

jman


ctags can do that!

--c-kinds=p generates the list of all function prototypes

--c-kinds=f generates the list of all function definitions

Now you just need to compare those.

diff -u <(ctags -R -x --sort=yes --c-kinds=f | cut -d' ' -f1) <(ctags -R -x --sort=yes --c-kinds=p | cut -d' ' -f1) | sed -n 's/^-//p'

like image 34
aragaer Avatar answered Nov 15 '22 03:11

aragaer