You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[[Extended example at Compiler Explorer](https://godbolt.org/z/T66Wj5YKv)]
265
265
266
266
[^gcc-fd_arg]: GCC team, [Using the GNU Compiler Collection (GCC): 6.35.1 Common Function Attributes: fd_arg](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-fd_005farg-function-attribute), GCC Manual, 2024-08-01.
267
+
268
+
### Mark functions that never return
269
+
270
+
| Attribute | Supported since | Type | Description |
| `noreturn` | GCC 2.5.0<br/>Clang 4.0.0 | Function | Mark functions that never return. |
273
+
274
+
The `noreturn` attribute indicates that the annotation function never return control flow to the calling function (e.g. functions that terminate the application such as `abort()` and `exit()`, throw exceptions, loop indefinitely, etc.).
275
+
276
+
Using this attribute allows the compiler to optimize the generated code without regard to what would happen if the annotated function ever did return. In addition, compiler can generate a diagnostic for any function declared as `noreturn` that appears to return to its caller[^clang-noreturn] and `noreturn` functions can improve the accuracy of other diagnostics, e.g., by helping the compiler reduce false warnings for uninitialized variables[^gcc-noreturn].
277
+
278
+
Users should be careful not to assume that registers saved by the calling function are restored before calling the `noreturn` function.
279
+
280
+
#### Example usage
281
+
282
+
~~~c
283
+
// Denotes that fatal will never return
284
+
void fatal () __attribute__ ((noreturn));
285
+
286
+
void /* It does not make sense for a noreturn function to have a return type other than void. */
287
+
fatal (...)
288
+
{
289
+
... /* Print error message. */ ...
290
+
exit (1);
291
+
}
292
+
~~~
293
+
294
+
An alternative way to declare that a function does not return before the `noreturn` attribute was added in GCC 2.5.0 was[^gcc-voidfn]:
295
+
296
+
~~~c
297
+
typedefvoidvoidfn ();
298
+
volatile voidfn fatal;
299
+
~~~
300
+
301
+
[[Extended example at Compiler Explorer](https://godbolt.org/z/1csnxsjrc)]
302
+
303
+
[^gcc-noreturn]: GCC team, [Using the GNU Compiler Collection (GCC): 6.35.1 Common Function Attributes: noreturn](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute), GCC Manual, 2024-08-01.
304
+
[^gcc-voidfn]: GCC team, [Using the GNU Compiler Collection (GCC): 6.35.1 Common Function Attributes: noreturn](https://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Function-Attributes.html), GCC Manual, 2003-05-25.
0 commit comments