Introduce and use new_breakpoint_from_type
authorSimon Marchi <simon.marchi@polymtl.ca>
Fri, 2 Jun 2017 21:16:19 +0000 (23:16 +0200)
committerSimon Marchi <simon.marchi@ericsson.com>
Fri, 2 Jun 2017 21:16:19 +0000 (23:16 +0200)
This is a small preparatory patch to factor out a snippet that appears
twice.  More kinds of breakpoints will need to be created based on
bptype, so I think it's a good idea to centralize the instantiation of
breakpoint objects.

gdb/ChangeLog:

* breakpoint.c (new_breakpoint_from_type): New function.
(create_breakpoint_sal): Use new_breakpoint_from_type and
unique_ptr.
(create_breakpoint): Likewise.

gdb/ChangeLog
gdb/breakpoint.c

index 263d57ff66087c97df38f3b8d1e9715eab5fa95f..7334ae4946117cf7b613bed2d3f7a511d73b0abb 100644 (file)
@@ -1,3 +1,10 @@
+2017-06-02  Simon Marchi  <simon.marchi@polymtl.ca>
+
+       * breakpoint.c (new_breakpoint_from_type): New function.
+       (create_breakpoint_sal): Use new_breakpoint_from_type and
+       unique_ptr.
+       (create_breakpoint): Likewise.
+
 2017-05-31  Simon Marchi  <simon.marchi@ericsson.com>
 
        * memattr.c (mem_info_command): Rename to ...
index 150b08ca9d44aecc09783e349a4acf42ef920ef7..d120290603784e516042480d0aab5a8464785245 100644 (file)
@@ -1176,6 +1176,22 @@ is_tracepoint (const struct breakpoint *b)
   return is_tracepoint_type (b->type);
 }
 
+/* Factory function to create an appropriate instance of breakpoint given
+   TYPE.  */
+
+static std::unique_ptr<breakpoint>
+new_breakpoint_from_type (bptype type)
+{
+  breakpoint *b;
+
+  if (is_tracepoint_type (type))
+    b = (breakpoint *) new tracepoint ();
+  else
+    b = new breakpoint ();
+
+  return std::unique_ptr<breakpoint> (b);
+}
+
 /* A helper function that validates that COMMANDS are valid for a
    breakpoint.  This function will throw an exception if a problem is
    found.  */
@@ -9310,22 +9326,9 @@ create_breakpoint_sal (struct gdbarch *gdbarch,
                       int enabled, int internal, unsigned flags,
                       int display_canonical)
 {
-  struct breakpoint *b;
-  struct cleanup *old_chain;
+  std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
 
-  if (is_tracepoint_type (type))
-    {
-      struct tracepoint *t;
-
-      t = new tracepoint ();
-      b = &t->base;
-    }
-  else
-    b = new breakpoint ();
-
-  old_chain = make_cleanup (xfree, b);
-
-  init_breakpoint_sal (b, gdbarch,
+  init_breakpoint_sal (b.get (), gdbarch,
                       sals, std::move (location),
                       filter, cond_string, extra_string,
                       type, disposition,
@@ -9333,9 +9336,8 @@ create_breakpoint_sal (struct gdbarch *gdbarch,
                       ops, from_tty,
                       enabled, internal, flags,
                       display_canonical);
-  discard_cleanups (old_chain);
 
-  install_breakpoint (internal, b, 0);
+  install_breakpoint (internal, b.release (), 0);
 }
 
 /* Add SALS.nelts breakpoints to the breakpoint table.  For each
@@ -9804,19 +9806,9 @@ create_breakpoint (struct gdbarch *gdbarch,
     }
   else
     {
-      struct breakpoint *b;
-
-      if (is_tracepoint_type (type_wanted))
-       {
-         struct tracepoint *t;
-
-         t = new tracepoint ();
-         b = &t->base;
-       }
-      else
-       b = new breakpoint ();
+      std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
 
-      init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
+      init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
       b->location = copy_event_location (location);
 
       if (parse_extra)
@@ -9848,7 +9840,7 @@ create_breakpoint (struct gdbarch *gdbarch,
            && type_wanted != bp_hardware_breakpoint) || thread != -1)
        b->pspace = current_program_space;
 
-      install_breakpoint (internal, b, 0);
+      install_breakpoint (internal, b.release (), 0);
     }
   
   if (VEC_length (linespec_sals, canonical.sals) > 1)