Using C11 `stdnoreturn.h` causes compiler warnings in headers
blue42u opened this issue · comments
The public headers introduce a compiler warning related to the noreturn
attribute if stdnoreturn.h
is included before they are. This prevents -Werror
builds from passing for XED users using C11 features:
# gcc --version
gcc (Ubuntu 13.2.0-23ubuntu4) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# cat example.c
#include <stdnoreturn.h>
#include <xed/xed-interface.h>
# gcc example.c -I kits/xed-*/include -Werror
In file included from kits/xed-install-base-2024-09-05-lin-x86-64/include/xed/xed-decoded-inst.h:26,
from kits/xed-install-base-2024-09-05-lin-x86-64/include/xed/xed-decode.h:24,
from kits/xed-install-base-2024-09-05-lin-x86-64/include/xed/xed-interface.h:40,
from example.c:2:
kits/xed-install-base-2024-09-05-lin-x86-64/include/xed/xed-util.h:120:1: error: '_Noreturn' attribute directive ignored [-Werror=attributes]
120 | XED_NORETURN XED_NOINLINE XED_DLL_EXPORT void xed_internal_assert(const char* s, const char* file, int line);
| ^~~~~~~~~~~~
cc1: all warnings being treated as errors
Unfortunately I can't help but be highly suspicious of a hidden bit.ly link posted not even a second after posting my issue. 🫤
@z3t444 If this isn't a bot spam or hacked account, could you post the fix as a PR as per the contribution guidelines instead of through these unconventional means?
Thank you for reporting the issue.
The XED library uses the __attribute__((noreturn))
directive to mark functions that do not return, instead of the _Noreturn
keyword. This decision was made to maintain backward compatibility with C99 standards (before C11) where _Noreturn
is not supported.
When you include <stdnoreturn.h>
(a C11 header) before the XED library, GCC encounters a potential conflict between the _Noreturn
keyword (introduced in C11) and the __attribute__((noreturn))
attribute used in XED. Both serve the same purpose, but GCC may issue warnings when it detects both attributes in the same code, sometimes ignoring one.
To resolve this issue, I recommend swapping the order in which you include headers. By including the XED library headers before including <stdnoreturn.h>
, you ensure that GCC correctly handles the __attribute__((noreturn))
directive used by the XED library, while still allowing you to use the _Noreturn
keyword in your own code if needed.
You can adjust it to:
#include <xed/xed-interface.h>
#include <stdnoreturn.h>
Please let us know if this solution works for you.
Thank you for the comprehensive response. Swapping the order of #include
s does work in this simple example, however in our full codebase we include external C11 headers that do or may in the future include stdnoreturn.h
themselves. Combined with #include
sorting it is generally difficult for us to ensure Xed headers are always #include
d before stdnoreturn.h
.
Please fix the headers to work in a C11 codebase. For C99 compatibility you can use the __attribute__((__noreturn__))
alias, but I recommend using the standardized noreturn
or [[noreturn]]
attributes when compiling in C11 or C23 mode respectively.
A fix will be available with the upcoming external release.