1 @c GNU noreturn, stdnoreturn modules documentation
3 @c Copyright (C) 2019--2024 Free Software Foundation, Inc.
5 @c Permission is granted to copy, distribute and/or modify this document
6 @c under the terms of the GNU Free Documentation License, Version 1.3 or
7 @c any later version published by the Free Software Foundation; with no
8 @c Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
9 @c copy of the license is at <https://www.gnu.org/licenses/fdl-1.3.en.html>.
11 @node Non-returning Functions
12 @section Non-returning Functions
14 @cindex @code{_Noreturn}
15 @cindex @code{noreturn}
16 @cindex @code{stdnoreturn}
17 A "non-returning" function is a function which cannot return normally.
18 Instead of returning, it can loop forever, or it can transfer control via
19 @code{abort}, @code{execvp}, @code{exit}, @code{longjmp}, @code{throw}
20 (in C++), or similar mechanisms. Non-returning functions are
21 declared with a @code{void} return type.
23 It helps the compiler's ability to emit sensible warnings, following
24 data-flow analysis, to declare which functions are non-returning.
25 It can also help generate more-efficient code, as there is no need
26 to save a return address when calling a non-returning function.
28 Gnulib has multiple ways to support such a declaration:
32 The @code{_Noreturn} keyword. No modules are needed, as Gnulib
33 arranges for @code{<config.h>} to define @code{_Noreturn} to an
34 appropriate replacement on platforms lacking it.
35 Unfortunately, although this approach works for all current C versions,
36 the @code{_Noreturn} keyword is obsolescent in C23.
40 The @samp{noreturn} module. It provides a way to put this declaration
41 at function declarations, at function definitions, and in function
42 pointer types. The identifiers to use are:
45 @code{_GL_NORETURN_FUNC} for use in function declarations and function
48 @code{_GL_NORETURN_FUNCPTR} for use on function pointers.
51 The include file is @code{<noreturn.h>}.
54 Which of the approaches to use? If the non-returning functions you
55 have to declare are unlikely to be accessed through function pointers,
56 you should use @code{_Noreturn}; otherwise the module
57 @code{noreturn} provides for better data-flow analysis and thus for
61 There is also an obsolete @code{stdnoreturn} module, but its use is no