Some gdb_exception{,error,quit} tweaks
authorPedro Alves <palves@redhat.com>
Mon, 8 Apr 2019 12:03:54 +0000 (13:03 +0100)
committerPedro Alves <palves@redhat.com>
Mon, 8 Apr 2019 19:13:32 +0000 (20:13 +0100)
- Explicitly include <string> for std::string.

- Use std::make_shared to construct gdb_exception::message instead of
  operator new, avoiding one heap allocation (2 instead of 3).  Add
  'const char *fmt, va_list ap' parameters to
  gdb_exception{,error,quit}'s ctors, and do the std::make_shared in
  the gdb_exception ctor.

- gdb_exception_error's constructor does not need to have an 'enum
  return_reason' parameter, since it is always RETURN_ERROR, by
  definition.

- Similarly, gdb_exception_quit's contructor does not need to have
  'enum return_reason'/'enum errors' parameters.

- In the gdb_exception_{quit,_error} ctors that take a gdb_exception
  as argument, assert that they're being passed a gdb_exception object
  of the right 'reason'.

gdb/ChangeLog:
2019-04-08  Pedro Alves  <palves@redhat.com>

* common/common-exceptions.c (throw_exception): Don't create
named object to throw; throw directly.
(throw_it): Likewise.  Don't initialize gdb_exception::message
here, with new; pass FMT and AP to the ctor instead.
* common/common-exceptions.h: Include <string>.
(gdb_exception::gdb_exception(enum return_reason, enum errors,
const char *, va_list)): New ctor.  Use std::make_shared.
(gdb_exception_error::gdb_exception_error(enum return_reason, enum
errors)): Delete.
(gdb_exception_error::gdb_exception_error(enum errors, const char
*, va_list)): New.
(gdb_exception_error::gdb_exception_error(const gdb_exception &)):
Add assertion.
(gdb_exception_quit::gdb_exception_quit(enum return_reason, enum
errors)): Delete.
(gdb_exception_quit::gdb_exception_quit(const char *, va_list)): New.
(gdb_exception_quit::gdb_exception_quit(const gdb_exception &)):
Add assertion.

gdb/ChangeLog
gdb/common/common-exceptions.c
gdb/common/common-exceptions.h

index 39c380815aa0045e61e006af98d95127b5d25560..9910952aa56cd8040ec655ecab63eca698894c75 100644 (file)
@@ -1,3 +1,24 @@
+2019-04-08  Pedro Alves  <palves@redhat.com>
+
+       * common/common-exceptions.c (throw_exception): Don't create
+       named object to throw; throw directly.
+       (throw_it): Likewise.  Don't initialize gdb_exception::message
+       here, with new; pass FMT and AP to the ctor instead.
+       * common/common-exceptions.h: Include <string>.
+       (gdb_exception::gdb_exception(enum return_reason, enum errors,
+       const char *, va_list)): New ctor.  Use std::make_shared.
+       (gdb_exception_error::gdb_exception_error(enum return_reason, enum
+       errors)): Delete.
+       (gdb_exception_error::gdb_exception_error(enum errors, const char
+       *, va_list)): New.
+       (gdb_exception_error::gdb_exception_error(const gdb_exception &)):
+       Add assertion.
+       (gdb_exception_quit::gdb_exception_quit(enum return_reason, enum
+       errors)): Delete.
+       (gdb_exception_quit::gdb_exception_quit(const char *, va_list)): New.
+       (gdb_exception_quit::gdb_exception_quit(const gdb_exception &)):
+       Add assertion.
+
 2019-04-08  Tom Tromey  <tom@tromey.com>
 
        * valops.c (value_rtti_indirect_type): Replace throw_exception
index 83f2c74bfd482cd098e763818aa44801b37c9f9e..6378dc40d6d645456c4e5052adf6604f951ef273 100644 (file)
@@ -184,15 +184,9 @@ void
 throw_exception (const gdb_exception &exception)
 {
   if (exception.reason == RETURN_QUIT)
-    {
-      gdb_exception_quit ex (exception);
-      throw ex;
-    }
+    throw gdb_exception_quit (exception);
   else if (exception.reason == RETURN_ERROR)
-    {
-      gdb_exception_error ex (exception);
-      throw ex;
-    }
+    throw gdb_exception_error (exception);
   else
     gdb_assert_not_reached ("invalid return reason");
 }
@@ -202,17 +196,9 @@ throw_it (enum return_reason reason, enum errors error, const char *fmt,
          va_list ap)
 {
   if (reason == RETURN_QUIT)
-    {
-      gdb_exception_quit ex (reason, error);
-      ex.message.reset (new std::string (string_vprintf (fmt, ap)));
-      throw ex;
-    }
+    throw gdb_exception_quit (fmt, ap);
   else if (reason == RETURN_ERROR)
-    {
-      gdb_exception_error ex (reason, error);
-      ex.message.reset (new std::string (string_vprintf (fmt, ap)));
-      throw ex;
-    }
+    throw gdb_exception_error (error, fmt, ap);
   else
     gdb_assert_not_reached ("invalid return reason");
 }
index 1aedb831a626797705e409fe9acdc9b89c9369ac..3f47caec7757ef88b5b5642db40edffc5f0f0a38 100644 (file)
@@ -23,6 +23,7 @@
 #include <setjmp.h>
 #include <new>
 #include <memory>
+#include <string>
 
 /* Reasons for calling throw_exceptions().  NOTE: all reason values
    must be different from zero.  enum value 0 is reserved for internal
@@ -123,6 +124,15 @@ struct gdb_exception
   {
   }
 
+  gdb_exception (enum return_reason r, enum errors e,
+                const char *fmt, va_list ap)
+    ATTRIBUTE_PRINTF (4, 0)
+    : reason (r),
+      error (e),
+      message (std::make_shared<std::string> (string_vprintf (fmt, ap)))
+  {
+  }
+
   /* The copy constructor exists so that we can mark it "noexcept",
      which is a good practice for any sort of exception object.  */
   gdb_exception (const gdb_exception &other) noexcept
@@ -214,27 +224,31 @@ extern int exceptions_state_mc_catch (struct gdb_exception *, int);
 
 struct gdb_exception_error : public gdb_exception
 {
-  gdb_exception_error (enum return_reason r, enum errors e)
-    : gdb_exception (r, e)
+  gdb_exception_error (enum errors e, const char *fmt, va_list ap)
+    ATTRIBUTE_PRINTF (3, 0)
+    : gdb_exception (RETURN_ERROR, e, fmt, ap)
   {
   }
 
   explicit gdb_exception_error (const gdb_exception &ex) noexcept
     : gdb_exception (ex)
   {
+    gdb_assert (ex.reason == RETURN_ERROR);
   }
 };
 
 struct gdb_exception_quit : public gdb_exception
 {
-  gdb_exception_quit (enum return_reason r, enum errors e)
-    : gdb_exception (r, e)
+  gdb_exception_quit (const char *fmt, va_list ap)
+    ATTRIBUTE_PRINTF (2, 0)
+    : gdb_exception (RETURN_QUIT, GDB_NO_ERROR, fmt, ap)
   {
   }
 
   explicit gdb_exception_quit (const gdb_exception &ex) noexcept
     : gdb_exception (ex)
   {
+    gdb_assert (ex.reason == RETURN_QUIT);
   }
 };
 
This page took 0.030566 seconds and 4 git commands to generate.