[1/2] Constify ada_encode return type

Message ID 20201002202604.1517475-2-tromey@adacore.com
State New
Headers show
Series
  • ada_encode rewrites
Related show

Commit Message

Tom Tromey Oct. 2, 2020, 8:26 p.m.
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(-)

-- 
2.26.2

Comments

Simon Marchi Oct. 2, 2020, 9:51 p.m. | #1
On 2020-10-02 4:26 p.m., Tom Tromey wrote:
> 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);


Could this be further simplified to:

  encoded_name = obstack_strdup (&temp_parse_space, ada_encode (name0.ptr));

?

In any case, this LGTM.

Simon
Tom Tromey Oct. 7, 2020, 6:26 p.m. | #2
>> +  const char *temp_name = ada_encode (name0.ptr);

>> +  name_len = strlen (temp_name);

>> +  encoded_name = obstack_strndup (&temp_parse_space, temp_name, name_len);


Simon> Could this be further simplified to:
Simon>   encoded_name = obstack_strdup (&temp_parse_space, ada_encode (name0.ptr));
Simon> ?

name_len is used later in the function, so this would involve an extra
strlen.

Tom

Patch

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 *);