How to find C functions without a prototype?

南笙酒味 提交于 2019-12-23 07:36:49

问题


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?


回答1:


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.




回答2:


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'



来源:https://stackoverflow.com/questions/15974112/how-to-find-c-functions-without-a-prototype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!