public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [RFC 4/5] [gdb/contrib] Add refactor_gdb_exception.py
Date: Tue, 25 Oct 2022 13:19:44 +0200	[thread overview]
Message-ID: <20221025111945.23886-5-tdevries@suse.de> (raw)
In-Reply-To: <20221025111945.23886-1-tdevries@suse.de>

Add a transformation script refactor_gdb_exception.py, to be used with
gdb/contrib/refactor.py that rewrites:
...
  catch (const gdb_exception/gdb_exception_error &VAR)
    {
      BODY;
    }
...
using respectively CATCH_ERROR_QUIT and CATCH_ERROR.
---
 gdb/contrib/refactor_gdb_exception.py | 68 +++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 gdb/contrib/refactor_gdb_exception.py

diff --git a/gdb/contrib/refactor_gdb_exception.py b/gdb/contrib/refactor_gdb_exception.py
new file mode 100644
index 00000000000..573ec90e996
--- /dev/null
+++ b/gdb/contrib/refactor_gdb_exception.py
@@ -0,0 +1,68 @@
+#! /usr/bin/env python3
+
+# Copyright (C) 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/>.
+
+# Implement a refactoring transformation to handling exception catching
+# using CATCH_ERROR_QUIT and CATCH_ERROR.
+
+import re
+
+pattern_lines = [
+    #   \1                    \2                          \3
+    r"^([ \t]*)catch \(const (gdb_exception(?:_error)?) &([a-zA-Z0-9]+)\)",
+    #  \4
+    r"([ \t]*){(?:}|",
+    #  \5
+    r"(.*?)^\4})"
+]
+pattern = "\n".join(pattern_lines)
+
+def repl(matchobj):
+    global have_match
+    have_match = True
+
+    g1 = matchobj.group(1)
+    exception = matchobj.group(2)
+    var = matchobj.group(3)
+    g4 = matchobj.group(4)
+    body = matchobj.group(5)
+
+    if exception == "gdb_exception":
+        m = "CATCH_ERROR_QUIT"
+    else:
+        m = "CATCH_ERROR"
+
+    if body == None or body == "":
+        return r"%s%s (%s, {})" % (g1, m, var)
+
+    body = re.sub(r"\n$", r"", body)
+
+    # Apply recursively.
+    body = transform(body)
+
+    repl_lines = [
+        r"%s%s (%s, {" % (g1, m, var),
+        r"%s" % body,
+        r"%s})" % g4
+    ]
+    repl= "\n".join(repl_lines)
+
+    return repl
+
+def transform(data):
+    return re.sub(pattern, repl, data, 0, re.MULTILINE | re.DOTALL)
-- 
2.35.3


  parent reply	other threads:[~2022-10-25 11:19 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-25 11:19 [RFC 0/5] [gdb] Catch rethrow exception slicing Tom de Vries
2022-10-25 11:19 ` [RFC 1/5] [gdb] Catch gdb_exception_quit in munmap_list::~munmap_list Tom de Vries
2022-10-25 11:19 ` [RFC 2/5] [gdbsupport] Add CATCH_ERROR and CATCH_ERROR_QUIT Tom de Vries
2022-10-25 11:19 ` [RFC 3/5] [gdb/contrib] Add refactor.py Tom de Vries
2022-10-25 11:19 ` Tom de Vries [this message]
2022-10-25 11:19 ` [RFC 5/5] [gdb] Do refactor_gdb_exception refactoring Tom de Vries

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=20221025111945.23886-5-tdevries@suse.de \
    --to=tdevries@suse.de \
    --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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).