Skip to content

Commit 6f4f0a0

Browse files
committed
Add detailed description of noreturn attribute to Compiler Annotations Guide
Signed-off-by: Thomas Nyman <[email protected]>
1 parent 6d47603 commit 6f4f0a0

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

docs/Compiler-Hardening-Guides/Compiler-Annotations-for-C-and-C++.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Table 1: Recommended attributes
3838
| `fd_arg(`_`fd-index`_`)` | GCC 13.1.0 | Function | Mark open file descriptors in positional arguments. |
3939
| `fd_arg_read(`_`fd-index`_`)` | GCC 13.1.0 | Function | Mark readable file descriptors in positional arguments. |
4040
| `fd_arg_write(`_`fd-index`_`)` | GCC 13.1.0 | Function | Mark writable file descriptors in positional arguments. |
41-
| `noreturn` | GCC 2.95.3<br/>Clang 4.0.0 | Function | The function does not return. |
41+
| `noreturn` | GCC 2.5.0<br/>Clang 4.0.0 | Function | Mark functions that never return. |
4242
| `tainted_args` | GCC 12 | Function or function pointer | Function needs sanitization of its arguments. Used by `-fanalyzer=taint` |
4343

4444
## Performance considerations
@@ -264,3 +264,42 @@ void read_from_file (int fd, void *dst, size_t size) __attribute__ ((fd_arg_read
264264
[[Extended example at Compiler Explorer](https://godbolt.org/z/T66Wj5YKv)]
265265
266266
[^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 |
271+
|:-----------------------------------------------------------------------------------------------|:---------------------------:|:----------------------------:|:------------------------------------------------------------------------------------------------- |
272+
| `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+
typedef void voidfn ();
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.
305+
[^clang-noreturn]: LLVM team, [Attributes in Clang: noreturn](https://clang.llvm.org/docs/AttributeReference.html#noreturn-noreturn), Clang Compiler User's Manual, 2025-03-04.

0 commit comments

Comments
 (0)