From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id AD1A63858D35; Fri, 5 May 2023 14:17:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD1A63858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683296271; bh=9BNqtTDt09421so1BONdQQxk5zl4lGn9OGXl99tphh8=; h=From:To:Subject:Date:From; b=jDZWJcUWb6TW7jp4FqN73PiJqWF8Vc6nIRZ2tCwrb0HyXIiuCIBbehTDoXnMtIAjy sKuJG8lWjuWWO0iVJotiKzz74AhLner7EZuYNjnyu02o1uSYnrXrkGYkXXY1inqWgT qY0bO3u9ECvh3LTL7CVpBqNSgWDt17+rh7GF1+gE= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Simplify auto_load_expand_dir_vars and remove substitute_path_component X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 4891c459927d9a9c0f516c35e2d9d4ce212dd06e X-Git-Newrev: 02601231fdd91a7bd4837ce202906ea2ce661489 Message-Id: <20230505141751.AD1A63858D35@sourceware.org> Date: Fri, 5 May 2023 14:17:51 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D02601231fdd9= 1a7bd4837ce202906ea2ce661489 commit 02601231fdd91a7bd4837ce202906ea2ce661489 Author: Tom Tromey Date: Tue Apr 4 12:50:03 2023 -0600 Simplify auto_load_expand_dir_vars and remove substitute_path_component =20 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. =20 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. =20 Regression tested on x86-64 Fedora 36. Diff: --- 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(-) diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 6af65357243..93f506ee391 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -489,7 +489,6 @@ SELFTESTS_SRCS =3D \ 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 =20 diff --git a/gdb/auto-load.c b/gdb/auto-load.c index 5267cb4e64d..9d6fabe6bbc 100644 --- a/gdb/auto-load.c +++ b/gdb/auto-load.c @@ -173,24 +173,33 @@ static std::string auto_load_safe_path =3D AUTO_LOAD_= SAFE_PATH; counterpart. */ static std::vector> auto_load_safe_path_vec; =20 -/* Expand $datadir and $debugdir in STRING according to the rules of - substitute_path_component. */ +/* Expand $datadir and $debugdir in STRING. */ =20 static std::vector> auto_load_expand_dir_vars (const char *string) { - char *s =3D xstrdup (string); - substitute_path_component (&s, "$datadir", gdb_datadir.c_str ()); - substitute_path_component (&s, "$debugdir", debug_file_directory.c_str (= )); + std::vector> result + =3D dirnames_to_char_ptr_vec (string); =20 - if (debug_auto_load && strcmp (s, string) !=3D 0) - auto_load_debug_printf ("Expanded $-variables to \"%s\".", s); - - std::vector> dir_vec - =3D dirnames_to_char_ptr_vec (s); - xfree(s); + for (auto &elt : result) + { + if (strcmp (elt.get (), "$datadir") =3D=3D 0) + { + elt =3D 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") =3D=3D 0) + { + elt =3D 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 ()); + } + } =20 - return dir_vec; + return result; } =20 /* Update auto_load_safe_path_vec from current AUTO_LOAD_SAFE_PATH. */ diff --git a/gdb/unittests/utils-selftests.c b/gdb/unittests/utils-selftest= s.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 =3D [] (std::string s, const char *from, const char *to, - const char *expected) - { - char *temp =3D xstrdup (s.c_str ()); - substitute_path_component (&temp, from, to); - SELF_CHECK (strcmp (temp, expected) =3D=3D 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 3af54ec5179..0347496e431 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3346,51 +3346,6 @@ parse_pid_to_attach (const char *args) return pid; } =20 -/* Substitute all occurrences of string FROM by string TO in *STRINGP. *S= TRINGP - must come from xrealloc-compatible allocator and it may be updated. FR= OM - 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 *t= o) -{ - char *string =3D *stringp, *s; - const size_t from_len =3D strlen (from); - const size_t to_len =3D strlen (to); - - for (s =3D string;;) - { - s =3D strstr (s, from); - if (s =3D=3D NULL) - break; - - if ((s =3D=3D string || IS_DIR_SEPARATOR (s[-1]) - || s[-1] =3D=3D DIRNAME_SEPARATOR) - && (s[from_len] =3D=3D '\0' || IS_DIR_SEPARATOR (s[from_len]) - || s[from_len] =3D=3D DIRNAME_SEPARATOR)) - { - char *string_new; - - string_new - =3D (char *) xrealloc (string, (strlen (string) + to_len + 1)); - - /* Relocate the current S pointer. */ - s =3D s - string + string_new; - string =3D string_new; - - /* Replace from by to. */ - memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1); - memcpy (s, to, to_len); - - s +=3D to_len; - } - else - s++; - } - - *stringp =3D string; -} - #ifdef HAVE_WAITPID =20 #ifdef SIGALRM diff --git a/gdb/utils.h b/gdb/utils.h index 2d831bac184..00b123e6117 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -136,9 +136,6 @@ private: extern int gdb_filename_fnmatch (const char *pattern, const char *string, int flags); =20 -extern void substitute_path_component (char **stringp, const char *from, - const char *to); - std::string ldirname (const char *filename); =20 extern int count_path_elements (const char *path);