From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 020AB384B83F for ; Thu, 20 Oct 2022 20:21:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 020AB384B83F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666297269; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7ayo0qYLEcUf9sTj0OOPtc9PxWAlkg9gyWp5VZJ+OEI=; b=XocpUwphDfaGS2aDW25NctfQlWOQK3IjMr20QOXU03JdtZUic6nP9wLRCZeVGHjcr/PHYi I6eB2THv0L1z/MLue+zczuQXZWeaF2t+uejYn/b8Hm/ap2KKzoS/z491zBnor5ZtrD436a +i28BPrrD1pnll3va6vLVfqnVe4YRps= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-371-3mUMPg0OMZGrdNzrW_jzvw-1; Thu, 20 Oct 2022 16:21:08 -0400 X-MC-Unique: 3mUMPg0OMZGrdNzrW_jzvw-1 Received: by mail-qk1-f199.google.com with SMTP id bm16-20020a05620a199000b006ee95743ad2so969914qkb.14 for ; Thu, 20 Oct 2022 13:21:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7ayo0qYLEcUf9sTj0OOPtc9PxWAlkg9gyWp5VZJ+OEI=; b=8M9nfoFH6qP93uVMCtzaaXAjWP8z/sc9yrUfROzEoJqUDIZS/R9cCCbRZzUg/JMqaX 1NXx00OmTldKdY3LjMKVctFadD2QwaHT+Qz0SrTBTlJzoQUlT1oJ2uISZ3uBGweDWbcs h7J1VnXI51fpUXpPNhSWZjzxZ5bfcRBo86b7/45S8WaNs39GWT9PYNbUvrq1Jo0vbcCE rTfzYPNKb7B4l+kzWSGo3Y79MkaPYgCHU5xVuswb6QwUZMPumfW2UNYowu6kUp1RFlzU sXmX8rg0aNJRNc5WnvWv4vfE+x7BReMrxcdiau5rqT0+qN2/zZPYPVazXWoTK1QxVbI2 siWQ== X-Gm-Message-State: ACrzQf3GaosT85l/edBhuhMlRt7qz/bBcbcG+N5JNzmJQ3zMci1PrZuQ Nxh7wRW8avJbO75CmXcCC97hFjl1XPBgdtBS4H4Ow4A8d3ouoVu+b4g5SQ3AVBLEHxgr9UHh1Ja 1ELAi/1oBHI2+ObY9Zbow2p4biLUeQninC6TxbF9Wzzs2pUM108xrrARLUEZjuZi4qA== X-Received: by 2002:a05:622a:1a9f:b0:39c:ae32:9f7f with SMTP id s31-20020a05622a1a9f00b0039cae329f7fmr13346925qtc.80.1666297267333; Thu, 20 Oct 2022 13:21:07 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6+/CpnLl9oOvjwSHspKRu9+G5DsEXBui93JIfnvxXwwp+whx3o/o3PnRk9YOTp5hxWVmLTMg== X-Received: by 2002:a05:622a:4a06:b0:393:9a99:a0b9 with SMTP id fv6-20020a05622a4a0600b003939a99a0b9mr12693681qtb.665.1666297256566; Thu, 20 Oct 2022 13:20:56 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id cm24-20020a05622a251800b0039492d503cdsm6774114qtb.51.2022.10.20.13.20.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Oct 2022 13:20:55 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [PATCH RFA] tree: add build_string_literal overloads Date: Thu, 20 Oct 2022 16:20:53 -0400 Message-Id: <20221020202053.3389227-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-pc-linux-gnu, OK for trunk? -- >8 -- Simplify several calls to build_string_literal by not requiring redundant strlen or IDENTIFIER_* in the caller. I also corrected a wrong comment on IDENTIFIER_LENGTH. gcc/ChangeLog: * tree.h (build_string_literal): New one-argument overloads that take tree (identifier) and const char *. * builtins.cc (fold_builtin_FILE) (fold_builtin_FUNCTION) * gimplify.cc (gimple_add_init_for_auto_var) * vtable-verify.cc (verify_bb_vtables): Simplify calls. gcc/cp/ChangeLog: * cp-gimplify.cc (fold_builtin_source_location) * vtable-class-hierarchy.cc (register_all_pairs): Simplify calls to build_string_literal. (build_string_from_id): Remove. --- gcc/tree.h | 9 ++++++++- gcc/builtins.cc | 6 +++--- gcc/cp/cp-gimplify.cc | 6 +++--- gcc/cp/vtable-class-hierarchy.cc | 20 +++----------------- gcc/gimplify.cc | 6 ++---- gcc/vtable-verify.cc | 12 ++---------- 6 files changed, 21 insertions(+), 38 deletions(-) diff --git a/gcc/tree.h b/gcc/tree.h index 9af971cf401..a50f7b2be9d 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -1135,7 +1135,7 @@ extern void omp_clause_range_check_failed (const_tree, const char *, int, /* Define fields and accessors for some special-purpose tree nodes. */ -/* As with STRING_CST, in C terms this is sizeof, not strlen. */ +/* Unlike STRING_CST, in C terms this is strlen, not sizeof. */ #define IDENTIFIER_LENGTH(NODE) \ (IDENTIFIER_NODE_CHECK (NODE)->identifier.id.len) #define IDENTIFIER_POINTER(NODE) \ @@ -4706,6 +4706,13 @@ extern tree build_alloca_call_expr (tree, unsigned int, HOST_WIDE_INT); extern tree build_string_literal (unsigned, const char * = NULL, tree = char_type_node, unsigned HOST_WIDE_INT = HOST_WIDE_INT_M1U); +inline tree build_string_literal (const char *p) +{ return build_string_literal (strlen (p) + 1, p); } +inline tree build_string_literal (tree t) +{ + return build_string_literal (IDENTIFIER_LENGTH (t) + 1, + IDENTIFIER_POINTER (t)); +} /* Construct various nodes representing data types. */ diff --git a/gcc/builtins.cc b/gcc/builtins.cc index 5f319b28030..26898d7e27e 100644 --- a/gcc/builtins.cc +++ b/gcc/builtins.cc @@ -9521,10 +9521,10 @@ fold_builtin_FILE (location_t loc) __FILE__ macro so it appears appropriate to use the same file prefix mappings. */ fname = remap_macro_filename (fname); - return build_string_literal (strlen (fname) + 1, fname); + return build_string_literal (fname); } - return build_string_literal (1, ""); + return build_string_literal (""); } /* Fold a call to __builtin_FUNCTION to a constant string. */ @@ -9537,7 +9537,7 @@ fold_builtin_FUNCTION () if (current_function_decl) name = lang_hooks.decl_printable_name (current_function_decl, 0); - return build_string_literal (strlen (name) + 1, name); + return build_string_literal (name); } /* Fold a call to __builtin_LINE to an integer constant. */ diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index 28c339869b8..cc8bfada5af 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -3378,10 +3378,10 @@ fold_builtin_source_location (location_t loc) if (const char *fname = LOCATION_FILE (loc)) { fname = remap_macro_filename (fname); - val = build_string_literal (strlen (fname) + 1, fname); + val = build_string_literal (fname); } else - val = build_string_literal (1, ""); + val = build_string_literal (""); } else if (strcmp (n, "_M_function_name") == 0) { @@ -3390,7 +3390,7 @@ fold_builtin_source_location (location_t loc) if (current_function_decl) name = cxx_printable_name (current_function_decl, 2); - val = build_string_literal (strlen (name) + 1, name); + val = build_string_literal (name); } else if (strcmp (n, "_M_line") == 0) val = build_int_cst (TREE_TYPE (field), LOCATION_LINE (loc)); diff --git a/gcc/cp/vtable-class-hierarchy.cc b/gcc/cp/vtable-class-hierarchy.cc index cc1df1ebdb2..1e180ea61dc 100644 --- a/gcc/cp/vtable-class-hierarchy.cc +++ b/gcc/cp/vtable-class-hierarchy.cc @@ -467,19 +467,6 @@ check_and_record_registered_pairs (tree vtable_decl, tree vptr_address, return !inserted_something; } -/* Given an IDENTIFIER_NODE, build and return a string literal based on it. */ - -static tree -build_string_from_id (tree identifier) -{ - int len; - - gcc_assert (TREE_CODE (identifier) == IDENTIFIER_NODE); - - len = IDENTIFIER_LENGTH (identifier); - return build_string_literal (len + 1, IDENTIFIER_POINTER (identifier)); -} - /* A class may contain secondary vtables in it, for various reasons. This function goes through the decl chain of a class record looking for any fields that point to secondary vtables, and adding calls to @@ -920,7 +907,7 @@ register_all_pairs (tree body) if (flag_vtv_debug) - str1 = build_string_from_id (DECL_NAME (base_ptr_var_decl)); + str1 = build_string_literal (DECL_NAME (base_ptr_var_decl)); new_type = build_pointer_type (TREE_TYPE (base_ptr_var_decl)); arg1 = build1 (ADDR_EXPR, new_type, base_ptr_var_decl); @@ -953,7 +940,7 @@ register_all_pairs (tree body) if (vtable_decl) { vtable_should_be_output = TREE_ASM_WRITTEN (vtable_decl); - str2 = build_string_from_id (DECL_NAME (vtable_decl)); + str2 = build_string_literal (DECL_NAME (vtable_decl)); } if (vtable_decl && vtable_should_be_output) @@ -1009,8 +996,7 @@ register_all_pairs (tree body) arg2 = build_key_buffer_arg (base_ptr_var_decl); if (str2 == NULL_TREE) - str2 = build_string_literal (strlen ("unknown") + 1, - "unknown"); + str2 = build_string_literal ("unknown"); if (flag_vtv_debug) output_set_info (current->class_info->class_type, diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc index 42a996dfeb9..69bad340d2e 100644 --- a/gcc/gimplify.cc +++ b/gcc/gimplify.cc @@ -1771,14 +1771,12 @@ gimple_add_init_for_auto_var (tree decl, tree decl_name = NULL_TREE; if (DECL_NAME (decl)) - decl_name = build_string_literal (IDENTIFIER_LENGTH (DECL_NAME (decl)) + 1, - IDENTIFIER_POINTER (DECL_NAME (decl))); + decl_name = build_string_literal (DECL_NAME (decl)); else { char *decl_name_anonymous = xasprintf ("D.%u", DECL_UID (decl)); - decl_name = build_string_literal (strlen (decl_name_anonymous) + 1, - decl_name_anonymous); + decl_name = build_string_literal (decl_name_anonymous); free (decl_name_anonymous); } diff --git a/gcc/vtable-verify.cc b/gcc/vtable-verify.cc index 24894e7f108..f01058e5412 100644 --- a/gcc/vtable-verify.cc +++ b/gcc/vtable-verify.cc @@ -725,10 +725,6 @@ verify_bb_vtables (basic_block bb) trace information to debug problems. */ if (flag_vtv_debug) { - int len1 = IDENTIFIER_LENGTH - (DECL_NAME (vtbl_var_decl)); - int len2 = strlen (vtable_name); - call_stmt = gimple_build_call (verify_vtbl_ptr_fndecl, 4, build1 (ADDR_EXPR, @@ -737,12 +733,8 @@ verify_bb_vtables (basic_block bb) vtbl_var_decl), lhs, build_string_literal - (len1 + 1, - IDENTIFIER_POINTER - (DECL_NAME - (vtbl_var_decl))), - build_string_literal (len2 + 1, - vtable_name)); + (DECL_NAME (vtbl_var_decl)), + build_string_literal (vtable_name)); } else call_stmt = gimple_build_call base-commit: 25861cf3a88a07c8dca3fb32d098c0ad756bbe38 -- 2.31.1