Page MenuHomeFreeBSD

cdefs.h: Don't define _Static_assert helper in C++ < C++11
AbandonedPublic

Authored by imp on Jun 19 2024, 11:11 PM.
Tags
None
Referenced Files
Unknown Object (File)
Wed, Oct 22, 4:41 AM
Unknown Object (File)
Sep 11 2025, 9:44 PM
Unknown Object (File)
Sep 7 2025, 4:57 PM
Unknown Object (File)
Sep 7 2025, 5:15 AM
Unknown Object (File)
Aug 21 2025, 12:15 AM
Unknown Object (File)
Aug 14 2025, 1:10 AM
Unknown Object (File)
Aug 11 2025, 9:50 AM
Unknown Object (File)
Aug 4 2025, 3:32 PM
Subscribers

Details

Reviewers
dim
jrtc27
kib
Summary

_Static_assert is a C11 keyword that maps to the C11 static_assert
macro. In C++ 2011 and newer, static_assert is the keyword so we map
_Static_assert to that to allow C11 headers to compile with C++ 2011.
For C++ prior to 2011, we fell back to one of many other ways to do
this, which conflicted with other things in the tree (which compile
on non C++ systems).

Sponsored by: Netflix

Test Plan

So I'm not at all sure about this.
We can't safely define _Static_assert as a macro for C++ < C++11, since it's used in the base and since the base defines static_assert as _Static_assert there. In that case, it defines it as #define static_assert(...) _Static_assert(...). But no where else is it defined, leading to issues if the compiler doesn't secretly implement it.
So that leaves me unsure. It fixes the bug https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=276738 by two headers avoiding fighting over this.
But I find it deeply unsatisfying, to be honest.
For modern compilers, it will all work. For non-modern compilers, meh.
I worry that we're breaking older g++ compilers that build something needing our headers, which assume that they can use _Static_assert w/o issue. We do still support compiling USER CODE with older compilers (just not the world). It might help to have an example to argue against...

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 58252
Build 55140: arc lint + arc unit

Event Timeline

imp requested review of this revision.Jun 19 2024, 11:11 PM
imp created this revision.
imp added a reviewer: kib.

_Static_assert is in the implementation namespace (_[A-Z]-prefixed), so we are within our right to define this. Whatever libc++ is doing should be made to be compatible; it looks like it's assuming things about the implementation that aren't true.

I put a note into the original bug report https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=276738#c12 . IMO that should be clarified first, before we try to squeeze that in /usr/include.

sys/sys/cdefs.h
242

The more I think about it, the more we should just delete everything form the #elif to the #endif at the end. That's super old compilers, which I'd like to just drop entirely from cdefs.h.

sys/sys/cdefs.h
252

Make this:

#elif __GNUC_PREREQ__(4,6) || defined(__clang__)

since clang still identifies itself (for historical reasons) as gcc 4.2.1 :)

emaste added inline comments.
sys/sys/cdefs.h
252

the comment below would need to be adjusted (or just avoid repeating the condition in the comment).

imp added inline comments.
sys/sys/cdefs.h
252

That's the problem with the GCC version checks. You'd think you'd need that extra #define, but you'd be wrong. clang does have the cxx_static_assert extension, so adding the || defined(clang) would be useless in all versions of clang that are at all mildly interesting. So it would get swept up in the or above.

But I'm going to abandon this review in favor of a more whole-sale ripping out.

252

Yea, I've just gutted all this code, so I'll be abandoning this as OBE.