Page MenuHomeFreeBSD

Fix Q_TOSTR(3) when called with first parameter being const
ClosedPublic

Authored by trasz on Sep 23 2019, 3:22 PM.
Tags
None
Referenced Files
Unknown Object (File)
Tue, Nov 26, 9:57 PM
Unknown Object (File)
Sat, Nov 23, 12:14 AM
Unknown Object (File)
Tue, Nov 12, 9:24 PM
Unknown Object (File)
Oct 30 2024, 7:09 AM
Unknown Object (File)
Oct 30 2024, 7:09 AM
Unknown Object (File)
Oct 30 2024, 7:08 AM
Unknown Object (File)
Oct 30 2024, 7:08 AM
Unknown Object (File)
Oct 30 2024, 6:44 AM
Subscribers

Details

Summary

Fix Q_TOSTR(3) with GCC when it's called with first parameter
being const.

One one hand this is trivial, on the other - it's a rather
weird construct, so I'd rather get some feedback.

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 26651
Build 25025: arc lint + arc unit

Event Timeline

Ok, I researched this a little bit. It looks like Q_BT is only used in two places, neither of which wants a const type. (Well, three places, 3rd being Q_TC(), but... it's easy enough to just use __typeof directly there.)

I think this problem is specific to ancient GCC4. I cannot reproduce it on GCC 6.3 or newer. If it were a newer GCC problem, we could use a C11 _Generic() construct to de-const the reference variable. But newer GCC already de-consts in __typeof(). (Source: https://godbolt.org/z/m-ye3e ) So I think I'd elect to use the same weird hack and just move it to the macro definition rather than this invocation.

E.g.,

#if __GNUC__ > 4 || defined(__clang__)
#define Q_BT(q) __typeof(q)
#else
/* Ancient GCC hack to de-const, remove when GCC4 is removed. */
#define Q_BT(q) __typeof(1 * q)
#endif
#define Q_TC(q, v) ((__typeof(q))(v))

For some reason the '#if GNUC > 4' doesn't seem to work for RISC-V tinderbox builds - they fail as before. It does fix the situation for in-tree GCC though. TBH I don't think narrowing it down is worth it.

This revision was not accepted when it landed; it landed in state Needs Review.Sep 28 2019, 9:54 AM
This revision was automatically updated to reflect the committed changes.
head/sys/sys/qmath.h
61 ↗(On Diff #62681)

Clang defines __GNUC__ (yeah, it's gross), so the #else is dead code. That's why I suggested __GNUC__ > 4 || defined(__clang__) in the first clause of the other version.

#if defined(__GNUC__) && !defined(__clang__) is probably closer to what you intended.