From patchwork Thu Oct 4 02:55:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Law X-Patchwork-Id: 978627 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-486914-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="vn2UKXOG"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42QcvF1R7Cz9s7T for ; Thu, 4 Oct 2018 12:55:17 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=YfI4xN8ZweJ2aVBCPFZrkLLeERybj4ijcOD8YzrgutGdRoc7/B sdBq0NgvsSLFIMsigSWR19O36lCS4uUmYFRE0EP3liS1htzlWtOTPvHPUukMVpm5 hl3GhJdgrJTDWzEOVZl9jFwX7ltAHtxH9+tHTF0DHSmwRedw0cEIP4GHc= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=wwjvxCSh+Pysn+45XQmkPjYwuqw=; b=vn2UKXOGqoaSn6PJ4o0N X/s9PxZht8Z6d5UNoEL/XDVYKF5USrDc0pHHZR6LtIJgo3gwUJFs7IMLDH+5wip/ pptQwF83IppoGv/26w2GD0G37IK+xBmTUH7dOTn6Foi56hsMTUyZE8cTmdCrpTdr Cq4ff971+54ApaLWkafDWII= Received: (qmail 70115 invoked by alias); 4 Oct 2018 02:55:09 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 70079 invoked by uid 89); 4 Oct 2018 02:55:08 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 04 Oct 2018 02:55:07 +0000 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C5F464E91C for ; Thu, 4 Oct 2018 02:55:05 +0000 (UTC) Received: from localhost.localdomain (ovpn-112-4.rdu2.redhat.com [10.10.112.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 218651084186 for ; Thu, 4 Oct 2018 02:55:02 +0000 (UTC) To: gcc-patches From: Jeff Law Subject: [committed] Fix sprintf warnings for targets with 2 byte wchar_t Openpgp: preference=signencrypt Message-ID: <25a50c5c-abd0-23ee-4425-eab5154e88ff@redhat.com> Date: Wed, 3 Oct 2018 20:55:01 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.0 MIME-Version: 1.0 X-IsSubscribed: yes As David noted several weeks ago, we broke the sprintf warnings in the testsuite on AIX and more generally on targets with 2 byte wide characters. This patch removes the hardcoded size of 4 bytes for a wchar. Instead it re-purposes an existing routine from the Fortran front-end which will return a standard C type for a given string. ie, if I pass the routine "short int", it returns short_integer_type_node. This allows us to solve the wchar problem because MODIFIED_WCHAR_TYPE uses types like "short int", "int", "unsigned int", etc to describe a wchar. So we just pass MODIFIED_WCHAR_TYPE to get_typenode_from_name and we get back a suitable type_node. We can then look at the size of the returned type node. Spot tested on aix 7. Bootstrapped and regression tested on x86-64. Installing on the trunk momentarily. Jeff * gimple-ssa-sprintf.c (format_string): Do not hardcode size of target's wchar_t. * tree.c (get_typenode_from_name): Moved from fortran/trans-types.c. * tree.h (get_typenode_from_name): Prototype. * trans-types.c (get_typenode_from_name): Moved into gcc/tree.c. diff --git a/gcc/fortran/trans-types.c b/gcc/fortran/trans-types.c index 46f6d8c03a6..1a813eaf4d4 100644 --- a/gcc/fortran/trans-types.c +++ b/gcc/fortran/trans-types.c @@ -218,43 +218,6 @@ get_int_kind_from_node (tree type) return -1; } -/* Return a typenode for the "standard" C type with a given name. */ -static tree -get_typenode_from_name (const char *name) -{ - if (name == NULL || *name == '\0') - return NULL_TREE; - - if (strcmp (name, "char") == 0) - return char_type_node; - if (strcmp (name, "unsigned char") == 0) - return unsigned_char_type_node; - if (strcmp (name, "signed char") == 0) - return signed_char_type_node; - - if (strcmp (name, "short int") == 0) - return short_integer_type_node; - if (strcmp (name, "short unsigned int") == 0) - return short_unsigned_type_node; - - if (strcmp (name, "int") == 0) - return integer_type_node; - if (strcmp (name, "unsigned int") == 0) - return unsigned_type_node; - - if (strcmp (name, "long int") == 0) - return long_integer_type_node; - if (strcmp (name, "long unsigned int") == 0) - return long_unsigned_type_node; - - if (strcmp (name, "long long int") == 0) - return long_long_integer_type_node; - if (strcmp (name, "long long unsigned int") == 0) - return long_long_unsigned_type_node; - - gcc_unreachable (); -} - static int get_int_kind_from_name (const char *name) { diff --git a/gcc/gimple-ssa-sprintf.c b/gcc/gimple-ssa-sprintf.c index 88e952828e1..471bfc45eb8 100644 --- a/gcc/gimple-ssa-sprintf.c +++ b/gcc/gimple-ssa-sprintf.c @@ -2179,7 +2179,19 @@ format_string (const directive &dir, tree arg, vr_values *) fmtresult res; /* Compute the range the argument's length can be in. */ - int count_by = dir.specifier == 'S' || dir.modifier == FMT_LEN_l ? 4 : 1; + int count_by = 1; + if (dir.specifier == 'S' || dir.modifier == FMT_LEN_l) + { + /* Get a node for a C type that will be the same size + as a wchar_t on the target. */ + tree node = get_typenode_from_name (MODIFIED_WCHAR_TYPE); + + /* Now that we have a suitable node, get the number of + bytes it occupies. */ + count_by = int_size_in_bytes (node); + gcc_checking_assert (count_by == 2 || count_by == 4); + } + fmtresult slen = get_string_length (arg, count_by); if (slen.range.min == slen.range.max && slen.range.min < HOST_WIDE_INT_MAX) diff --git a/gcc/tree.c b/gcc/tree.c index 748ece690ea..d7dca77d2b2 100644 --- a/gcc/tree.c +++ b/gcc/tree.c @@ -14408,6 +14408,43 @@ expr_type_first_operand_type_p (tree_code code) } } +/* Return a typenode for the "standard" C type with a given name. */ +tree +get_typenode_from_name (const char *name) +{ + if (name == NULL || *name == '\0') + return NULL_TREE; + + if (strcmp (name, "char") == 0) + return char_type_node; + if (strcmp (name, "unsigned char") == 0) + return unsigned_char_type_node; + if (strcmp (name, "signed char") == 0) + return signed_char_type_node; + + if (strcmp (name, "short int") == 0) + return short_integer_type_node; + if (strcmp (name, "short unsigned int") == 0) + return short_unsigned_type_node; + + if (strcmp (name, "int") == 0) + return integer_type_node; + if (strcmp (name, "unsigned int") == 0) + return unsigned_type_node; + + if (strcmp (name, "long int") == 0) + return long_integer_type_node; + if (strcmp (name, "long unsigned int") == 0) + return long_unsigned_type_node; + + if (strcmp (name, "long long int") == 0) + return long_long_integer_type_node; + if (strcmp (name, "long long unsigned int") == 0) + return long_long_unsigned_type_node; + + gcc_unreachable (); +} + /* List of pointer types used to declare builtins before we have seen their real declaration. diff --git a/gcc/tree.h b/gcc/tree.h index a0f24b61ef1..1e59dd59cf3 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -4987,6 +4987,9 @@ extern tree get_base_address (tree t); of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */ extern tree array_ref_element_size (tree); +/* Return a typenode for the "standard" C type with a given name. */ +extern tree get_typenode_from_name (const char *); + /* Return a tree representing the upper bound of the array mentioned in EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */ extern tree array_ref_up_bound (tree);