Skip to contentSkip to conversation

"noreturn"

DISCUSSION Shriramana Sharma 3 participants 3 messages Jul 29, 2007 – Jul 30, 2007

lore
Share & tools
lore
#0 lore raw

man:gcc at -Wunitialized says:

Some spurious warnings can be avoided if you declare all the functions you use that never return as noreturn.

Google says this term is used only in Microsoft C++. So what is it doing in GCC's page?

Shriramana Sharma.

#1 lore raw

Shriramana,

Quoted 7 lines · Shriramana Sharma

man:gcc at -Wunitialized says:

Some spurious warnings can be avoided if you declare all the functions you use that never return as noreturn.

Google says this term is used only in Microsoft C++. So what is it doing in GCC's page?

What do you mean by that? There is no indication that __attribute__((noreturn)) is _only_ used within the Microsoft environment.

Some standard library functions, such as abort(3) and exit(3), cannot return. GCC is able to determine this fact automatically. When you define your own functions that never return you can declare them noreturn to tell the compiler about it. For example,

	void doesnotreturn(void) __attribute__ ((noreturn));
	
	void doesnotreturn (void) {
		/* do some work here */
		exit (1);
	}

Telling the compiler to assume that doesnotreturn() cannot return allows for gcc to optimize code without regard to what would happen if doesnotreturn actucally returns.

What exactly is not clear about the usage of noreturn? The gcc manual also makes clear that even semantically correct code can cause an unitialized warning since

"GNU CC is not smart enough to see all the reasons why the code might be correct despite appearing to have an error."

\Steve

--

Steve Graegert <[email protected]>

#2 lore raw
In reply to Shriramana Sharma
Quoted 6 lines · Shriramana Sharma

man:gcc at -Wunitialized says:

Some spurious warnings can be avoided if you declare all the functions you use that never return as noreturn.

Google says this term is used only in Microsoft C++.

Wrong.

Quoted 1 line

So what is it doing in GCC's page?

The "noreturn" attribute is the first one in the "Function Attributes" page of the gcc Info file:

`noreturn'
A few standard library functions, such as abort' and exit', cannot return. GCC knows this automatically. Some programs define their own functions that never return. You can declare them `noreturn' to tell the compiler this fact. For example,

void fatal () __attribute__ ((noreturn));

void
fatal (/* ... */)
{
/* ... */ /* Print error message. */ /* ... */ exit (1);
}

The noreturn' keyword tells the compiler to assume that fatal' cannot return. It can then optimize without regard to what would happen if `fatal' ever did return. This makes slightly better code. More importantly, it helps avoid spurious warnings of uninitialized variables.

Do not assume that registers saved by the calling function are restored before calling the `noreturn' function.

It does not make sense for a noreturn' function to have a return type other than void'.

The attribute `noreturn' is not implemented in GCC versions earlier than 2.5. An alternative way to declare that a function does not return, which works in the current version and in some older versions, is as follows:

typedef void voidfn ();

volatile voidfn fatal;

Signature · 1 line
Glynn Clements <[email protected]>