From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gproxy3-pub.mail.unifiedlayer.com (gproxy3-pub.mail.unifiedlayer.com [69.89.30.42]) by sourceware.org (Postfix) with ESMTPS id 86E5C3858D32 for ; Fri, 7 Apr 2023 18:09:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 86E5C3858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw11.mail.unifiedlayer.com (unknown [10.0.90.126]) by progateway5.mail.pro1.eigbox.com (Postfix) with ESMTP id C7ADD100482A2 for ; Fri, 7 Apr 2023 18:09:17 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id kqWTpKprD2LM4kqWTpfIxQ; Fri, 07 Apr 2023 18:09:17 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=KbQXDSUD c=1 sm=1 tr=0 ts=64305c4d a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=dKHAf1wccvYA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=mDV3o1hIAAAA:8 a=Mv6Kwy-Yx36Vh7zzx7YA:9 a=_FVE-zBwftR9WsbkzFJk:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=433Xc2XtXn79h9k8OvrfiMLN5VkKXzIjcIOxe6GfoHI=; b=TV62/jvg0BVzV6DAMJbcHzZB3A ViJkkFzGRPgbK3Bmc+OrFrZv+ACkb597E5eye9eRls+SMrWvjRKJrogozrTpL1VZCQTXdf+gGfg88 2s0kPi3LCK432do36bck7ZOlC; Received: from 75-166-159-36.hlrn.qwest.net ([75.166.159.36]:38088 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pkqWT-003pIb-DL; Fri, 07 Apr 2023 12:09:17 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2] Simplify auto_load_expand_dir_vars and remove substitute_path_component Date: Fri, 7 Apr 2023 12:08:56 -0600 Message-Id: <20230407180856.3403239-1-tom@tromey.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.159.36 X-Source-L: No X-Exim-ID: 1pkqWT-003pIb-DL X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-159-36.hlrn.qwest.net (localhost.localdomain) [75.166.159.36]:38088 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3026.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,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: This simplifies auto_load_expand_dir_vars to first split the string, then do any needed substitutions. This was suggested by Simon, and is much simpler than the current approach. Then this patch also removes substitute_path_component, as it is no longer called. This is nice because it helps with the long term goal of removing utils.h. Regression tested on x86-64 Fedora 36. --- gdb/Makefile.in | 1 - gdb/auto-load.c | 33 +++++++++++------- gdb/unittests/utils-selftests.c | 60 --------------------------------- gdb/utils.c | 45 ------------------------- gdb/utils.h | 3 -- 5 files changed, 21 insertions(+), 121 deletions(-) delete mode 100644 gdb/unittests/utils-selftests.c diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 40497541880..3129c1f798d 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -489,7 +489,6 @@ SELFTESTS_SRCS = \ unittests/ui-file-selftests.c \ unittests/unique_xmalloc_ptr_char.c \ unittests/unpack-selftests.c \ - unittests/utils-selftests.c \ unittests/vec-utils-selftests.c \ unittests/xml-utils-selftests.c diff --git a/gdb/auto-load.c b/gdb/auto-load.c index 40b05fdc634..d6c199d7ede 100644 --- a/gdb/auto-load.c +++ b/gdb/auto-load.c @@ -173,24 +173,33 @@ static std::string auto_load_safe_path = AUTO_LOAD_SAFE_PATH; counterpart. */ static std::vector> auto_load_safe_path_vec; -/* Expand $datadir and $debugdir in STRING according to the rules of - substitute_path_component. */ +/* Expand $datadir and $debugdir in STRING. */ static std::vector> auto_load_expand_dir_vars (const char *string) { - char *s = xstrdup (string); - substitute_path_component (&s, "$datadir", gdb_datadir.c_str ()); - substitute_path_component (&s, "$debugdir", debug_file_directory.c_str ()); + std::vector> result + = dirnames_to_char_ptr_vec (string); - if (debug_auto_load && strcmp (s, string) != 0) - auto_load_debug_printf ("Expanded $-variables to \"%s\".", s); - - std::vector> dir_vec - = dirnames_to_char_ptr_vec (s); - xfree(s); + for (auto &elt : result) + { + if (strcmp (elt.get (), "$datadir") == 0) + { + elt = make_unique_xstrdup (gdb_datadir.c_str ()); + if (debug_auto_load) + auto_load_debug_printf ("Expanded $datadir to \"%s\".", + gdb_datadir.c_str ()); + } + else if (strcmp (elt.get (), "$debugdir") == 0) + { + elt = make_unique_xstrdup (debug_file_directory.c_str ()); + if (debug_auto_load) + auto_load_debug_printf ("Expanded $debugdir to \"%s\".", + debug_file_directory.c_str ()); + } + } - return dir_vec; + return result; } /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */ diff --git a/gdb/unittests/utils-selftests.c b/gdb/unittests/utils-selftests.c deleted file mode 100644 index 70609aa4294..00000000000 --- a/gdb/unittests/utils-selftests.c +++ /dev/null @@ -1,60 +0,0 @@ -/* Unit tests for the utils.c file. - - Copyright (C) 2018-2023 Free Software Foundation, Inc. - - This file is part of GDB. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . */ - -#include "defs.h" -#include "utils.h" -#include "gdbsupport/selftest.h" - -namespace selftests { -namespace utils { - -static void -test_substitute_path_component () -{ - auto test = [] (std::string s, const char *from, const char *to, - const char *expected) - { - char *temp = xstrdup (s.c_str ()); - substitute_path_component (&temp, from, to); - SELF_CHECK (strcmp (temp, expected) == 0); - xfree (temp); - }; - - test ("/abc/$def/g", "abc", "xyz", "/xyz/$def/g"); - test ("abc/$def/g", "abc", "xyz", "xyz/$def/g"); - test ("/abc/$def/g", "$def", "xyz", "/abc/xyz/g"); - test ("/abc/$def/g", "g", "xyz", "/abc/$def/xyz"); - test ("/abc/$def/g", "ab", "xyz", "/abc/$def/g"); - test ("/abc/$def/g", "def", "xyz", "/abc/$def/g"); - test ("/abc/$def/g", "abc", "abc", "/abc/$def/g"); - test ("/abc/$def/g", "abc", "", "//$def/g"); - test ("/abc/$def/g", "abc/$def", "xyz", "/xyz/g"); - test ("/abc/$def/abc", "abc", "xyz", "/xyz/$def/xyz"); -} - -} -} - -void _initialize_utils_selftests (); -void -_initialize_utils_selftests () -{ - selftests::register_test ("substitute_path_component", - selftests::utils::test_substitute_path_component); -} diff --git a/gdb/utils.c b/gdb/utils.c index 6ec1cc0d48d..e9e5fd812e9 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3263,51 +3263,6 @@ parse_pid_to_attach (const char *args) return pid; } -/* Substitute all occurrences of string FROM by string TO in *STRINGP. *STRINGP - must come from xrealloc-compatible allocator and it may be updated. FROM - needs to be delimited by IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be - located at the start or end of *STRINGP. */ - -void -substitute_path_component (char **stringp, const char *from, const char *to) -{ - char *string = *stringp, *s; - const size_t from_len = strlen (from); - const size_t to_len = strlen (to); - - for (s = string;;) - { - s = strstr (s, from); - if (s == NULL) - break; - - if ((s == string || IS_DIR_SEPARATOR (s[-1]) - || s[-1] == DIRNAME_SEPARATOR) - && (s[from_len] == '\0' || IS_DIR_SEPARATOR (s[from_len]) - || s[from_len] == DIRNAME_SEPARATOR)) - { - char *string_new; - - string_new - = (char *) xrealloc (string, (strlen (string) + to_len + 1)); - - /* Relocate the current S pointer. */ - s = s - string + string_new; - string = string_new; - - /* Replace from by to. */ - memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1); - memcpy (s, to, to_len); - - s += to_len; - } - else - s++; - } - - *stringp = string; -} - #ifdef HAVE_WAITPID #ifdef SIGALRM diff --git a/gdb/utils.h b/gdb/utils.h index a383036bcfe..a252432e20f 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -136,9 +136,6 @@ struct set_batch_flag_and_restore_page_info extern int gdb_filename_fnmatch (const char *pattern, const char *string, int flags); -extern void substitute_path_component (char **stringp, const char *from, - const char *to); - std::string ldirname (const char *filename); extern int count_path_elements (const char *path); -- 2.39.2