From patchwork Tue Feb 13 08:21:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 200269 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp395050dyb; Tue, 13 Feb 2024 00:22:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVK6cKHt32ALrha35ztrZbZekib7RSB5imcmgcIT1FetgI90Jybu3RLetHCdZ6TxuP/PP26OqJwvDSofvnEisSKwI2jpQ== X-Google-Smtp-Source: AGHT+IHi63VQzG22MQ1DN4Ucv1STWAqs1KrFwQyHejvdXcTlreFWDEYEH+jxsLh4oGexdB8HfHX4 X-Received: by 2002:a05:620a:4708:b0:787:1a2e:b687 with SMTP id bs8-20020a05620a470800b007871a2eb687mr1650244qkb.19.1707812564229; Tue, 13 Feb 2024 00:22:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707812564; cv=pass; d=google.com; s=arc-20160816; b=phqbzBBVie1D9P77O6lWptUoTndUuvJowNMQlVE+xQN5KQneVWPBBJxyK6xI5VhCXO bEe7Edzc+3rPF88/4b+C4Wfju9cXt3n8ZX9kQwn9frSMhC5sv/PD6t4bSA/6LtXDMIWQ 1DqD9ocehtSPSUT+leFSMPYdmKzfuhZB0KWqsACflwGLnBzadwPAtB42i1jYJ8dpq7PH qXKT707N/Lpv4UiwX9QDPtmMWvc3hISslPWyWaRjiFdgNV+ndZrnB51vxVaaRIjQiW13 qBIjTjs6Xodq8fWLIZDjP1pvOKJRCSBDRCv/vx1+Rbiye4lTB9y5Yc6sqqsX7xNoKEli kAjg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=opzAUgd0awgBUQtkOZu9NdRc04eQWxTVSBjtsiKJ65w=; fh=f+pE4zHJT40aezz9VnIuLTudWmHSYuEi8+WMKGP5lBc=; b=S2TrAysuf5ktZ8rAGDwvPtOFXNgcaCui6Qc4Ri2TuTEyMgPoSazNdMThVlB4kmh2re JGj1s5eaCmUAFXvH/BSKeOZcQwjCEfvDJiQcCSzhJ5ygLMbQroWVbp7V2DbT6ftg3HVz 6vSB+gH1CoLnMEP44zo+naS8DZe8f/L4Pe5NHrK4U6orW3uOXzjMglg7m8sITLr9slR4 wXXk+Bqm83VLGx94boZarX0XZETXPh22IgTMPY5Ioi7XS0XuwL03IcWs9mjeAbhxLXVF QvHtvZr2fzwFsab8HVG/UoRdvvMeBTTIDFXcZGMm3nnVG6trJICXHvAD9DQHLTaW7tlY NWuQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="fjgn/yAY"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCVJ97EnNouXuAkzNK1ic/CzK50AWlC9MxS/yKxDjvJCmkJBzI1So8DCq/TEzsfpcmLxE5QqoR/Ig2QkN4N1+zOWJ/eJOw== Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id y7-20020a05620a44c700b0078722673b7csi108495qkp.492.2024.02.13.00.22.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 00:22:44 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="fjgn/yAY"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E89C63858C39 for ; Tue, 13 Feb 2024 08:22:43 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 858163858D20 for ; Tue, 13 Feb 2024 08:21:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 858163858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 858163858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707812482; cv=none; b=fLqZeOnmcQalYwwTm4ufWKVOxkUZptQDOMEoezzZxU6ukNJh7RFy4hgIL+5AQBXf/nnHS6rm1BjexJSa/eLOP7gCIiIBdu9XIfehwRaft298nsOQSTrV+8Q0ue2PsUIFAQKkkrp7iYd4+mcJDnyQ8NHPBhKzU9S0L/fyjIStvDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707812482; c=relaxed/simple; bh=1MbmEyEvUB7V92qrnTVZmOoBMPbj3+jLn/P/0d5QZ3U=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=jKIm+nigsyhKUxQWIfqzmAdYf4wKwVoke40joddnanOxZtp9pdEijlqAE6b32T+pV4eIlOmK5CicM0hzgcg6YjlducMhlqTGfLsxZWJvSY7h1gaUIlOkY8HFlVrUMdLwBn047CLBSNxMy9g6CDPVVq/jddNsPPCo1+hW0G29chg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707812480; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=opzAUgd0awgBUQtkOZu9NdRc04eQWxTVSBjtsiKJ65w=; b=fjgn/yAYQJgalA5wTHYGFIWaCilx2uwdHt84RyKBDJWQtn3FG17F0xFpBrtOJS1+fAcBI7 Ey2jJ1iDFEtvJN/yjyZ4gtjeSTomF6vHqasX1EF2a3e/U65iaPRdgSvnXIAoEVBtBJmeYY WuK3KUf+BvsmQj4QOZEfi10Ryw/g05w= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-648-86Xe1OQBP1yER2P49E0mSw-1; Tue, 13 Feb 2024 03:21:16 -0500 X-MC-Unique: 86Xe1OQBP1yER2P49E0mSw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1AA5E1C07F4C; Tue, 13 Feb 2024 08:21:16 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.8]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C64361C05E0F; Tue, 13 Feb 2024 08:21:15 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 41D8LDeZ1327614 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 13 Feb 2024 09:21:13 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41D8LCs71327613; Tue, 13 Feb 2024 09:21:12 +0100 Date: Tue, 13 Feb 2024 09:21:11 +0100 From: Jakub Jelinek To: Richard Biener , "Joseph S. Myers" Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] pretty-print: Fix up ptrdiff handling for %tu, %to, %tx Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790771267131526245 X-GMAIL-MSGID: 1790771267131526245 Hi! 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. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-02-13 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. Jakub --- gcc/pretty-print.cc.jj 2024-02-12 12:44:09.344335964 +0100 +++ gcc/pretty-print.cc 2024-02-12 19:09:27.594483176 +0100 @@ -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)) \ --- gcc/fortran/error.cc.jj 2024-02-12 12:44:09.343335978 +0100 +++ gcc/fortran/error.cc 2024-02-12 19:11:23.564886530 +0100 @@ -886,13 +886,14 @@ error_print (const char *type, const cha 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);