From patchwork Mon Aug 20 22:50:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 960007 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-484034-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="OuBM95Pi"; 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 41vTXv0hv0z9s4V for ; Tue, 21 Aug 2018 08:50:22 +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:content-type:mime-version; q=dns; s= default; b=IUreaz/q6AGO09n4eCE14qjvtMI79NaleejTjtfBm/n9Rcjf9+NNM HxBXIupo5QvBC0uXCaelGAp795ObiWz+A0K0eDizEx8fckcsp3++Gok0oGd9Cg2s ABXGHMW4SA8fk2Of9cjE4c6bOQXPZE/fdrvnE313Bvq2Xu0j4zx4/M= 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:content-type:mime-version; s= default; bh=5ryozSyR09h49Rj2kYd6JzEZ0+k=; b=OuBM95PixubH/J69M7Zg bRLXI1TyS2txu7wj2673SGIQ9gZlOr6vpQcUvq+meeVEoUlkGKXmAKekxVzc0t0f yKIvflchQD3YPINP1wIegUzCOPfVMiZ+nUuTh2iW7AXbZzA/mvTHNXrsIZtejqso Gfe9y8L/S/2mtzHNmUnfDDE= Received: (qmail 39386 invoked by alias); 20 Aug 2018 22:50:15 -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 39370 invoked by uid 89); 20 Aug 2018 22:50:14 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.4 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=H*MI:outlook, FORMAT, HX-MS-Exchange-Transport-CrossTenantHeadersStamped:VE1EUR03HT031, 2018-08-21 X-HELO: EUR03-DB5-obe.outbound.protection.outlook.com Received: from mail-oln040092071096.outbound.protection.outlook.com (HELO EUR03-DB5-obe.outbound.protection.outlook.com) (40.92.71.96) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 20 Aug 2018 22:50:12 +0000 Received: from VE1EUR03FT038.eop-EUR03.prod.protection.outlook.com (10.152.18.54) by VE1EUR03HT031.eop-EUR03.prod.protection.outlook.com (10.152.19.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.1080.9; Mon, 20 Aug 2018 22:50:09 +0000 Received: from AM5PR0701MB2657.eurprd07.prod.outlook.com (10.152.18.51) by VE1EUR03FT038.mail.protection.outlook.com (10.152.19.112) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.20.1080.9 via Frontend Transport; Mon, 20 Aug 2018 22:50:08 +0000 Received: from AM5PR0701MB2657.eurprd07.prod.outlook.com ([fe80::24cf:823c:758c:41b7]) by AM5PR0701MB2657.eurprd07.prod.outlook.com ([fe80::24cf:823c:758c:41b7%7]) with mapi id 15.20.1080.010; Mon, 20 Aug 2018 22:50:08 +0000 From: Bernd Edlinger To: "gcc-patches@gcc.gnu.org" , Jeff Law , Richard Biener , Martin Sebor Subject: [PATCH] Use c_getstr to get the format string in gimple-ssa-sprintf.c Date: Mon, 20 Aug 2018 22:50:08 +0000 Message-ID: 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, this simplifies get_format_string in gimple-ssa-sprintf.c to use c_getstr. Bootstrapped and reg-tested on x86_64-pc-linux-gnu. Is it OK for trunk? Thanks Bernd. 2018-08-21 Bernd Edlinger * gimple-ssa-sprintf.c (decl_constant_value): Remove. (get_format_string): Refer to c_getstr. diff -Npur gcc/gimple-ssa-sprintf.c gcc/gimple-ssa-sprintf.c --- gcc/gimple-ssa-sprintf.c 2018-08-17 06:01:14.000000000 +0200 +++ gcc/gimple-ssa-sprintf.c 2018-08-20 22:54:08.724679430 +0200 @@ -443,152 +443,15 @@ target_strtol10 (const char **ps, const return val; } -/* Return the constant initial value of DECL if available or DECL - otherwise. Same as the synonymous function in c/c-typeck.c. */ - -static tree -decl_constant_value (tree decl) -{ - if (/* Don't change a variable array bound or initial value to a constant - in a place where a variable is invalid. Note that DECL_INITIAL - isn't valid for a PARM_DECL. */ - current_function_decl != 0 - && TREE_CODE (decl) != PARM_DECL - && !TREE_THIS_VOLATILE (decl) - && TREE_READONLY (decl) - && DECL_INITIAL (decl) != 0 - && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK - /* This is invalid if initial value is not constant. - If it has either a function call, a memory reference, - or a variable, then re-evaluating it could give different results. */ - && TREE_CONSTANT (DECL_INITIAL (decl)) - /* Check for cases where this is sub-optimal, even though valid. */ - && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) - return DECL_INITIAL (decl); - return decl; -} - /* Given FORMAT, set *PLOC to the source location of the format string and return the format string if it is known or null otherwise. */ static const char* get_format_string (tree format, location_t *ploc) { - if (VAR_P (format)) - { - /* Pull out a constant value if the front end didn't. */ - format = decl_constant_value (format); - STRIP_NOPS (format); - } - - if (integer_zerop (format)) - { - /* FIXME: Diagnose null format string if it hasn't been diagnosed - by -Wformat (the latter diagnoses only nul pointer constants, - this pass can do better). */ - return NULL; - } - - HOST_WIDE_INT offset = 0; - - if (TREE_CODE (format) == POINTER_PLUS_EXPR) - { - tree arg0 = TREE_OPERAND (format, 0); - tree arg1 = TREE_OPERAND (format, 1); - STRIP_NOPS (arg0); - STRIP_NOPS (arg1); - - if (TREE_CODE (arg1) != INTEGER_CST) - return NULL; - - format = arg0; - - /* POINTER_PLUS_EXPR offsets are to be interpreted signed. */ - if (!cst_and_fits_in_hwi (arg1)) - return NULL; - - offset = int_cst_value (arg1); - } - - if (TREE_CODE (format) != ADDR_EXPR) - return NULL; - *ploc = EXPR_LOC_OR_LOC (format, input_location); - format = TREE_OPERAND (format, 0); - - if (TREE_CODE (format) == ARRAY_REF - && tree_fits_shwi_p (TREE_OPERAND (format, 1)) - && (offset += tree_to_shwi (TREE_OPERAND (format, 1))) >= 0) - format = TREE_OPERAND (format, 0); - - if (offset < 0) - return NULL; - - tree array_init; - tree array_size = NULL_TREE; - - if (VAR_P (format) - && TREE_CODE (TREE_TYPE (format)) == ARRAY_TYPE - && (array_init = decl_constant_value (format)) != format - && TREE_CODE (array_init) == STRING_CST) - { - /* Extract the string constant initializer. Note that this may - include a trailing NUL character that is not in the array (e.g. - const char a[3] = "foo";). */ - array_size = DECL_SIZE_UNIT (format); - format = array_init; - } - - if (TREE_CODE (format) != STRING_CST) - return NULL; - - tree type = TREE_TYPE (format); - - scalar_int_mode char_mode; - if (!is_int_mode (TYPE_MODE (TREE_TYPE (type)), &char_mode) - || GET_MODE_SIZE (char_mode) != 1) - { - /* Wide format string. */ - return NULL; - } - - const char *fmtstr = TREE_STRING_POINTER (format); - unsigned fmtlen = TREE_STRING_LENGTH (format); - - if (array_size) - { - /* Variable length arrays can't be initialized. */ - gcc_assert (TREE_CODE (array_size) == INTEGER_CST); - - if (tree_fits_shwi_p (array_size)) - { - HOST_WIDE_INT array_size_value = tree_to_shwi (array_size); - if (array_size_value > 0 - && array_size_value == (int) array_size_value - && fmtlen > array_size_value) - fmtlen = array_size_value; - } - } - if (offset) - { - if (offset >= fmtlen) - return NULL; - - fmtstr += offset; - fmtlen -= offset; - } - - if (fmtlen < 1 || fmtstr[--fmtlen] != 0) - { - /* FIXME: Diagnose an unterminated format string if it hasn't been - diagnosed by -Wformat. Similarly to a null format pointer, - -Wformay diagnoses only nul pointer constants, this pass can - do better). */ - return NULL; - } - - return fmtstr; + return c_getstr (format); } /* For convenience and brevity, shorter named entrypoints of