9

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?

jman
  • 11,334
  • 5
  • 39
  • 61
Gnubie
  • 2,587
  • 4
  • 25
  • 38
  • 2
    static functions don't really need prototypes, unless they are called before they are defined – Paul R Apr 12 '13 at 14:35
  • @Paul: Yes, but I suppose it allows the project manager to quickly see what functions are available etc. – Gnubie Apr 12 '13 at 14:39
  • 1
    If you can't use another compiler, for reasons unknown, you'll have to get a separate static analyser tool. All such tools in the market will find all missing prototypes for you – Lundin Apr 12 '13 at 14:46
  • 1
    `its own make system` - what do you mean? – fableal Apr 12 '13 at 14:46
  • @fableal: They have their own compiler which is not gcc, that is run under make. – Gnubie Apr 12 '13 at 14:53
  • 1
    Then I guess the solution from @Lundin applies; unless that compiler has an option to detect missing prototypes like gcc has – fableal Apr 12 '13 at 14:57
  • @PaulR: A prototype can be either standalone: `void foo(void);` or part of a definition: `void foo(void) { /* ... */ }`. – Keith Thompson Apr 29 '13 at 19:56
  • @Gnubie: This is more of a discussion for the Programming SX, but I think it is a mistake for a policy like that to apply retroactively to an old code base. Modifying old (working) code just to meet a new standard is likely to introduce bugs. You should apply the policy to new code, and update code that you directly touch, but leave the rest of the code base alone. – Ben Jackson Apr 29 '13 at 19:58

2 Answers2

12

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.

jman
  • 11,334
  • 5
  • 39
  • 61
  • Sorry if I didn't make it clear, but it has its own build system, so I can't test it on gcc. – Gnubie Apr 12 '13 at 14:45
  • 5
    "build system" != "compiler". I would presume that whatever compiler this custom build system uses (gcc/icc/clang/.......) has various options/switches to enable warnings/errors/etc. That would be the place to start, even if it's not gcc... – twalberg Apr 12 '13 at 14:48
5

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'

aragaer
  • 17,238
  • 6
  • 47
  • 49