[1/4] Move gdb_argv to gdbsupport

Message ID 20220114195705.2311967-2-tom@tromey.com
State New
Headers show
Series
  • Move some generic code to gdbsupport
Related show

Commit Message

Tom Tromey Jan. 14, 2022, 7:57 p.m.
This moves the gdb_argv class to a new header in gdbsupport.
---
 gdb/cli/cli-decode.h   |   1 +
 gdb/compile/compile.c  |   1 +
 gdb/exec.c             |   1 +
 gdb/guile/scm-string.c |   1 +
 gdb/inferior.c         |   1 +
 gdb/infrun.c           |   1 +
 gdb/interps.c          |   1 +
 gdb/psymtab.c          |   1 +
 gdb/skip.c             |   1 +
 gdb/source.c           |   1 +
 gdb/stack.c            |   1 +
 gdb/symfile.c          |   1 +
 gdb/symmisc.c          |   1 +
 gdb/tracefile.c        |   1 +
 gdb/tracepoint.c       |   1 +
 gdb/utils.c            |  12 +--
 gdb/utils.h            | 174 -----------------------------------
 gdbsupport/buildargv.h | 204 +++++++++++++++++++++++++++++++++++++++++
 18 files changed, 220 insertions(+), 185 deletions(-)
 create mode 100644 gdbsupport/buildargv.h

-- 
2.31.1

Patch

diff --git a/gdb/cli/cli-decode.h b/gdb/cli/cli-decode.h
index 8f367bf4634..ed75d3dfc15 100644
--- a/gdb/cli/cli-decode.h
+++ b/gdb/cli/cli-decode.h
@@ -27,6 +27,7 @@ 
 #include "cli-script.h"
 #include "completer.h"
 #include "gdbsupport/intrusive_list.h"
+#include "gdbsupport/buildargv.h"
 
 /* Not a set/show command.  Note that some commands which begin with
    "set" or "show" might be in this category, if their syntax does
diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
index 3b76e102257..6794c93d60e 100644
--- a/gdb/compile/compile.c
+++ b/gdb/compile/compile.c
@@ -44,6 +44,7 @@ 
 #include "gdbsupport/gdb_unlinker.h"
 #include "gdbsupport/pathstuff.h"
 #include "gdbsupport/scoped_ignore_signal.h"
+#include "gdbsupport/buildargv.h"
 
 
 
diff --git a/gdb/exec.c b/gdb/exec.c
index f42862b2159..d9854d50f78 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -49,6 +49,7 @@ 
 #include <algorithm>
 #include "gdbsupport/pathstuff.h"
 #include "cli/cli-style.h"
+#include "gdbsupport/buildargv.h"
 
 void (*deprecated_file_changed_hook) (const char *);
 
diff --git a/gdb/guile/scm-string.c b/gdb/guile/scm-string.c
index 184eaaded07..bf558922db6 100644
--- a/gdb/guile/scm-string.c
+++ b/gdb/guile/scm-string.c
@@ -23,6 +23,7 @@ 
 #include "defs.h"
 #include "charset.h"
 #include "guile-internal.h"
+#include "gdbsupport/buildargv.h"
 
 /* Convert STRING to an int.
    STRING must be a valid integer.  */
diff --git a/gdb/inferior.c b/gdb/inferior.c
index c7a6ae87cf5..bebddb44173 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -35,6 +35,7 @@ 
 #include "target-descriptions.h"
 #include "readline/tilde.h"
 #include "progspace-and-thread.h"
+#include "gdbsupport/buildargv.h"
 
 /* Keep a registry of per-inferior data-pointers required by other GDB
    modules.  */
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 793d83a17a6..0ea5483edc5 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -72,6 +72,7 @@ 
 #include "scoped-mock-context.h"
 #include "test-target.h"
 #include "gdbsupport/common-debug.h"
+#include "gdbsupport/buildargv.h"
 
 /* Prototypes for local functions */
 
diff --git a/gdb/interps.c b/gdb/interps.c
index d65698f0cb4..a475d8790c9 100644
--- a/gdb/interps.c
+++ b/gdb/interps.c
@@ -38,6 +38,7 @@ 
 #include "completer.h"
 #include "top.h"		/* For command_loop.  */
 #include "main.h"
+#include "gdbsupport/buildargv.h"
 
 /* Each UI has its own independent set of interpreters.  */
 
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index 7e2da485ba5..9db8a7aeba2 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -36,6 +36,7 @@ 
 #include "gdbcmd.h"
 #include <algorithm>
 #include <set>
+#include "gdbsupport/buildargv.h"
 
 static struct partial_symbol *lookup_partial_symbol (struct objfile *,
 						     struct partial_symtab *,
diff --git a/gdb/skip.c b/gdb/skip.c
index 2525b8b60db..3308252cb47 100644
--- a/gdb/skip.c
+++ b/gdb/skip.c
@@ -37,6 +37,7 @@ 
 #include "gdbsupport/gdb_optional.h"
 #include <list>
 #include "cli/cli-style.h"
+#include "gdbsupport/buildargv.h"
 
 /* True if we want to print debug printouts related to file/function
    skipping. */
diff --git a/gdb/source.c b/gdb/source.c
index 6bf4a4e881e..ab5cfc5a9d9 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -50,6 +50,7 @@ 
 #include "observable.h"
 #include "build-id.h"
 #include "debuginfod-support.h"
+#include "gdbsupport/buildargv.h"
 
 #define OPEN_MODE (O_RDONLY | O_BINARY)
 #define FDOPEN_MODE FOPEN_RB
diff --git a/gdb/stack.c b/gdb/stack.c
index 8239c76de20..b81be2962d4 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -55,6 +55,7 @@ 
 #include "gdbsupport/def-vector.h"
 #include "cli/cli-option.h"
 #include "cli/cli-style.h"
+#include "gdbsupport/buildargv.h"
 
 /* The possible choices of "set print frame-arguments", and the value
    of this setting.  */
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 0449b919974..37ce01bb0e6 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -61,6 +61,7 @@ 
 #include "gdbsupport/selftest.h"
 #include "cli/cli-style.h"
 #include "gdbsupport/forward-scope-exit.h"
+#include "gdbsupport/buildargv.h"
 
 #include <sys/types.h>
 #include <fcntl.h>
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index ca803fc5342..4cbe338cb84 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -38,6 +38,7 @@ 
 #include "source.h"
 #include "readline/tilde.h"
 #include <cli/cli-style.h>
+#include "gdbsupport/buildargv.h"
 
 /* Prototypes for local functions */
 
diff --git a/gdb/tracefile.c b/gdb/tracefile.c
index 50510af9805..f7d9331e5b7 100644
--- a/gdb/tracefile.c
+++ b/gdb/tracefile.c
@@ -24,6 +24,7 @@ 
 #include "regcache.h"
 #include "gdbsupport/byte-vector.h"
 #include "gdbarch.h"
+#include "gdbsupport/buildargv.h"
 
 /* Helper macros.  */
 
diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index dc5e7f141cf..33f5c7f59fe 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -58,6 +58,7 @@ 
 #include <algorithm>
 #include "cli/cli-style.h"
 #include "expop.h"
+#include "gdbsupport/buildargv.h"
 
 #include <unistd.h>
 
diff --git a/gdb/utils.c b/gdb/utils.c
index 3ee2b5444df..da60c1e4144 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -76,6 +76,7 @@ 
 #include "cli-out.h"
 #include "gdbsupport/gdb-safe-ctype.h"
 #include "bt-utils.h"
+#include "gdbsupport/buildargv.h"
 
 void (*deprecated_error_begin_hook) (void);
 
@@ -2890,17 +2891,6 @@  ldirname (const char *filename)
   return dirname;
 }
 
-/* See utils.h.  */
-
-void
-gdb_argv::reset (const char *s)
-{
-  char **argv = buildargv (s);
-
-  freeargv (m_argv);
-  m_argv = argv;
-}
-
 /* Return ARGS parsed as a valid pid, or throw an error.  */
 
 int
diff --git a/gdb/utils.h b/gdb/utils.h
index ac30fd5f114..5ac34eb41a9 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -120,180 +120,6 @@  extern int parse_pid_to_attach (const char *args);
 
 extern int parse_escape (struct gdbarch *, const char **);
 
-/* A wrapper for an array of char* that was allocated in the way that
-   'buildargv' does, and should be freed with 'freeargv'.  */
-
-class gdb_argv
-{
-public:
-
-  /* A constructor that initializes to NULL.  */
-
-  gdb_argv ()
-    : m_argv (NULL)
-  {
-  }
-
-  /* A constructor that calls buildargv on STR.  STR may be NULL, in
-     which case this object is initialized with a NULL array.  */
-
-  explicit gdb_argv (const char *str)
-    : m_argv (NULL)
-  {
-    reset (str);
-  }
-
-  /* A constructor that takes ownership of an existing array.  */
-
-  explicit gdb_argv (char **array)
-    : m_argv (array)
-  {
-  }
-
-  gdb_argv (const gdb_argv &) = delete;
-  gdb_argv &operator= (const gdb_argv &) = delete;
-
-  gdb_argv &operator= (gdb_argv &&other)
-  {
-    freeargv (m_argv);
-    m_argv = other.m_argv;
-    other.m_argv = nullptr;
-    return *this;
-  }
-
-  gdb_argv (gdb_argv &&other)
-  {
-    m_argv = other.m_argv;
-    other.m_argv = nullptr;
-  }
-
-  ~gdb_argv ()
-  {
-    freeargv (m_argv);
-  }
-
-  /* Call buildargv on STR, storing the result in this object.  Any
-     previous state is freed.  STR may be NULL, in which case this
-     object is reset with a NULL array.  If buildargv fails due to
-     out-of-memory, call malloc_failure.  Therefore, the value is
-     guaranteed to be non-NULL, unless the parameter itself is
-     NULL.  */
-
-  void reset (const char *str);
-
-  /* Return the underlying array.  */
-
-  char **get ()
-  {
-    return m_argv;
-  }
-
-  const char * const * get () const
-  {
-    return m_argv;
-  }
-
-  /* Return the underlying array, transferring ownership to the
-     caller.  */
-
-  ATTRIBUTE_UNUSED_RESULT char **release ()
-  {
-    char **result = m_argv;
-    m_argv = NULL;
-    return result;
-  }
-
-  /* Return the number of items in the array.  */
-
-  int count () const
-  {
-    return countargv (m_argv);
-  }
-
-  /* Index into the array.  */
-
-  char *operator[] (int arg)
-  {
-    gdb_assert (m_argv != NULL);
-    return m_argv[arg];
-  }
-
-  /* Return the arguments array as an array view.  */
-
-  gdb::array_view<char *> as_array_view ()
-  {
-    return gdb::array_view<char *> (this->get (), this->count ());
-  }
-
-  gdb::array_view<const char * const> as_array_view () const
-  {
-    return gdb::array_view<const char * const> (this->get (), this->count ());
-  }
-
-  /* Append arguments to this array.  */
-  void append (gdb_argv &&other)
-  {
-    int size = count ();
-    int argc = other.count ();
-    m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
-
-    for (int argi = 0; argi < argc; argi++)
-      {
-	/* Transfer ownership of the string.  */
-	m_argv[size++] = other.m_argv[argi];
-	/* Ensure that destruction of OTHER works correctly.  */
-	other.m_argv[argi] = nullptr;
-      }
-    m_argv[size] = nullptr;
-  }
-
-  /* Append arguments to this array.  */
-  void append (const gdb_argv &other)
-  {
-    int size = count ();
-    int argc = other.count ();
-    m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
-
-    for (int argi = 0; argi < argc; argi++)
-      m_argv[size++] = xstrdup (other.m_argv[argi]);
-    m_argv[size] = nullptr;
-  }
-
-  /* The iterator type.  */
-
-  typedef char **iterator;
-
-  /* Return an iterator pointing to the start of the array.  */
-
-  iterator begin ()
-  {
-    return m_argv;
-  }
-
-  /* Return an iterator pointing to the end of the array.  */
-
-  iterator end ()
-  {
-    return m_argv + count ();
-  }
-
-  bool operator!= (std::nullptr_t)
-  {
-    return m_argv != NULL;
-  }
-
-  bool operator== (std::nullptr_t)
-  {
-    return m_argv == NULL;
-  }
-
-private:
-
-  /* The wrapped array.  */
-
-  char **m_argv;
-};
-
 
 /* Cleanup utilities.  */
 
diff --git a/gdbsupport/buildargv.h b/gdbsupport/buildargv.h
new file mode 100644
index 00000000000..7a34d9b509d
--- /dev/null
+++ b/gdbsupport/buildargv.h
@@ -0,0 +1,204 @@ 
+/* RAII wrapper for buildargv
+
+   Copyright (C) 2021 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 <http://www.gnu.org/licenses/>.  */
+
+#ifndef GDBSUPPORT_BUILDARGV_H
+#define GDBSUPPORT_BUILDARGV_H
+
+#include "libiberty.h"
+
+/* A wrapper for an array of char* that was allocated in the way that
+   'buildargv' does, and should be freed with 'freeargv'.  */
+
+class gdb_argv
+{
+public:
+
+  /* A constructor that initializes to NULL.  */
+
+  gdb_argv ()
+    : m_argv (NULL)
+  {
+  }
+
+  /* A constructor that calls buildargv on STR.  STR may be NULL, in
+     which case this object is initialized with a NULL array.  */
+
+  explicit gdb_argv (const char *str)
+    : m_argv (NULL)
+  {
+    reset (str);
+  }
+
+  /* A constructor that takes ownership of an existing array.  */
+
+  explicit gdb_argv (char **array)
+    : m_argv (array)
+  {
+  }
+
+  gdb_argv (const gdb_argv &) = delete;
+  gdb_argv &operator= (const gdb_argv &) = delete;
+
+  gdb_argv &operator= (gdb_argv &&other)
+  {
+    freeargv (m_argv);
+    m_argv = other.m_argv;
+    other.m_argv = nullptr;
+    return *this;
+  }
+
+  gdb_argv (gdb_argv &&other)
+  {
+    m_argv = other.m_argv;
+    other.m_argv = nullptr;
+  }
+
+  ~gdb_argv ()
+  {
+    freeargv (m_argv);
+  }
+
+  /* Call buildargv on STR, storing the result in this object.  Any
+     previous state is freed.  STR may be NULL, in which case this
+     object is reset with a NULL array.  If buildargv fails due to
+     out-of-memory, call malloc_failure.  Therefore, the value is
+     guaranteed to be non-NULL, unless the parameter itself is
+     NULL.  */
+
+  void reset (const char *str)
+  {
+    char **argv = buildargv (str);
+    freeargv (m_argv);
+    m_argv = argv;
+  }
+
+  /* Return the underlying array.  */
+
+  char **get ()
+  {
+    return m_argv;
+  }
+
+  const char * const * get () const
+  {
+    return m_argv;
+  }
+
+  /* Return the underlying array, transferring ownership to the
+     caller.  */
+
+  ATTRIBUTE_UNUSED_RESULT char **release ()
+  {
+    char **result = m_argv;
+    m_argv = NULL;
+    return result;
+  }
+
+  /* Return the number of items in the array.  */
+
+  int count () const
+  {
+    return countargv (m_argv);
+  }
+
+  /* Index into the array.  */
+
+  char *operator[] (int arg)
+  {
+    gdb_assert (m_argv != NULL);
+    return m_argv[arg];
+  }
+
+  /* Return the arguments array as an array view.  */
+
+  gdb::array_view<char *> as_array_view ()
+  {
+    return gdb::array_view<char *> (this->get (), this->count ());
+  }
+
+  gdb::array_view<const char * const> as_array_view () const
+  {
+    return gdb::array_view<const char * const> (this->get (), this->count ());
+  }
+
+  /* Append arguments to this array.  */
+  void append (gdb_argv &&other)
+  {
+    int size = count ();
+    int argc = other.count ();
+    m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
+
+    for (int argi = 0; argi < argc; argi++)
+      {
+	/* Transfer ownership of the string.  */
+	m_argv[size++] = other.m_argv[argi];
+	/* Ensure that destruction of OTHER works correctly.  */
+	other.m_argv[argi] = nullptr;
+      }
+    m_argv[size] = nullptr;
+  }
+
+  /* Append arguments to this array.  */
+  void append (const gdb_argv &other)
+  {
+    int size = count ();
+    int argc = other.count ();
+    m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
+
+    for (int argi = 0; argi < argc; argi++)
+      m_argv[size++] = xstrdup (other.m_argv[argi]);
+    m_argv[size] = nullptr;
+  }
+
+  /* The iterator type.  */
+
+  typedef char **iterator;
+
+  /* Return an iterator pointing to the start of the array.  */
+
+  iterator begin ()
+  {
+    return m_argv;
+  }
+
+  /* Return an iterator pointing to the end of the array.  */
+
+  iterator end ()
+  {
+    return m_argv + count ();
+  }
+
+  bool operator!= (std::nullptr_t)
+  {
+    return m_argv != NULL;
+  }
+
+  bool operator== (std::nullptr_t)
+  {
+    return m_argv == NULL;
+  }
+
+private:
+
+  /* The wrapped array.  */
+
+  char **m_argv;
+};
+
+#endif /* GDBSUPPORT_BUILDARGV_H */