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: 1898117 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=fjgn/yAY; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TYvSY51Dmz23hM for ; Tue, 13 Feb 2024 19:22:03 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8790A3858401 for ; Tue, 13 Feb 2024 08:22:01 +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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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);