1 /* Macros for declaring functions as non-returning.
3 Copyright (C) 2017-2020 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert and Bruno Haible. */
23 /* A "non-returning" function is a function which cannot return normally.
24 It can transfer control only through longjmp(), throw (in C++), or similar
27 This file defines two macros _GL_NORETURN_FUNC and _GL_NORETURN_FUNCPTR,
28 that declare a function to be non-returning.
29 _GL_NORETURN_FUNC is for use in function declarations and function
31 _GL_NORETURN_FUNCPTR is for use on function pointers.
33 Comparison of this file with <stdnoreturn.h>:
34 <stdnoreturn.h> defines a macro (or keyword) _Noreturn that declares
35 a function to be non-returning. _Noreturn is only for use in function
36 declarations and function definitions.
37 Therefore, if the non-returning functions you have to declare are unlikely
38 to be accessed through function pointers, and if the efficiency with C++
39 compilers other than g++, clang, MSVC++ is not an issue to you, you can use
40 module 'stdnoreturn' instead of this one, and _Noreturn instead of
44 /* Declares that a function is nonreturning.
46 _GL_NORETURN_FUNC extern void func (void);
47 extern _GL_NORETURN_FUNC void func (void);
48 extern void _GL_NORETURN_FUNC func (void);
49 Use in C++ only code for a function with C linkage:
50 extern "C" { _GL_NORETURN_FUNC void func (void); }
51 Use in C++ only code for a function with C++ linkage:
52 _GL_NORETURN_FUNC extern void func (void);
53 Use in C & C++ code for a function with C linkage:
57 _GL_NORETURN_FUNC void func (void);
61 Use in C & C++ code for a function with current language linkage:
62 _GL_NORETURN_FUNC extern void func (void);
64 #if (3 <= __GNUC__ || (__GNUC__ == 2 && 8 <= __GNUC_MINOR__)) \
65 || defined __clang__ \
66 || (0x5110 <= __SUNPRO_C)
67 /* For compatibility with _GL_NORETURN_FUNCPTR on clang, use
68 __attribute__((__noreturn__)), not _Noreturn. */
69 # define _GL_NORETURN_FUNC __attribute__ ((__noreturn__))
70 #elif 1200 <= _MSC_VER
71 /* Use MSVC specific syntax. */
72 # define _GL_NORETURN_FUNC __declspec (noreturn)
73 #elif defined __cplusplus
74 /* Use ISO C++11 syntax when the compiler supports it. */
75 # if (__cplusplus >= 201103 && !(__GNUC__ == 4 && __GNUC_MINOR__ == 7)) \
77 # define _GL_NORETURN_FUNC [[noreturn]]
78 /* clang++ supports the _Noreturn keyword, but g++ doesn't. */
79 # elif defined __clang__
80 # define _GL_NORETURN_FUNC _Noreturn
82 # define _GL_NORETURN_FUNC /* empty */
85 /* Use ISO C11 syntax when the compiler supports it. */
86 # if __STDC_VERSION__ >= 201112 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
87 # define _GL_NORETURN_FUNC _Noreturn
89 # define _GL_NORETURN_FUNC /* empty */
93 /* Declares that a function is nonreturning.
94 Use in types and declarations that involve function pointers:
95 _GL_NORETURN_FUNCPTR void (*funcptr) (void);
97 #if (3 <= __GNUC__ || (__GNUC__ == 2 && 8 <= __GNUC_MINOR__)) \
98 || defined __clang__ \
99 || (0x5110 <= __SUNPRO_C)
100 # define _GL_NORETURN_FUNCPTR __attribute__ ((__noreturn__))
102 # define _GL_NORETURN_FUNCPTR /* empty */
105 /* Comments about the compiler dependent language features:
106 - '_Noreturn' - standardized by ISO C11, available in C only (not in C++),
107 and not applicable to pointers.
108 - '[[noreturn]]' - standardized by ISO C++11, available in C++ only,
109 and not applicable to pointers.
110 - '__attribute__((__noreturn__))' - available in GCC and clang only,
112 - '__declspec(noreturn)' - available in MSVC only,
113 both in C and C++, not applicable to pointers.
116 #endif /* _NORETURN_H */