DevSolar / pdclib

The Public Domain C Library
https://pdclib.rootdirectory.de
Creative Commons Zero v1.0 Universal
229 stars 41 forks source link

_PDCLIB_Noreturn not effective in C++ code #21

Open thrimbor opened 3 years ago

thrimbor commented 3 years ago

In nxdk, we have been getting warnings when building libc++ about functions marked as noreturn supposedly returning. I have tracked this down to the function abort, which wasn't marked as noreturn when building C++ code. This is caused by the preprocessor code in _PDCLIB_internal.h (or _PDCLIB_aux.h in our case, we haven't rebased in a while), which relies on __STDC_VERSION__. Unfortunately, __STDC_VERSION__ is not defined in C++.

This is the problematic part:

#if __STDC_VERSION__ < 201112L
#define _PDCLIB_Noreturn
#else
#define _PDCLIB_Noreturn _Noreturn
#endif

I checked how the compiler handles this, and it evaluates the __STDC_VERSION__ < 201112L condition to true, thereby not marking the functions as noreturn despite the compiler supporting it.

I replaced it with the following code, which fixes the warnings mentioned above:

#if defined(__cplusplus) && __cplusplus >= 201103L
#define _PDCLIB_Noreturn [[noreturn]]
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define _PDCLIB_Noreturn _Noreturn
#else
#define _PDCLIB_Noreturn
#endif

There are other uses of __STD_VERSION__ in PDCLib that might need similar treatment.

DevSolar commented 3 years ago

I will make this change for now. I will have to think a bit about any effects that might arise from the PDCLib code being compiled with _Noreturn (or nothing at all), but then used with [[noreturn]]...

JayFoxRox commented 3 years ago

Should be closed by https://github.com/DevSolar/pdclib/commit/068f3b5a108c0d5d994fb488dec6cb69d140ba59 and https://github.com/DevSolar/pdclib/commit/13217c52ef338207e7b08f2866aca4c10ae05c74; please verify and close the issue.

DevSolar commented 3 years ago

I will keep this open until I have had a good look at the implications of mixing _Noreturn and [[noreturn]], and at other instances of __STDC_VERSION__ use. The immediate issue is resolved though.