#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wcast-qual"
#pragma GCC diagnostic error "-Wdiscarded-qualifiers"
#pragma GCC diagnostic error "-Wreturn-local-addr"
void *stack_depth_check( void )
{
volatile uint32_t dummy; // Put a variable on the stack
return( (void *) &dummy ); // Return its address - therefore the (approx.) present SP value
}
#pragma GCC diagnostic pop
In the project I'm currently working on this is what my GCC setup, with all warnings and all diagnostics on as they always shall be for any project, produces:
WatchDog/stackchk.c: In function 'stack_depth_check':
WatchDog/stackchk.c:19:11: error: cast discards 'volatile' qualifier from pointer target type [-Werror=cast-qual]
return( (void *) &dummy ); // Return its address - therefore the (approx.) present SP value
^
WatchDog/stackchk.c:19:9: error: function returns address of local variable [-Werror=return-local-addr]
return( (void *) &dummy ); // Return its address - therefore the (approx.) present SP value
As these are actual errors and not diagnostic messages not even GCC's
mechanism will let this code compile.
https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
It could still be done by selectively compiling with its own error options in the Make build system. However if someone is working on (safety related?) code doesn't understand C there is even less of a chance an error in the build system is going to be uncovered. Such exceptions are always best to avoid.
Software Safety: Do you just ignore Compiler Warnings?
I agree code reuse can be a problem. There we can get into obscure things like pointers being different sizes than integers or one I struggle with using tables of function pointers, where sizes have changed from when the code was first created.
/*
* You may not even convert a void * to a function
* pointer by explicit casting (6.3.2.3|1).
*
* C's abstract machine does not assume that code and
* data are addressed the same way, so as far as C is
* concerned function pointers and data pointers have
* nothing to do with each other (they could have
* different widths).
*/