I read this question about noreturn attribute, which is used for functions that don't return to the caller.
Then I have made a program in C.
#include <stdio.h>
#include <stdnoreturn.h>
noreturn void func()
{
printf("noreturn func\n");
}
int main()
{
func();
}
And generated assembly of the code using this:
.LC0:
.string "func"
func:
pushq %rbp
movq %rsp, %rbp
movl $.LC0, %edi
call puts
nop
popq %rbp
ret // ==> Here function return value.
main:
pushq %rbp
movq %rsp, %rbp
movl $0, %eax
call func
Why does function func() return after providing noreturn attribute?
The function specifiers in C are a hint to the compiler, the degree of acceptance is implementation defined.
First of all,
_Noreturnfunction specifier (or,noreturn, using<stdnoreturn.h>) is a hint to the compiler about a theoretical promise made by the programmer that this function will never return. Based on this promise, compiler can make certain decisions, perform some optimizations for the code generation.IIRC, if a function specified with
noreturnfunction specifier eventually returns to its caller, eitherreturnstatementthe behaviour is undefined. You MUST NOT return from the function.
To make it clear, using
noreturnfunction specifier does not stop a function form returning to its caller. It is a promise made by the programmer to the compiler to allow it some more degree of freedom to generate optimized code.Now, in case, you made a promise earlier and later, choose to violate this, the result is UB. Compilers are encouraged, but not required, to produce warnings when a
_Noreturnfunction appears to be capable of returning to its caller.According to chapter §6.7.4,
C11, Paragraph 8and, the paragraph 12, (Note the comments!!)
For
C++, the behaviour is quite similar. Quoting from chapter §7.6.4,C++14, paragraph 2 (emphasis mine)