pretty-print: Fix up ptrdiff handling for %tu, %to, %tx
commite8971ef99505161d09c9bf0174b38fa15fc6e59f
authorJakub Jelinek <jakub@redhat.com>
Wed, 14 Feb 2024 13:35:32 +0000 (14 14:35 +0100)
committerJakub Jelinek <jakub@redhat.com>
Wed, 14 Feb 2024 13:35:32 +0000 (14 14:35 +0100)
treebb26e76d7f4bb7a8c1d41fd05f60c7c3d7227ccc
parent5352ede92483b949e811cbdcdfaec5378f3e06d6
pretty-print: Fix up ptrdiff handling for %tu, %to, %tx

This is IMHO more of a theoretical case, I believe my current code
doesn't handle %tu or %to or %tx correctly if ptrdiff_t is not one of
int, long int or long long int.  For size_t and %zd or %zi I'm
using va_arg (whatever, ssize_t) and hope that ssize_t is the signed
type corresponding to size_t which C99 talks about.
For ptrdiff_t there is no type for unsigned type corresponding to
ptrdiff_t and I'm not aware of a portable way on the host to get
such a type (the fmt tests use hacks like
 #define signed /* Type might or might not have explicit 'signed'.  */
 typedef unsigned __PTRDIFF_TYPE__ unsigned_ptrdiff_t;
 #undef signed
but that only works with compilers which predefine __PTRDIFF_TYPE__),
std::make_unsigned<ptrdiff_t> I believe only works reliably if
ptrdiff_t is one of char, signed char, short, int, long or long long,
but won't work e.g. for __int20__ or whatever other weird ptrdiff_t
the host might have.

The following patch assumes host is two's complement (I think we
rely on it pretty much everywhere anyway) and prints unsigned type
corresponding to ptrdiff_t as unsigned long long printing of
ptrdiff_t value masked with 2ULL * PTRDIFF_MAX + 1.  So the only
actual limitation is that it doesn't support ptrdiff_t wider than
long long int.

2024-02-14  Jakub Jelinek  <jakub@redhat.com>

* pretty-print.cc (PTRDIFF_MAX): Define if not yet defined.
(pp_integer_with_precision): For unsigned ptrdiff_t printing
with u, o or x print ptrdiff_t argument converted to
unsigned long long and masked with 2ULL * PTRDIFF_MAX + 1.

* error.cc (error_print): For u printing of ptrdiff_t,
print ptrdiff_t argument converted to unsigned long long and
masked with 2ULL * PTRDIFF_MAX + 1.
gcc/fortran/error.cc
gcc/pretty-print.cc