C/C++ and Fortran

 View Only

The XL C/C++ V12.1 Compiler supports the _Noreturn function specifier

By Archive User posted Sun May 26, 2013 11:17 PM

  

Originally posted by: RoyHu


Benefit:
The _Noreturn keyword enables optimizers to produce faster code by ignoring what would happen if the function returns.

 

Usage:
A function declared with the _Noreturn specifier indicates that this function will not return the control to its caller.

You can use the _Noreturn specifier to declare or define any functions other than the main function. It is undefined behavior if such function actually returns.

However, any functions that are declared with _Noreturn must call one of the following functions. Otherwise, the functions will return the control to their respective caller.
• abort
• exit
• _Exit
• longjmp
• quick_exit
• thrd_exit

 

Example:
_Noreturn void good ()
{
  abort(); // okay
}

_Noreturn void bad (int i)
{
  if (i > 0) abort(); // causes undefined behavior if i <= 0
}

 

How to enable this feature:
You can enable this feature with the -qlanglvl=extc1x option.

 

Related information:
• The _Noreturn function specifier:
http://pic.dhe.ibm.com/infocenter/comphelp/v121v141/topic/com.ibm.xlc121.aix.doc/language_ref/noreturn.html

• Keywords:
http://pic.dhe.ibm.com/infocenter/comphelp/v121v141/topic/com.ibm.xlcpp121.aix.doc/language_ref/keyw.html

• Extensions for C1X compatibility
http://pic.dhe.ibm.com/infocenter/comphelp/v121v141/topic/com.ibm.xlc121.aix.doc/language_ref/c1x_ext.html

3 comments
0 views

Permalink

Comments

Thu May 30, 2013 07:10 AM

Originally posted by: RajanB


You can add the _Noreturn function specifier to *any* function. The compiler can perform optimizations assuming that function will never return even if it does not call one of the functions given in the list. The list is likely just for the common use cases, though like the example you gave, there could be other functions that do not return that do not call any of those six functions. By the way, If you want to know more about C11 and what the compiler supports, you can see the developerWorks article at http://www.ibm.com/developerworks/rational/library/support-iso-c11/

Thu May 30, 2013 02:18 AM

Originally posted by: RoyHu


Peeter, Thank you for posting this comment. I will discuss this with the developer of this feature, and get back to you soon.

Wed May 29, 2013 06:33 PM

Originally posted by: PeeterJoot


are these 6 functions truely the only functions that are allowed? Something like: void sleepForever() { for ( ; ; ) sleep( 10000 ) ; } also never returns. Are other functions that are also marked _NoReturn also allowed?