From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 1/2] Allow move of compiled_regex
Date: Fri, 24 Apr 2026 09:23:19 -0600 [thread overview]
Message-ID: <20260424-print-var-and-cleanup-v1-1-7b6a2861ccb5@adacore.com> (raw)
In-Reply-To: <20260424-print-var-and-cleanup-v1-0-7b6a2861ccb5@adacore.com>
This patch makes it possible to move a compiled_regex.
---
gdbsupport/gdb_regex.cc | 13 +++++++++++--
gdbsupport/gdb_regex.h | 30 +++++++++++++++++++++++++++++-
2 files changed, 40 insertions(+), 3 deletions(-)
diff --git a/gdbsupport/gdb_regex.cc b/gdbsupport/gdb_regex.cc
index 111e5ff24a8..6774aa4c805 100644
--- a/gdbsupport/gdb_regex.cc
+++ b/gdbsupport/gdb_regex.cc
@@ -33,17 +33,25 @@ compiled_regex::compiled_regex (const char *regex, int cflags,
regerror (code, &m_pattern, err.data (), length);
error (("%s: %s"), message, err.data ());
}
+
+ m_valid = true;
}
-compiled_regex::~compiled_regex ()
+void
+compiled_regex::clear ()
{
- regfree (&m_pattern);
+ if (m_valid)
+ {
+ regfree (&m_pattern);
+ m_valid = false;
+ }
}
int
compiled_regex::exec (const char *string, size_t nmatch,
regmatch_t pmatch[], int eflags) const
{
+ gdb_assert (m_valid);
return regexec (&m_pattern, string, nmatch, pmatch, eflags);
}
@@ -52,5 +60,6 @@ compiled_regex::search (const char *string,
int length, int start, int range,
struct re_registers *regs)
{
+ gdb_assert (m_valid);
return re_search (&m_pattern, string, length, start, range, regs);
}
diff --git a/gdbsupport/gdb_regex.h b/gdbsupport/gdb_regex.h
index 63fd9c32119..f7d3190839f 100644
--- a/gdbsupport/gdb_regex.h
+++ b/gdbsupport/gdb_regex.h
@@ -35,10 +35,32 @@ class compiled_regex
const char *message)
ATTRIBUTE_NONNULL (2) ATTRIBUTE_NONNULL (4);
- ~compiled_regex ();
+ ~compiled_regex ()
+ {
+ clear ();
+ }
DISABLE_COPY_AND_ASSIGN (compiled_regex);
+ compiled_regex (compiled_regex &&other)
+ : m_valid (other.m_valid),
+ m_pattern (other.m_pattern)
+ {
+ other.m_valid = false;
+ }
+
+ compiled_regex &operator= (compiled_regex &&other)
+ {
+ if (&other != this)
+ {
+ clear ();
+ m_valid = other.m_valid;
+ m_pattern = other.m_pattern;
+ other.m_valid = false;
+ }
+ return *this;
+ }
+
/* Wrapper around ::regexec. */
int exec (const char *string,
size_t nmatch, regmatch_t pmatch[],
@@ -50,6 +72,12 @@ class compiled_regex
int range, struct re_registers *regs);
private:
+ /* Free the compiled pattern, if necessary. */
+ void clear ();
+
+ /* True if the compiled pattern is valid. */
+ bool m_valid = false;
+
/* The compiled pattern. */
regex_t m_pattern;
};
--
2.53.0
next prev parent reply other threads:[~2026-04-24 15:24 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-24 15:23 [PATCH 0/2] C++-ify print_variable_and_value_data Tom Tromey
2026-04-24 15:23 ` Tom Tromey [this message]
2026-05-08 10:00 ` [PATCH 1/2] Allow move of compiled_regex Andrew Burgess
2026-04-24 15:23 ` [PATCH 2/2] C++-ify print_variable_and_value_data Tom Tromey
2026-05-08 12:33 ` Andrew Burgess
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260424-print-var-and-cleanup-v1-1-7b6a2861ccb5@adacore.com \
--to=tromey@adacore.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox