From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 41ACC385C422; Wed, 14 Feb 2024 13:37:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 41ACC385C422 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707917846; bh=kOkfAwpxsmgwG+jHhHg3ITE8ge313YiOxRM0Ggrowc8=; h=From:To:Subject:Date:From; b=i5m7ezjizPiPP2J9objgoYkMkmBszTLpcU7qalFieCANYMnOMXhRWsewnrP/ZnsLC n9vnfp+4efKVlFwILprykYntA/rjKfaQlS9VMMTmCmcLZ5A5J0ef++u5JfbqYG4IqH OjkjVJH+ugzH+BcyOR06jH4Tibg6c/QmF40nPGy0= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-8976] pretty-print: Fix up ptrdiff handling for %tu, %to, %tx X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 5352ede92483b949e811cbdcdfaec5378f3e06d6 X-Git-Newrev: e8971ef99505161d09c9bf0174b38fa15fc6e59f Message-Id: <20240214133726.41ACC385C422@sourceware.org> Date: Wed, 14 Feb 2024 13:37:26 +0000 (GMT) List-Id: https://gcc.gnu.org/g:e8971ef99505161d09c9bf0174b38fa15fc6e59f commit r14-8976-ge8971ef99505161d09c9bf0174b38fa15fc6e59f Author: Jakub Jelinek Date: Wed Feb 14 14:35:32 2024 +0100 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 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 * 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. Diff: --- gcc/fortran/error.cc | 15 ++++++++------- gcc/pretty-print.cc | 13 ++++++++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/gcc/fortran/error.cc b/gcc/fortran/error.cc index c346552235b3..65e38b0e8667 100644 --- a/gcc/fortran/error.cc +++ b/gcc/fortran/error.cc @@ -886,13 +886,14 @@ error_print (const char *type, const char *format0, va_list argp) format++; if (*format == 'u') { - ptrdiff_t ptrdiffval = spec[n++].u.ptrdiffval; - if (sizeof (ptrdiff_t) == sizeof (int)) - error_uinteger ((unsigned) ptrdiffval); - else if (sizeof (ptrdiff_t) == sizeof (long)) - error_uinteger ((unsigned long) ptrdiffval); - else - error_uinteger (ptrdiffval); + unsigned long long a = spec[n++].u.ptrdiffval, m; +#ifdef PTRDIFF_MAX + m = PTRDIFF_MAX; +#else + m = INTTYPE_MAXIMUM (ptrdiff_t); +#endif + m = 2 * m + 1; + error_uinteger (a & m); } else error_integer (spec[n++].u.ptrdiffval); diff --git a/gcc/pretty-print.cc b/gcc/pretty-print.cc index 825175787a84..67c213bbaf21 100644 --- a/gcc/pretty-print.cc +++ b/gcc/pretty-print.cc @@ -752,6 +752,9 @@ output_buffer::~output_buffer () obstack_free (&formatted_obstack, NULL); } +#ifndef PTRDIFF_MAX +#define PTRDIFF_MAX INTTYPE_MAXIMUM (ptrdiff_t) +#endif /* Format an integer given by va_arg (ARG, type-specifier T) where type-specifier is a precision modifier as indicated by PREC. F is @@ -783,7 +786,15 @@ output_buffer::~output_buffer () break; \ \ case 4: \ - if (sizeof (ptrdiff_t) <= sizeof (int)) \ + if (T (-1) >= T (0)) \ + { \ + unsigned long long a = va_arg (ARG, ptrdiff_t); \ + unsigned long long m = PTRDIFF_MAX; \ + m = 2 * m + 1; \ + pp_scalar (PP, "%" HOST_LONG_LONG_FORMAT F, \ + a & m); \ + } \ + else if (sizeof (ptrdiff_t) <= sizeof (int)) \ pp_scalar (PP, "%" F, \ (int) va_arg (ARG, ptrdiff_t)); \ else if (sizeof (ptrdiff_t) <= sizeof (long)) \