Move gdb_regex to gdbsupport

This moves the gdb_regex convenience class to gdbsupport.
This commit is contained in:
Tom Tromey
2022-01-01 11:31:47 -07:00
parent 0589ca4e7b
commit d322d6d69d
29 changed files with 36 additions and 34 deletions

View File

@ -54,6 +54,7 @@ libgdbsupport_a_SOURCES = \
gdb-dlfcn.cc \
gdb-hashtab.cc \
gdb_obstack.cc \
gdb_regex.cc \
gdb_tilde_expand.cc \
gdb_wait.cc \
gdb_vecs.cc \

View File

@ -152,14 +152,15 @@ am_libgdbsupport_a_OBJECTS = agent.$(OBJEXT) btrace-common.$(OBJEXT) \
environ.$(OBJEXT) errors.$(OBJEXT) event-loop.$(OBJEXT) \
fileio.$(OBJEXT) filestuff.$(OBJEXT) format.$(OBJEXT) \
gdb-dlfcn.$(OBJEXT) gdb-hashtab.$(OBJEXT) \
gdb_obstack.$(OBJEXT) gdb_tilde_expand.$(OBJEXT) \
gdb_wait.$(OBJEXT) gdb_vecs.$(OBJEXT) job-control.$(OBJEXT) \
netstuff.$(OBJEXT) new-op.$(OBJEXT) pathstuff.$(OBJEXT) \
print-utils.$(OBJEXT) ptid.$(OBJEXT) rsp-low.$(OBJEXT) \
run-time-clock.$(OBJEXT) safe-strerror.$(OBJEXT) \
scoped_mmap.$(OBJEXT) search.$(OBJEXT) signals.$(OBJEXT) \
signals-state-save-restore.$(OBJEXT) tdesc.$(OBJEXT) \
thread-pool.$(OBJEXT) xml-utils.$(OBJEXT) $(am__objects_1)
gdb_obstack.$(OBJEXT) gdb_regex.$(OBJEXT) \
gdb_tilde_expand.$(OBJEXT) gdb_wait.$(OBJEXT) \
gdb_vecs.$(OBJEXT) job-control.$(OBJEXT) netstuff.$(OBJEXT) \
new-op.$(OBJEXT) pathstuff.$(OBJEXT) print-utils.$(OBJEXT) \
ptid.$(OBJEXT) rsp-low.$(OBJEXT) run-time-clock.$(OBJEXT) \
safe-strerror.$(OBJEXT) scoped_mmap.$(OBJEXT) search.$(OBJEXT) \
signals.$(OBJEXT) signals-state-save-restore.$(OBJEXT) \
tdesc.$(OBJEXT) thread-pool.$(OBJEXT) xml-utils.$(OBJEXT) \
$(am__objects_1)
libgdbsupport_a_OBJECTS = $(am_libgdbsupport_a_OBJECTS)
AM_V_P = $(am__v_P_@AM_V@)
am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
@ -379,6 +380,7 @@ libgdbsupport_a_SOURCES = \
gdb-dlfcn.cc \
gdb-hashtab.cc \
gdb_obstack.cc \
gdb_regex.cc \
gdb_tilde_expand.cc \
gdb_wait.cc \
gdb_vecs.cc \
@ -486,6 +488,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gdb-dlfcn.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gdb-hashtab.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gdb_obstack.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gdb_regex.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gdb_tilde_expand.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gdb_vecs.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gdb_wait.Po@am__quote@

57
gdbsupport/gdb_regex.cc Normal file
View File

@ -0,0 +1,57 @@
/* Copyright (C) 2011-2022 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/>. */
#include "defs.h"
#include "gdb_regex.h"
#include "gdbsupport/def-vector.h"
compiled_regex::compiled_regex (const char *regex, int cflags,
const char *message)
{
gdb_assert (regex != NULL);
gdb_assert (message != NULL);
int code = regcomp (&m_pattern, regex, cflags);
if (code != 0)
{
size_t length = regerror (code, &m_pattern, NULL, 0);
gdb::def_vector<char> err (length);
regerror (code, &m_pattern, err.data (), length);
error (("%s: %s"), message, err.data ());
}
}
compiled_regex::~compiled_regex ()
{
regfree (&m_pattern);
}
int
compiled_regex::exec (const char *string, size_t nmatch,
regmatch_t pmatch[], int eflags) const
{
return regexec (&m_pattern, string, nmatch, pmatch, eflags);
}
int
compiled_regex::search (const char *string,
int length, int start, int range,
struct re_registers *regs)
{
return re_search (&m_pattern, string, length, start, range, regs);
}

57
gdbsupport/gdb_regex.h Normal file
View File

@ -0,0 +1,57 @@
/* Portable <regex.h>.
Copyright (C) 2000-2022 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 GDB_REGEX_H
#define GDB_REGEX_H 1
# include "xregex.h"
/* A compiled regex. This is mainly a wrapper around regex_t. The
the constructor throws on regcomp error and the destructor is
responsible for calling regfree. The former means that it's not
possible to create an instance of compiled_regex that isn't
compiled, hence the name. */
class compiled_regex
{
public:
/* Compile a regexp and throw an exception on error, including
MESSAGE. REGEX and MESSAGE must not be NULL. */
compiled_regex (const char *regex, int cflags,
const char *message)
ATTRIBUTE_NONNULL (2) ATTRIBUTE_NONNULL (4);
~compiled_regex ();
DISABLE_COPY_AND_ASSIGN (compiled_regex);
/* Wrapper around ::regexec. */
int exec (const char *string,
size_t nmatch, regmatch_t pmatch[],
int eflags) const;
/* Wrapper around ::re_search. (Not const because re_search's
regex_t parameter isn't either.) */
int search (const char *string, int size, int startpos,
int range, struct re_registers *regs);
private:
/* The compiled pattern. */
regex_t m_pattern;
};
#endif /* not GDB_REGEX_H */