Use std::string in event_location

This changes event_location to use std::string, removing some manual
memory management, and an unnecessary string copy.
This commit is contained in:
Tom Tromey
2022-01-14 08:53:35 -07:00
parent 2b0c285ea5
commit 85e428a69f
2 changed files with 34 additions and 39 deletions

View File

@ -29,7 +29,7 @@
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
static gdb::unique_xmalloc_ptr<char> explicit_location_to_string static std::string explicit_location_to_string
(const struct explicit_location *explicit_loc); (const struct explicit_location *explicit_loc);
/* The base class for all an event locations used to set a stop event /* The base class for all an event locations used to set a stop event
@ -37,10 +37,7 @@ static gdb::unique_xmalloc_ptr<char> explicit_location_to_string
struct event_location struct event_location
{ {
virtual ~event_location () virtual ~event_location () = default;
{
xfree (as_string);
}
/* Clone this object. */ /* Clone this object. */
virtual event_location_up clone () const = 0; virtual event_location_up clone () const = 0;
@ -49,11 +46,13 @@ struct event_location
virtual bool empty_p () const = 0; virtual bool empty_p () const = 0;
/* Return a string representation of this location. */ /* Return a string representation of this location. */
const char *to_string () const char *to_string () const
{ {
if (as_string == nullptr) if (as_string.empty ())
as_string = compute_string (); as_string = compute_string ();
return as_string; if (as_string.empty ())
return nullptr;
return as_string.c_str ();
} }
DISABLE_COPY_AND_ASSIGN (event_location); DISABLE_COPY_AND_ASSIGN (event_location);
@ -61,9 +60,9 @@ struct event_location
/* The type of this breakpoint specification. */ /* The type of this breakpoint specification. */
enum event_location_type type; enum event_location_type type;
/* Cached string representation of this location. This is used, e.g., to /* Cached string representation of this location. This is used,
save stop event locations to file. Malloc'd. */ e.g., to save stop event locations to file. */
char *as_string = nullptr; mutable std::string as_string;
protected: protected:
@ -74,15 +73,13 @@ protected:
explicit event_location (const event_location *to_clone) explicit event_location (const event_location *to_clone)
: type (to_clone->type), : type (to_clone->type),
as_string (to_clone->as_string == nullptr as_string (to_clone->as_string)
? nullptr
: xstrdup (to_clone->as_string))
{ {
} }
/* Compute the string representation of this object. This is called /* Compute the string representation of this object. This is called
by to_string when needed. */ by to_string when needed. */
virtual char *compute_string () = 0; virtual std::string compute_string () const = 0;
}; };
/* A probe. */ /* A probe. */
@ -123,9 +120,9 @@ protected:
{ {
} }
char *compute_string () override std::string compute_string () const override
{ {
return xstrdup (addr_string); return addr_string;
} }
}; };
@ -176,17 +173,17 @@ protected:
linespec_location.spec_string = xstrdup (linespec_location.spec_string); linespec_location.spec_string = xstrdup (linespec_location.spec_string);
} }
char *compute_string () override std::string compute_string () const override
{ {
if (linespec_location.spec_string != nullptr) if (linespec_location.spec_string != nullptr)
{ {
struct linespec_location *ls = &linespec_location; const struct linespec_location *ls = &linespec_location;
if (ls->match_type == symbol_name_match_type::FULL) if (ls->match_type == symbol_name_match_type::FULL)
return concat ("-qualified ", ls->spec_string, (char *) nullptr); return std::string ("-qualified ") + ls->spec_string;
else else
return xstrdup (ls->spec_string); return ls->spec_string;
} }
return nullptr; return {};
} }
}; };
@ -199,7 +196,7 @@ struct event_location_address : public event_location
address (addr) address (addr)
{ {
if (addr_string != nullptr) if (addr_string != nullptr)
as_string = xstrndup (addr_string, addr_string_len); as_string = std::string (addr_string, addr_string_len);
} }
event_location_up clone () const override event_location_up clone () const override
@ -222,10 +219,10 @@ protected:
{ {
} }
char *compute_string () override std::string compute_string () const override
{ {
const char *addr_string = core_addr_to_string (address); const char *addr_string = core_addr_to_string (address);
return xstrprintf ("*%s", addr_string).release (); return std::string ("*") + addr_string;
} }
}; };
@ -268,9 +265,9 @@ protected:
copy_loc (&to_clone->explicit_loc); copy_loc (&to_clone->explicit_loc);
} }
char *compute_string () override std::string compute_string () const override
{ {
return explicit_location_to_string (&explicit_loc).release (); return explicit_location_to_string (&explicit_loc);
} }
private: private:
@ -354,7 +351,7 @@ const char *
get_address_string_location (const struct event_location *location) get_address_string_location (const struct event_location *location)
{ {
gdb_assert (location->type == ADDRESS_LOCATION); gdb_assert (location->type == ADDRESS_LOCATION);
return location->as_string; return location->to_string ();
} }
/* See description in location.h. */ /* See description in location.h. */
@ -406,7 +403,7 @@ get_explicit_location_const (const struct event_location *location)
AS_LINESPEC is true if this string should be a linespec. AS_LINESPEC is true if this string should be a linespec.
Otherwise it will be output in explicit form. */ Otherwise it will be output in explicit form. */
static gdb::unique_xmalloc_ptr<char> static std::string
explicit_to_string_internal (bool as_linespec, explicit_to_string_internal (bool as_linespec,
const struct explicit_location *explicit_loc) const struct explicit_location *explicit_loc)
{ {
@ -457,12 +454,12 @@ explicit_to_string_internal (bool as_linespec,
explicit_loc->line_offset.offset); explicit_loc->line_offset.offset);
} }
return make_unique_xstrdup (buf.c_str ()); return std::move (buf.string ());
} }
/* See description in location.h. */ /* See description in location.h. */
static gdb::unique_xmalloc_ptr<char> static std::string
explicit_location_to_string (const struct explicit_location *explicit_loc) explicit_location_to_string (const struct explicit_location *explicit_loc)
{ {
return explicit_to_string_internal (false, explicit_loc); return explicit_to_string_internal (false, explicit_loc);
@ -470,7 +467,7 @@ explicit_location_to_string (const struct explicit_location *explicit_loc)
/* See description in location.h. */ /* See description in location.h. */
gdb::unique_xmalloc_ptr<char> std::string
explicit_location_to_linespec (const struct explicit_location *explicit_loc) explicit_location_to_linespec (const struct explicit_location *explicit_loc)
{ {
return explicit_to_string_internal (true, explicit_loc); return explicit_to_string_internal (true, explicit_loc);
@ -1020,8 +1017,7 @@ event_location_empty_p (const struct event_location *location)
void void
set_event_location_string (struct event_location *location, set_event_location_string (struct event_location *location,
gdb::unique_xmalloc_ptr<char> string) std::string &&string)
{ {
xfree (location->as_string); location->as_string = std::move (string);
location->as_string = string.release ();
} }

View File

@ -112,7 +112,7 @@ extern enum event_location_type
/* Return a linespec string representation of the given explicit /* Return a linespec string representation of the given explicit
location. The location must already be canonicalized/valid. */ location. The location must already be canonicalized/valid. */
extern gdb::unique_xmalloc_ptr<char> extern std::string
explicit_location_to_linespec (const struct explicit_location *explicit_loc); explicit_location_to_linespec (const struct explicit_location *explicit_loc);
/* Return a string representation of the LOCATION. /* Return a string representation of the LOCATION.
@ -275,11 +275,10 @@ extern event_location_up
extern int event_location_empty_p (const struct event_location *location); extern int event_location_empty_p (const struct event_location *location);
/* Set the location's string representation. If STRING is NULL, clear /* Set the location's string representation. */
the string representation. */
extern void extern void
set_event_location_string (struct event_location *location, set_event_location_string (struct event_location *location,
gdb::unique_xmalloc_ptr<char> string); std::string &&string);
#endif /* LOCATION_H */ #endif /* LOCATION_H */