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)
Dec 23 2023, 1:13 AM
Unknown Object (File)
Dec 12 2023, 5:39 AM
Unknown Object (File)
Nov 10 2023, 2:22 PM
Unknown Object (File)
Nov 9 2023, 1:07 AM
Unknown Object (File)
Nov 8 2023, 2:14 PM
Unknown Object (File)
Oct 7 2023, 11:57 PM
Unknown Object (File)
Oct 7 2023, 1:11 PM
Unknown Object (File)
Sep 26 2023, 5:52 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 Not Applicable
Unit
Tests Not Applicable

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

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.