[PATCH 1/2] Constify ada_encode return type

Tom Tromey tromey@adacore.com
Fri Oct 2 20:26:03 GMT 2020


Callers of ada_encode don't ever modify the result, and also don't own
it -- it is owned by a static buffer in ada_encode_1.  This patch
changes ada_encode to return a const char *, which I feel is somewhat
clearer.

gdb/ChangeLog
2020-10-02  Tom Tromey  <tromey@adacore.com>

	* ada-lang.h (ada_encode): Constify result.
	* ada-lang.c (ada_encode): Constify result.
	(ada_encode_1): Constify result.
---
 gdb/ChangeLog  | 6 ++++++
 gdb/ada-exp.y  | 6 +++---
 gdb/ada-lang.c | 4 ++--
 gdb/ada-lang.h | 2 +-
 4 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/gdb/ada-exp.y b/gdb/ada-exp.y
index 57d89b01fec..61d8df9df48 100644
--- a/gdb/ada-exp.y
+++ b/gdb/ada-exp.y
@@ -1201,9 +1201,9 @@ 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);
+  const char *temp_name = ada_encode (name0.ptr);
+  name_len = strlen (temp_name);
+  encoded_name = obstack_strndup (&temp_parse_space, temp_name, 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..2502d2847b2 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -923,7 +923,7 @@ const struct ada_opname_map ada_opname_table[] = {
    THROW_ERRORS, throw an error if invalid operator name is found.
    Otherwise, return NULL in that case.  */
 
-static char *
+static const char *
 ada_encode_1 (const char *decoded, bool throw_errors)
 {
   static char *encoding_buffer = NULL;
@@ -978,7 +978,7 @@ ada_encode_1 (const char *decoded, bool throw_errors)
 /* The "encoded" form of DECODED, according to GNAT conventions.
    The result is valid until the next call to ada_encode.  */
 
-char *
+const char *
 ada_encode (const char *decoded)
 {
   return ada_encode_1 (decoded, true);
diff --git a/gdb/ada-lang.h b/gdb/ada-lang.h
index ae313ce700a..949d16ce3a6 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 const char *ada_encode (const char *);
 
 extern const char *ada_enum_name (const char *);
 
-- 
2.26.2



More information about the Gdb-patches mailing list