When using a macro that might expand to a function with a noreturn
attribute, is there a way to stop it from suppressing -Wreturn-type
?
Simple example:
/* Defined in a separate header. */#ifndef NDEBUG# define MY_ASSERT_MACRO(test) ((void)((test) ? 0 : abort()))#else# define MY_ASSERT_MACRO(test) ((void)(0 ? 0 : (test)))#endif/* C source file. */int function(enum MyEnum foo){ switch (foo) { case A: return 1; case B: return 1; } MY_ASSERT_MACRO(0); /* <-- Missing return! This should always warn! */}
The problem with this is in release builds this gives a -Wreturn-type
warning, and in debug builds it gives no warning at all, since abort has a noreturn
attrubute.
I would like to get the warning in both cases so developers using debug builds don't break release builds.
Is there a (clean*) way to stop the abort function from disabling the -Wreturn-type
warning?
Not very clean ways:
- Call abort with a cast to remove the
noreturn
attribute:((void (*)(void))(*(((void **)abort))))()
- Compare two things which could theoretically be the same:
(((const void *)(abort) != (const void *)(stderr)) ? abort() : 0)