I am using C99 under GCC.
I have a function declared static inline in a header that I cannot modify.
The function never returns but is not marked __attribute__((noreturn)).
How can I call the function in a way that tells the compiler it will not return?
I am calling it from my own noreturn function, and partly want to suppress the "noreturn function returns" warning but also want to help the optimizer etc.
I have tried including a declaration with the attribute but get a warning about the repeated declaration.
I have tried creating a function pointer and applying the attribute to that, but it says the function attribute cannot apply to a pointed function.
From the function you defined, and which calls the external function, add a call to
__builtin_unreachablewhich is built into at least GCC and Clang compilers and is markednoreturn. In fact, this function does nothing else and should not be called. It's only here so that the compiler can infer that program execution will stop at this point.Edit: Just to clarify a few points raised in the comments, and generally give a bit of context:
__attribute__((noreturn))is an annotation (likeconst) which is a way for the programmer to inform the compiler that he's absolutely sure a function will not return. Following the trust but verify principle, the compiler tries to prove that the function does indeed not return. If may then issue an error if it proves the function may return, or a warning if it was not able to prove whether the function returns or not.__builtin_unreachablehas undefined behaviour because it is not meant to be called. It's only meant to help the compiler's static analysis. Indeed the compiler knows that this function does not return, so any following code is provably unreachable (except through a jump).Once the compiler has established (either by itself, or with the programmer's help) that some code is unreachable, it may use this information to do optimizations like these:
__builtin_unreachable()is unreachable.noreturn. That's what happens foryour_function.purefunctions) may be removed.Illustration:
external_functioncannot be removed because it might have side-effects. In fact, it probably has at least the side effect of terminating the process!your_functionmay be removedHere's another example showing how code before the unreachable point may be removed