[RFC,2/5,gdbsupport] Add CATCH_ERROR and CATCH_ERROR_QUIT

Message ID 20221025111945.23886-3-tdevries@suse.de
State Dropped
Headers
Series Catch rethrow exception slicing |

Commit Message

Tom de Vries Oct. 25, 2022, 11:19 a.m. UTC
  Add convenience macros CATCH_ERROR and CATCH_ERROR_QUIT, that can be used to
rewrite catch clauses of try/catch statements.

More concretely, this:
...
  catch (const gdb_exception &VAR)
    {
      BODY;
    }
...
into:
...
  CATCH_ERROR_QUIT (VAR, {
      BODY;
    }
...
and this:
...
  catch (const gdb_exception_error &VAR)
    {
      BODY;
    }
...
into:
...
  CATCH_ERROR (VAR, {
      BODY;
    }
...
---
 gdbsupport/common-exceptions.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
  

Patch

diff --git a/gdbsupport/common-exceptions.h b/gdbsupport/common-exceptions.h
index 543afda508a..ea998652edc 100644
--- a/gdbsupport/common-exceptions.h
+++ b/gdbsupport/common-exceptions.h
@@ -294,6 +294,37 @@  struct gdb_exception_quit : public gdb_exception
   }
 };
 
+#define CATCH_ERROR_QUIT(VAR, BODY)		\
+  catch (const gdb_exception_error &VAR)	\
+    {						\
+      BODY;					\
+    }						\
+  catch (const gdb_exception_quit &VAR)		\
+    {						\
+      BODY;					\
+    }						\
+  catch (const gdb_exception &)			\
+    {						\
+      /* Exception slicing occurred.  */	\
+      gdb_assert (0);				\
+    }
+
+#define CATCH_ERROR(VAR, BODY)			\
+  catch (const gdb_exception_error &VAR)	\
+    {						\
+      BODY;					\
+    }						\
+  catch (const gdb_exception_quit &)		\
+    {						\
+      throw;					\
+    }						\
+  catch (const gdb_exception &)			\
+    {						\
+      /* Exception slicing occurred.  */	\
+      gdb_assert (0);				\
+    }
+
+
 /* An exception type that inherits from both std::bad_alloc and a gdb
    exception.  This is necessary because operator new can only throw
    std::bad_alloc, and OTOH, we want exceptions thrown due to memory