From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTP id 8694E3857C6F for ; Thu, 8 Oct 2020 17:50:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8694E3857C6F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey@adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 3AE261179A7; Thu, 8 Oct 2020 13:50:42 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id oQUYwrNhSGJS; Thu, 8 Oct 2020 13:50:42 -0400 (EDT) Received: from murgatroyd.Home (174-16-122-38.hlrn.qwest.net [174.16.122.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPSA id EBD0D1179C0; Thu, 8 Oct 2020 13:50:41 -0400 (EDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Return std::string from ada_encode Date: Thu, 8 Oct 2020 11:50:40 -0600 Message-Id: <20201008175040.3673312-1-tromey@adacore.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 08 Oct 2020 17:50:43 -0000 This changes ada_encode to return a std::string. This simplifies it somewhat, removes a use of GROW_VECT, and is also simpler for callers to use. gdb/ChangeLog 2020-10-08 Tom Tromey * ada-lang.h (ada_encode): Return std::string. * ada-lang.c (ada_encode_1): Return std::string. (ada_encode): Likewise. (type_from_tag, ada_lookup_name_info::ada_lookup_name_info): Update. * ada-exp.y (block_lookup, write_var_or_type): Update. --- gdb/ChangeLog | 9 +++++++++ gdb/ada-exp.y | 13 +++++++++---- gdb/ada-lang.c | 50 +++++++++++++++----------------------------------- gdb/ada-lang.h | 2 +- 4 files changed, 34 insertions(+), 40 deletions(-) diff --git a/gdb/ada-exp.y b/gdb/ada-exp.y index 57d89b01fec..d09b43ea9ca 100644 --- a/gdb/ada-exp.y +++ b/gdb/ada-exp.y @@ -953,13 +953,17 @@ block_lookup (const struct block *context, const char *raw_name) struct symtab *symtab; const struct block *result = NULL; + std::string name_storage; if (raw_name[0] == '\'') { raw_name += 1; name = raw_name; } else - name = ada_encode (raw_name); + { + name_storage = ada_encode (raw_name); + name = name_storage.c_str (); + } nsyms = ada_lookup_symbol_list (name, context, VAR_DOMAIN, &syms); @@ -1201,9 +1205,10 @@ write_var_or_type (struct parser_state *par_state, if (block == NULL) block = par_state->expression_context_block; - encoded_name = ada_encode (name0.ptr); - name_len = strlen (encoded_name); - encoded_name = obstack_strndup (&temp_parse_space, encoded_name, name_len); + std::string name_storage = ada_encode (name0.ptr); + name_len = name_storage.size (); + encoded_name = obstack_strndup (&temp_parse_space, name_storage.c_str (), + name_len); for (depth = 0; depth < MAX_RENAMING_CHAIN_LENGTH; depth += 1) { int tail_index; diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index 0df406bff48..fbce14bbe42 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -918,33 +918,21 @@ const struct ada_opname_map ada_opname_table[] = { {NULL, NULL} }; -/* The "encoded" form of DECODED, according to GNAT conventions. The - result is valid until the next call to ada_encode. If +/* The "encoded" form of DECODED, according to GNAT conventions. If THROW_ERRORS, throw an error if invalid operator name is found. - Otherwise, return NULL in that case. */ + Otherwise, return the empty string in that case. */ -static char * +static std::string ada_encode_1 (const char *decoded, bool throw_errors) { - static char *encoding_buffer = NULL; - static size_t encoding_buffer_size = 0; - const char *p; - int k; - if (decoded == NULL) - return NULL; - - GROW_VECT (encoding_buffer, encoding_buffer_size, - 2 * strlen (decoded) + 10); + return {}; - k = 0; - for (p = decoded; *p != '\0'; p += 1) + std::string encoding_buffer; + for (const char *p = decoded; *p != '\0'; p += 1) { if (*p == '.') - { - encoding_buffer[k] = encoding_buffer[k + 1] = '_'; - k += 2; - } + encoding_buffer.append ("__"); else if (*p == '"') { const struct ada_opname_map *mapping; @@ -958,27 +946,21 @@ ada_encode_1 (const char *decoded, bool throw_errors) if (throw_errors) error (_("invalid Ada operator name: %s"), p); else - return NULL; + return {}; } - strcpy (encoding_buffer + k, mapping->encoded); - k += strlen (mapping->encoded); + encoding_buffer.append (mapping->encoded); break; } else - { - encoding_buffer[k] = *p; - k += 1; - } + encoding_buffer.push_back (*p); } - encoding_buffer[k] = '\0'; return encoding_buffer; } -/* The "encoded" form of DECODED, according to GNAT conventions. - The result is valid until the next call to ada_encode. */ +/* The "encoded" form of DECODED, according to GNAT conventions. */ -char * +std::string ada_encode (const char *decoded) { return ada_encode_1 (decoded, true); @@ -6384,7 +6366,7 @@ type_from_tag (struct value *tag) gdb::unique_xmalloc_ptr type_name = ada_tag_name (tag); if (type_name != NULL) - return ada_find_any_type (ada_encode (type_name.get ())); + return ada_find_any_type (ada_encode (type_name.get ()).c_str ()); return NULL; } @@ -13613,10 +13595,8 @@ ada_lookup_name_info::ada_lookup_name_info (const lookup_name_info &lookup_name) if (!m_encoded_p) { const char *folded = ada_fold_name (user_name); - const char *encoded = ada_encode_1 (folded, false); - if (encoded != NULL) - m_encoded_name = encoded; - else + m_encoded_name = ada_encode_1 (folded, false); + if (m_encoded_name.empty ()) m_encoded_name = gdb::to_string (user_name); } else diff --git a/gdb/ada-lang.h b/gdb/ada-lang.h index ae313ce700a..93319267654 100644 --- a/gdb/ada-lang.h +++ b/gdb/ada-lang.h @@ -318,7 +318,7 @@ extern struct type *ada_get_base_type (struct type *); extern struct type *ada_check_typedef (struct type *); -extern char *ada_encode (const char *); +extern std::string ada_encode (const char *); extern const char *ada_enum_name (const char *); -- 2.26.2