From patchwork Tue Aug 29 19:25:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 22405 Received: (qmail 84269 invoked by alias); 29 Aug 2017 19:25:39 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 83953 invoked by uid 89); 29 Aug 2017 19:25:38 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.2 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: gproxy9-pub.mail.unifiedlayer.com Received: from gproxy9-pub.mail.unifiedlayer.com (HELO gproxy9-pub.mail.unifiedlayer.com) (69.89.20.122) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 29 Aug 2017 19:25:34 +0000 Received: from cmgw4 (unknown [10.0.90.85]) by gproxy9.mail.unifiedlayer.com (Postfix) with ESMTP id 87F951E072A for ; Tue, 29 Aug 2017 13:25:33 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by cmgw4 with id 37RW1w00b2f2jeq017RZ0l; Tue, 29 Aug 2017 13:25:33 -0600 X-Authority-Analysis: v=2.2 cv=G8xsK5s5 c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=KeKAF7QvOSUA:10 a=zstS-IiYAAAA:8 a=Mcj-n4vna9mAaJfxAVAA:9 a=72ZSuUaD-LLKFubc:21 a=6PNy0_h6fJ9ywLu1:21 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 75-166-76-94.hlrn.qwest.net ([75.166.76.94]:36914 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1dmm8g-004DCm-BA; Tue, 29 Aug 2017 13:25:30 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 09/10] Use std::string and unique_xmalloc_ptr in compile/ code Date: Tue, 29 Aug 2017 13:25:23 -0600 Message-Id: <20170829192524.29971-10-tom@tromey.com> In-Reply-To: <20170829192524.29971-1-tom@tromey.com> References: <20170829192524.29971-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1dmm8g-004DCm-BA X-Source-Sender: 75-166-76-94.hlrn.qwest.net (bapiya.Home) [75.166.76.94]:36914 X-Source-Auth: tom+tromey.com X-Email-Count: 10 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-Local-Domain: yes Change various things in the compile/ code to use std::string or unique_xmalloc_ptr as appropriate. This allows the removal of some cleanups. ChangeLog 2017-08-29 Tom Tromey * compile/compile.c (compile_register_name_mangled): Return std::string. * compile/compile-loc2c.c (pushf_register_address): Update. (pushf_register): Update. * compile/compile-c-types.c (convert_array): Update. * compile/compile-c-symbols.c (generate_vla_size): Update. (error_symbol_once): Use a gdb::unique_xmalloc_ptr. (symbol_substitution_name): Return a gdb::unique_xmalloc_ptr. (convert_one_symbol): Update. (generate_c_for_for_one_variable): Update. * compile/compile-c-support.c (c_get_range_decl_name): Return a std::string. (generate_register_struct): Update. * compile/compile-internal.h (c_get_range_decl_name): Return a std::string. (compile_register_name_mangled): Return std::string. --- gdb/ChangeLog | 19 +++++++++++++++++++ gdb/compile/compile-c-support.c | 16 +++++++--------- gdb/compile/compile-c-symbols.c | 32 +++++++++++++------------------- gdb/compile/compile-c-types.c | 7 +++---- gdb/compile/compile-internal.h | 16 +++++++--------- gdb/compile/compile-loc2c.c | 16 +++++----------- gdb/compile/compile.c | 4 ++-- 7 files changed, 56 insertions(+), 54 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index b301516..cc679ea 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,24 @@ 2017-08-29 Tom Tromey + * compile/compile.c (compile_register_name_mangled): Return + std::string. + * compile/compile-loc2c.c (pushf_register_address): Update. + (pushf_register): Update. + * compile/compile-c-types.c (convert_array): Update. + * compile/compile-c-symbols.c (generate_vla_size): Update. + (error_symbol_once): Use a gdb::unique_xmalloc_ptr. + (symbol_substitution_name): Return a gdb::unique_xmalloc_ptr. + (convert_one_symbol): Update. + (generate_c_for_for_one_variable): Update. + * compile/compile-c-support.c (c_get_range_decl_name): Return a + std::string. + (generate_register_struct): Update. + * compile/compile-internal.h (c_get_range_decl_name): Return a + std::string. + (compile_register_name_mangled): Return std::string. + +2017-08-29 Tom Tromey + * utils.c (perror_string): Return a std::string. (throw_perror_with_name, perror_warning_with_name): Update. diff --git a/gdb/compile/compile-c-support.c b/gdb/compile/compile-c-support.c index 3bec28c..6f759ab 100644 --- a/gdb/compile/compile-c-support.c +++ b/gdb/compile/compile-c-support.c @@ -58,10 +58,10 @@ c_get_mode_for_size (int size) /* See compile-internal.h. */ -char * +std::string c_get_range_decl_name (const struct dynamic_prop *prop) { - return xstrprintf ("__gdb_prop_%s", host_address_to_string (prop)); + return string_printf ("__gdb_prop_%s", host_address_to_string (prop)); } @@ -263,8 +263,7 @@ generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch, if (registers_used[i]) { struct type *regtype = check_typedef (register_type (gdbarch, i)); - char *regname = compile_register_name_mangled (gdbarch, i); - struct cleanup *cleanups = make_cleanup (xfree, regname); + std::string regname = compile_register_name_mangled (gdbarch, i); seen = 1; @@ -282,7 +281,8 @@ generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch, switch (TYPE_CODE (regtype)) { case TYPE_CODE_PTR: - fprintf_filtered (stream, "__gdb_uintptr %s", regname); + fprintf_filtered (stream, "__gdb_uintptr %s", + regname.c_str ()); break; case TYPE_CODE_INT: @@ -297,7 +297,7 @@ generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch, fprintf_unfiltered (stream, "int %s" " __attribute__ ((__mode__(__%s__)))", - regname, + regname.c_str (), mode); break; } @@ -310,12 +310,10 @@ generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch, " unsigned char %s[%d]" " __attribute__((__aligned__(" "__BIGGEST_ALIGNMENT__)))", - regname, + regname.c_str (), TYPE_LENGTH (regtype)); } fputs_unfiltered (";\n", stream); - - do_cleanups (cleanups); } } diff --git a/gdb/compile/compile-c-symbols.c b/gdb/compile/compile-c-symbols.c index 15e1d6d..1cdea85 100644 --- a/gdb/compile/compile-c-symbols.c +++ b/gdb/compile/compile-c-symbols.c @@ -107,7 +107,6 @@ error_symbol_once (struct compile_c_instance *context, { struct symbol_error search; struct symbol_error *err; - char *message; if (context->symbol_err_map == NULL) return; @@ -117,10 +116,9 @@ error_symbol_once (struct compile_c_instance *context, if (err == NULL || err->message == NULL) return; - message = err->message; + gdb::unique_xmalloc_ptr message (err->message); err->message = NULL; - make_cleanup (xfree, message); - error (_("%s"), message); + error (_("%s"), message.get ()); } @@ -128,10 +126,11 @@ error_symbol_once (struct compile_c_instance *context, /* Compute the name of the pointer representing a local symbol's address. */ -static char * +static gdb::unique_xmalloc_ptr symbol_substitution_name (struct symbol *sym) { - return concat ("__", SYMBOL_NATURAL_NAME (sym), "_ptr", (char *) NULL); + return gdb::unique_xmalloc_ptr + (concat ("__", SYMBOL_NATURAL_NAME (sym), "_ptr", (char *) NULL)); } /* Convert a given symbol, SYM, to the compiler's representation. @@ -170,7 +169,7 @@ convert_one_symbol (struct compile_c_instance *context, gcc_decl decl; enum gcc_c_symbol_kind kind; CORE_ADDR addr = 0; - char *symbol_name = NULL; + gdb::unique_xmalloc_ptr symbol_name; switch (SYMBOL_CLASS (sym.symbol)) { @@ -290,13 +289,11 @@ convert_one_symbol (struct compile_c_instance *context, SYMBOL_NATURAL_NAME (sym.symbol), kind, sym_type, - symbol_name, addr, + symbol_name.get (), addr, filename, line); C_CTX (context)->c_ops->bind (C_CTX (context), decl, is_global); } - - xfree (symbol_name); } } @@ -604,13 +601,11 @@ generate_vla_size (struct compile_c_instance *compiler, || TYPE_HIGH_BOUND_KIND (type) == PROP_LOCLIST) { const struct dynamic_prop *prop = &TYPE_RANGE_DATA (type)->high; - char *name = c_get_range_decl_name (prop); - struct cleanup *cleanup = make_cleanup (xfree, name); + std::string name = c_get_range_decl_name (prop); - dwarf2_compile_property_to_c (stream, name, + dwarf2_compile_property_to_c (stream, name.c_str (), gdbarch, registers_used, prop, pc, sym); - do_cleanups (cleanup); } } break; @@ -663,8 +658,8 @@ generate_c_for_for_one_variable (struct compile_c_instance *compiler, if (SYMBOL_COMPUTED_OPS (sym) != NULL) { - char *generated_name = symbol_substitution_name (sym); - struct cleanup *cleanup = make_cleanup (xfree, generated_name); + gdb::unique_xmalloc_ptr generated_name + = symbol_substitution_name (sym); /* We need to emit to a temporary buffer in case an error occurs in the middle. */ string_file local_file; @@ -672,10 +667,9 @@ generate_c_for_for_one_variable (struct compile_c_instance *compiler, SYMBOL_COMPUTED_OPS (sym)->generate_c_location (sym, local_file, gdbarch, registers_used, - pc, generated_name); + pc, + generated_name.get ()); stream.write (local_file.c_str (), local_file.size ()); - - do_cleanups (cleanup); } else { diff --git a/gdb/compile/compile-c-types.c b/gdb/compile/compile-c-types.c index 22aee78..dc6391c 100644 --- a/gdb/compile/compile-c-types.c +++ b/gdb/compile/compile-c-types.c @@ -123,18 +123,17 @@ convert_array (struct compile_c_instance *context, struct type *type) || TYPE_HIGH_BOUND_KIND (range) == PROP_LOCLIST) { gcc_type result; - char *upper_bound; if (TYPE_VECTOR (type)) return C_CTX (context)->c_ops->error (C_CTX (context), _("variably-sized vector type" " is not supported")); - upper_bound = c_get_range_decl_name (&TYPE_RANGE_DATA (range)->high); + std::string upper_bound + = c_get_range_decl_name (&TYPE_RANGE_DATA (range)->high); result = C_CTX (context)->c_ops->build_vla_array_type (C_CTX (context), element_type, - upper_bound); - xfree (upper_bound); + upper_bound.c_str ()); return result; } else diff --git a/gdb/compile/compile-internal.h b/gdb/compile/compile-internal.h index 0c53f8c..091e654 100644 --- a/gdb/compile/compile-internal.h +++ b/gdb/compile/compile-internal.h @@ -95,11 +95,10 @@ struct compile_c_instance /* Call gdbarch_register_name (GDBARCH, REGNUM) and convert its result to a form suitable for the compiler source. The register names - should not clash with inferior defined macros. Returned pointer is - never NULL. Returned pointer needs to be deallocated by xfree. */ + should not clash with inferior defined macros. */ -extern char *compile_register_name_mangled (struct gdbarch *gdbarch, - int regnum); +extern std::string compile_register_name_mangled (struct gdbarch *gdbarch, + int regnum); /* Convert compiler source register name to register number of GDBARCH. Returned value is always >= 0, function throws an error @@ -144,13 +143,12 @@ extern unsigned char *generate_c_for_variable_locations extern const char *c_get_mode_for_size (int size); -/* Given a dynamic property, return an xmallocd name that is used to - represent its size. The result must be freed by the caller. The - contents of the resulting string will be the same each time for - each call with the same argument. */ +/* Given a dynamic property, return a name that is used to represent + its size. The contents of the resulting string will be the same + each time for each call with the same argument. */ struct dynamic_prop; -extern char *c_get_range_decl_name (const struct dynamic_prop *prop); +extern std::string c_get_range_decl_name (const struct dynamic_prop *prop); /* Type used to hold and pass around the source and object file names to use for compilation. */ diff --git a/gdb/compile/compile-loc2c.c b/gdb/compile/compile-loc2c.c index ead1003..7ec6f67 100644 --- a/gdb/compile/compile-loc2c.c +++ b/gdb/compile/compile-loc2c.c @@ -515,15 +515,12 @@ pushf_register_address (int indent, string_file &stream, unsigned char *registers_used, struct gdbarch *gdbarch, int regnum) { - char *regname = compile_register_name_mangled (gdbarch, regnum); - struct cleanup *cleanups = make_cleanup (xfree, regname); + std::string regname = compile_register_name_mangled (gdbarch, regnum); registers_used[regnum] = 1; pushf (indent, stream, "(" GCC_UINTPTR ") &" COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s", - regname); - - do_cleanups (cleanups); + regname.c_str ()); } /* Emit code that pushes a register's value on the stack. @@ -536,19 +533,16 @@ pushf_register (int indent, string_file &stream, unsigned char *registers_used, struct gdbarch *gdbarch, int regnum, uint64_t offset) { - char *regname = compile_register_name_mangled (gdbarch, regnum); - struct cleanup *cleanups = make_cleanup (xfree, regname); + std::string regname = compile_register_name_mangled (gdbarch, regnum); registers_used[regnum] = 1; if (offset == 0) pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s", - regname); + regname.c_str ()); else pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + (" GCC_UINTPTR ") %s", - regname, hex_string (offset)); - - do_cleanups (cleanups); + regname.c_str (), hex_string (offset)); } /* Compile a DWARF expression to C code. diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c index bbb31f1..e4865d0 100644 --- a/gdb/compile/compile.c +++ b/gdb/compile/compile.c @@ -648,12 +648,12 @@ eval_compile_command (struct command_line *cmd, const char *cmd_string, /* See compile/compile-internal.h. */ -char * +std::string compile_register_name_mangled (struct gdbarch *gdbarch, int regnum) { const char *regname = gdbarch_register_name (gdbarch, regnum); - return xstrprintf ("__%s", regname); + return string_printf ("__%s", regname); } /* See compile/compile-internal.h. */