While trying to rid my code of GCC warnings I have discovered a strange bug/behaviour. My program uses a custom exit function which performs some clean-up and finally calls exit()
.
However my use of this exit function is causing GCC to produce spurious warnings of:
control reaches end of non-void function with custom exit function
An example of a program causing this bug is:
File1.c:
extern void CleanExit(int ExitCode);
int MyFunc(int ErrorCode)
{
if(-1 == ErrorCode)
return -1;
if(1 == ErrorCode)
return 1;
if(2 == ErrorCode)
return 0;
if(3 == ErrorCode)
CleanExit(0);
CleanExit(-1);
}
File2.c:
void CleanExit(int ExitCode)
{
/* Do Some Cleanup */
exit(ExitCode);
}
Is there some way to be rid of these warnings without adding useless cruft to every function which uses this structure?
I would prefer to do so with standard C but am willing to use some kind of GCC pragma if needed.
You tagged your question with GCC. So instead of changing function from void
to int
and adding return
everywhere, you can try and GCC specify attribute. Add __attribute__((noreturn))
above CleanExit()
declaration. This will alert the compiler that CleanExit()
will never return directly to the caller.