[clang] [Headers] Don't declare unreachable() from stddef.h in C++ (PR #86748)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 29 08:20:14 PDT 2024
AaronBallman wrote:
> I mean that `stddef.h` would be modified to provide it as a function instead of a macro. In C++ it would simply never be a macro.
Yeah, that could be done too, but it seems pretty unclean IMO (at least when aiming for an ideal implementation) -- C++ changing the contents of C headers in ways that are observable to the user is pretty gross. For example, writing a C header file that includes stddef.h and some inline functions may then compile quite differently whether included in a C TU or a C++ TU. e.g.,
```
// MyAwesomeLibrary.h
#include <stddef.h>
#ifndef unreachable
extern void my_special_unreachable_handler_only_works_in_c_for_reasons(void);
#define unreachable() (my_special_unreachable_handler_only_works_in_c_for_reasons(),*(int *)nullptr)
#endif
enum CoolStuff { Awesome, Radical, Tubular };
inline int func(enum CoolStuff CS) {
switch (CS) {
case Awesome: ... break;
case Radical: ... break;
case Tubular: ... break;
}
unreachable();
}
```
where the expectation is that because we're including stddef.h, the only time the fallback is needed is with C standard libraries that don't yet support C23. It's a pretty contrived example, so don't read into it too much, it's more that I'm not certain of the usability of quietly changing standard interfaces in the C standard library.
https://github.com/llvm/llvm-project/pull/86748
More information about the cfe-commits
mailing list