False ‘noreturn’ function does return warnings

Ralf Baechle ralf@linux-mips.org
Tue Feb 6 14:42:00 GMT 2007

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");

It would even conveniently be usable in macros or conditionals.


More information about the Gcc mailing list