From patchwork Tue Jan 29 05:03:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omair Javaid X-Patchwork-Id: 156928 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp4230627jaa; Mon, 28 Jan 2019 21:03:58 -0800 (PST) X-Google-Smtp-Source: ALg8bN5F+QrF4MM2z9JwcNKOiXfwVdlbUYgQ2ryipRJtiZ93TfJfQzF80a42IsHkHzqCoj36Hunc X-Received: by 2002:a62:d448:: with SMTP id u8mr24599449pfl.105.1548738238072; Mon, 28 Jan 2019 21:03:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548738238; cv=none; d=google.com; s=arc-20160816; b=GTyhMdUmUMqGnebRF0o/9FpV1eejylpd8nNgQ8ZBzfJSL9BCVPY5iWZq3zmoRxn2Bg D9hTbTWGWGjbN9V3O0ER3GAblb3iPdGcLNmgNYhjVHKnlHci1g3z/svYhGC+Q4AiSxE0 2QmTM38980N3259JyI5HdFphSjT+yMrM01sfF0M3LrHeLAiRSvMSJTSK/yuvPxD0t2gP mEbBrRJexvsEGU1t1OaLpfwjPSbi9koUj8w9NKGbGZ1Z+9e8BfGabE7J//ht5r3Rl49y U/hRNcoH+IhXAzM4J58AT5lceFQdx57AoEY4pzxTiljcz9ZL9FM6MSgfT6qUnRad99B/ pxAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:delivered-to:sender:list-help:list-post:list-archive :list-subscribe:list-unsubscribe:list-id:precedence:mailing-list :dkim-signature:domainkey-signature; bh=X3HK9wuxLC2qs3CH7a1zBhIi36VO/RJUfP15R6t22QQ=; b=KEj+Ffb+/zyZpIFJIcO25Na9ok0CQv5WMvD760G8MXKS9hhD5CNxs2zKlTWvXLFftA Jhaqty8lr6RoSWczoNXkJfo6Ui5KsJukfUxKFVZtbqba5GE6DIMxd9Bs8QEe8UKKg7ve y3WII8i26WU+UKfworqA16zKpebZe3Yp+9lKrnsSNA/kCN8WuwneX04YbttgolqyHxwe 5SptLBOEh0dWHrA1e6c6pKozRgLd7vJ21ltOk/z7TLwzpmjlivbbTGO3iild11Qbyysu 6pU6e6qXQRLx9BxSGNI9BpIYHsx6t4mKcSUf22VXgj6F0FpDRje8NEcWezQs0TGDrRsb ALqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=sP4UOJWS; dkim=pass header.i=@linaro.org header.s=google header.b=YQfq3MlE; spf=pass (google.com: domain of gdb-patches-return-153707-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="gdb-patches-return-153707-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id d32si32616408pla.136.2019.01.28.21.03.57 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Jan 2019 21:03:58 -0800 (PST) Received-SPF: pass (google.com: domain of gdb-patches-return-153707-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=sP4UOJWS; dkim=pass header.i=@linaro.org header.s=google header.b=YQfq3MlE; spf=pass (google.com: domain of gdb-patches-return-153707-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="gdb-patches-return-153707-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=bZtXqBxNv2kICg4uqGynlutKO1XCjLY aOQ2VFhCTLIkUig5YueVX2OnmLzDuU4y2xah0M/77gG2S42gpg8Gk7AcCOV9jAbA +GVD+vWsnNI2qkJzxFC0X92BQom8tyuqGV5uMCbKwX1HkZcSxCpkr5PlXTvVas2H /1W+sFs+nkPE= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=xeRaoene6so6YbBKKMPjR1nkd3k=; b=sP4UO JWSZs3ZjskIMBjfGroj4aKAFgZnlVVthoyn1ISnRAjPKlWKQuNZIu+1EYJXfRM9c 6Ul8Nv/U4DHirtP6JE93porxwwpN8i8JB3dAAuyi9jTfH3cg3yZja9DZ6VAMVdNF pg4ujoZZ564bbYV1MhpvSYcZEOXToKtSJM9EDA= Received: (qmail 35989 invoked by alias); 29 Jan 2019 05:03:48 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 35931 invoked by uid 89); 29 Jan 2019 05:03:47 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-wm1-f66.google.com Received: from mail-wm1-f66.google.com (HELO mail-wm1-f66.google.com) (209.85.128.66) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 29 Jan 2019 05:03:45 +0000 Received: by mail-wm1-f66.google.com with SMTP id a62so16336318wmh.4 for ; Mon, 28 Jan 2019 21:03:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=X3HK9wuxLC2qs3CH7a1zBhIi36VO/RJUfP15R6t22QQ=; b=YQfq3MlE1CJZg5bulnmqwnk9Yd5W6l66hF1esoywHlyNf7vdcRNBL9F01zvNj8baLB 4JOGl1EpKH5RTZUB25TbKjkVU1Ecn21tEhE+7xQbYLIYFFTgBrvOqNvgqwIiJS6udhkt iZn4i6jZL/pY9bZzaP0r6evRXRiF4tRNAvnk0= Return-Path: Received: from localhost.localdomain ([43.251.253.48]) by smtp.gmail.com with ESMTPSA id s1sm170325615wro.9.2019.01.28.21.03.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 Jan 2019 21:03:42 -0800 (PST) From: Omair Javaid To: gdb-patches@sourceware.org Cc: palves@redhat.com, prudo@linux.ibm.com, arnez@linux.vnet.ibm.com, peter.griffin@linaro.org, Ulrich.Weigand@de.ibm.com, kieran@linuxembedded.co.uk Subject: [RFC PATCH 1/6] Convert substitute_path_component to C++ Date: Tue, 29 Jan 2019 10:03:14 +0500 Message-Id: <1548738199-9403-2-git-send-email-omair.javaid@linaro.org> In-Reply-To: <1548738199-9403-1-git-send-email-omair.javaid@linaro.org> References: <1548738199-9403-1-git-send-email-omair.javaid@linaro.org> X-IsSubscribed: yes From: Philipp Rudo Simplify the code of utils.c:substiute_path_component by converting it to C++. gdb/ChangeLog: * utils.c (substitute_path_component): Convert to C++. * utils.h (substitute_path_componetn): Adjust declatation. * auto-load.c (auto_load_expand_dir_vars): Adjust. * unittests/utils-selftests.c (test_substitute_path_component): Adjust. --- gdb/auto-load.c | 19 +++++++---------- gdb/unittests/utils-selftests.c | 10 ++++----- gdb/utils.c | 47 ++++++++++------------------------------- gdb/utils.h | 10 +++++++-- 4 files changed, 31 insertions(+), 55 deletions(-) -- 2.7.4 diff --git a/gdb/auto-load.c b/gdb/auto-load.c index 00869fe..a3507bd 100644 --- a/gdb/auto-load.c +++ b/gdb/auto-load.c @@ -175,21 +175,18 @@ std::vector> auto_load_safe_path_vec; substitute_path_component. */ static std::vector> -auto_load_expand_dir_vars (const char *string) +auto_load_expand_dir_vars (const std::string &string) { - char *s = xstrdup (string); - substitute_path_component (&s, "$datadir", gdb_datadir); - substitute_path_component (&s, "$debugdir", debug_file_directory); + std::string s (string); + substitute_path_component (s, "$datadir", gdb_datadir); + substitute_path_component (s, "$debugdir", debug_file_directory); - if (debug_auto_load && strcmp (s, string) != 0) + if (debug_auto_load && s.compare (string) != 0) fprintf_unfiltered (gdb_stdlog, - _("auto-load: Expanded $-variables to \"%s\".\n"), s); + _("auto-load: Expanded $-variables to \"%s\".\n"), + s.c_str ()); - std::vector> dir_vec - = dirnames_to_char_ptr_vec (s); - xfree(s); - - return dir_vec; + return dirnames_to_char_ptr_vec (s.c_str ()); } /* 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 index c874de3..4d87162 100644 --- a/gdb/unittests/utils-selftests.c +++ b/gdb/unittests/utils-selftests.c @@ -27,13 +27,11 @@ namespace utils { static void test_substitute_path_component () { - auto test = [] (std::string s, const char *from, const char *to, - const char *expected) + auto test = [] (std::string s, const std::string from, const std::string to, + const std::string expected) { - char *temp = xstrdup (s.c_str ()); - substitute_path_component (&temp, from, to); - SELF_CHECK (strcmp (temp, expected) == 0); - xfree (temp); + substitute_path_component (s, from, to); + SELF_CHECK (s == expected); }; test ("/abc/$def/g", "abc", "xyz", "/xyz/$def/g"); diff --git a/gdb/utils.c b/gdb/utils.c index 6fb5736..2394443 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3057,49 +3057,24 @@ parse_pid_to_attach (const char *args) return pid; } -/* Substitute all occurences 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. */ +/* See utils.h. */ void -substitute_path_component (char **stringp, const char *from, const char *to) +substitute_path_component (std::string &str, const std::string &from, + const std::string &to) { - char *string = *stringp, *s; - const size_t from_len = strlen (from); - const size_t to_len = strlen (to); - - for (s = string;;) + for (size_t pos = str.find (from); pos != std::string::npos; + pos = str.find (from, pos + to.length ())) { - 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 start = pos == 0 ? str[0] : str[pos - 1]; + char end = str[pos + from.length ()]; + if ((pos == 0 || IS_DIR_SEPARATOR (start) || start == DIRNAME_SEPARATOR) + && (end == '\0' || IS_DIR_SEPARATOR (end) + || end == 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; + str.replace (pos, from.length (), to); } - else - s++; } - - *stringp = string; } #ifdef HAVE_WAITPID diff --git a/gdb/utils.h b/gdb/utils.h index 896feb9..bc319de 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -292,8 +292,14 @@ private: 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); +/* Substitute all occurences of string FROM by string TO in STR. STR + 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 STR). */ + +extern void substitute_path_component (std::string &str, + const std::string &from, + const std::string &to); std::string ldirname (const char *filename);