From patchwork Wed Aug 8 16:24:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 955192 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-483414-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=hotmail.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="y6frf162"; 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 41lxYw1ybCz9s1c for ; Thu, 9 Aug 2018 02:25:06 +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:from :to:subject:date:message-id:references:in-reply-to:content-type :mime-version; q=dns; s=default; b=yzEvJyx/SX3m5EdJIBgQYohNq+RoV f0IsPwrFtTFFx4MAL1/4EbxfGGP2qGQoIburBCjNDh83HC5fw/hc/S7JA5jgqCCG yTTmTLcpmhlMM0PNTqAuxSUWhwd32FbRKrf7cts58xng6C0INMBHUqR9aI7HsR33 3bLUzn/esVL20s= 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:from :to:subject:date:message-id:references:in-reply-to:content-type :mime-version; s=default; bh=0wWciAFxRmsq+0PPbwHzu9A/jL8=; b=y6f rf162vvRhg/AFzDkficK0xu4DyUQpFJuDeIG7cVHaxu2II6VHtVmSoNKGm6J2Nm9 9lrQlxWpGP7yLlHNC1hOTFp3kT8pzMvcmD0LXMVc7IaefRkyJAWCiNi/7HkMppuI NXGyR8ulLRA+7n4EP9iXPLqBUTSazVNw1/2xIT9U= Received: (qmail 52942 invoked by alias); 8 Aug 2018 16:24:49 -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 52608 invoked by uid 89); 8 Aug 2018 16:24:24 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.3 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=HTo:U*burnus, H*Ad:U*burnus, thorough, 20180731 X-HELO: EUR03-AM5-obe.outbound.protection.outlook.com Received: from mail-oln040092070101.outbound.protection.outlook.com (HELO EUR03-AM5-obe.outbound.protection.outlook.com) (40.92.70.101) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 08 Aug 2018 16:24:18 +0000 Received: from VE1EUR03FT008.eop-EUR03.prod.protection.outlook.com (10.152.18.54) by VE1EUR03HT195.eop-EUR03.prod.protection.outlook.com (10.152.19.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.20.1038.13; Wed, 8 Aug 2018 16:24:15 +0000 Received: from AM5PR0701MB2657.eurprd07.prod.outlook.com (10.152.18.51) by VE1EUR03FT008.mail.protection.outlook.com (10.152.18.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.20.1038.3 via Frontend Transport; Wed, 8 Aug 2018 16:24:14 +0000 Received: from AM5PR0701MB2657.eurprd07.prod.outlook.com ([fe80::24cf:823c:758c:41b7]) by AM5PR0701MB2657.eurprd07.prod.outlook.com ([fe80::24cf:823c:758c:41b7%5]) with mapi id 15.20.1059.007; Wed, 8 Aug 2018 16:24:14 +0000 From: Bernd Edlinger To: "gcc-patches@gcc.gnu.org" , "fortran@gcc.gnu.org" , Richard Biener , Tobias Burnus Subject: [PING] [PATCH] Create internally nul terminated string literals in fortan FE Date: Wed, 8 Aug 2018 16:24:14 +0000 Message-ID: References: In-Reply-To: received-spf: None (protection.outlook.com: hotmail.de does not designate permitted sender hosts) authentication-results: spf=none (sender IP is ) smtp.mailfrom=bernd.edlinger@hotmail.de; MIME-Version: 1.0 Hi, I'd like to ping this patch: https://gcc.gnu.org/ml/fortran/2018-08/msg00000.html I attach a new version, which contains only a minor white-space change from the previous version, in the function header of gfc_build_hollerith_string_const to contain "static tree" on one line instead of two. Thanks Bernd. On 08/01/18 13:32, Bernd Edlinger wrote: > Hi, > > this patch changes the Fortan FE to create NUL terminated STRING_CST > objects.  This is a cleanup in preparation of a more thorough check > on the STRING_CST objects in the middle-end. > > > Bootstrapped and reg-tested on x86_64-pc-linux-gnu. > Is it OK for trunk? > > > Thanks > Bernd. 2018-08-01 Bernd Edlinger * trans-array.c (gfc_conv_array_initializer): Remove excess precision from overlength string initializers. * trans-const.c (gfc_build_wide_string_const): Make the internal representation of STRING_CST properly NUL terminated. (gfc_build_hollerith_string_const): New helper function. (gfc_conv_constant_to_tree): Use it. diff -pur gcc/fortran/trans-array.c gcc/fortran/trans-array.c --- gcc/fortran/trans-array.c 2018-07-02 09:24:43.000000000 +0200 +++ gcc/fortran/trans-array.c 2018-08-01 06:45:20.529923246 +0200 @@ -5964,6 +5964,32 @@ gfc_conv_array_initializer (tree type, g { case EXPR_CONSTANT: gfc_conv_constant (&se, c->expr); + + /* See gfortran.dg/charlen_15.f90 for instance. */ + if (TREE_CODE (se.expr) == STRING_CST + && TREE_CODE (type) == ARRAY_TYPE) + { + tree atype = type; + while (TREE_CODE (TREE_TYPE (atype)) == ARRAY_TYPE) + atype = TREE_TYPE (atype); + if (TREE_CODE (TREE_TYPE (atype)) == INTEGER_TYPE + && tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (se.expr))) + > tree_to_uhwi (TYPE_SIZE_UNIT (atype))) + { + unsigned HOST_WIDE_INT size + = tree_to_uhwi (TYPE_SIZE_UNIT (atype)); + unsigned unit + = TYPE_PRECISION (TREE_TYPE (atype)) / BITS_PER_UNIT; + const char *p = TREE_STRING_POINTER (se.expr); + char *q = (char *)xmalloc (size + unit); + + memcpy (q, p, size); + memset (q + size, 0, unit); + se.expr = build_string (size + unit, q); + TREE_TYPE (se.expr) = atype; + free (q); + } + } break; case EXPR_STRUCTURE: diff -pur gcc/fortran/trans-const.c gcc/fortran/trans-const.c --- gcc/fortran/trans-const.c 2018-06-10 14:50:03.000000000 +0200 +++ gcc/fortran/trans-const.c 2018-07-31 20:15:21.721153877 +0200 @@ -93,13 +93,16 @@ gfc_build_wide_string_const (int kind, s int i; tree str, len; size_t size; + size_t elem; char *s; i = gfc_validate_kind (BT_CHARACTER, kind, false); - size = length * gfc_character_kinds[i].bit_size / 8; + elem = gfc_character_kinds[i].bit_size / 8; + size = (length + 1) * elem; s = XCNEWVAR (char, size); gfc_encode_character (kind, length, string, (unsigned char *) s, size); + memset (s + size - elem, 0, elem); str = build_string (size, s); free (s); @@ -131,6 +134,30 @@ gfc_build_localized_cstring_const (const char *msg } +/* Build a hollerith string constant. */ + +static tree +gfc_build_hollerith_string_const (size_t length, const char *s) +{ + tree str; + tree len; + char *p; + + p = XCNEWVAR (char, length + 1); + memcpy (p, s, length); + p[length] = '\0'; + str = build_string (length + 1, p); + free (p); + len = size_int (length); + TREE_TYPE (str) = + build_array_type (gfc_character1_type_node, + build_range_type (gfc_charlen_type_node, + size_one_node, len)); + TYPE_STRING_FLAG (TREE_TYPE (str)) = 1; + return str; +} + + /* Return a string constant with the given length. Used for static initializers. The constant will be padded or truncated to match length. */ @@ -363,8 +390,8 @@ gfc_conv_constant_to_tree (gfc_expr * expr) return res; case BT_HOLLERITH: - return gfc_build_string_const (expr->representation.length, - expr->representation.string); + return gfc_build_hollerith_string_const (expr->representation.length, + expr->representation.string); default: gcc_unreachable ();