This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
False ânoreturn â function does return warnings
- From: Ralf Baechle <ralf at linux-mips dot org>
- To: gcc at gcc dot gnu dot org
- Date: Tue, 6 Feb 2007 14:34:52 +0000
- Subject: False ânoreturn â function does return warnings
In an OS kernel functions that do not return are commonly used and
practically always their code is beyond gcc's ability to recognize
noreturn functions. A typical example would for example be the BUG()
function in Linux which is implemented as something like:
static inline void __attribute__((noreturn)) BUG(void)
{
__asm__ __volatile__("trap");
}
So the code doesn't return but gcc isn't able to figure that out and the
caring programmer trying to help gcc to do a better job by adding the
noreturn is punished with
warning: ânoreturnâ function does return
There are other such situations in plain C. A common solution is to add
something like like while(1) - but that does generate code. Quite a bit
for frequently called or very small functions. This frequently makes the
use of noreturn functions unattractive. So two suggested solutions:
1) Burry the warning for good. The programmer has explicitly said
noreturn so if the function does return it's his problem.
2) Introduce a __builtin_unreached() builtin function which tells gcc that
it not being reached, ever and does not generate any code. This could
be used like:
static inline void __attribute__((noreturn)) BUG(void)
{
__asm__ __volatile__("trap");
__builtin_unreached();
}
It would even conveniently be usable in macros or conditionals.
Ralf