Re: [PATCHv2 4/5] gdb: Introduce new language field la_is_string_type_p

Andrew Burgess
2019-04-19T22:45:21Z

* Tom Tromey <tom@tromey.com> [2019-04-19 08:45:03 -0600]:

> >>>>> "Andrew" == Andrew Burgess <andrew.burgess@embecosm.com> writes:
>
> Andrew> Some languages already have a "is this a string" predicate that I was
> Andrew> able to reuse, while for other languages I've had to add a new
> Andrew> predicate. In this case I took inspiration from the value printing
> Andrew> code for that language - what different conditions would result in
> Andrew> printing something as a string.
>
> This looks essentially fine, but I had some questions.
>
> Andrew> +bool
> Andrew> +c_is_string_type_p (struct type *type)
> Andrew> +{
> Andrew> + type = check_typedef (type);
> Andrew> + while (TYPE_CODE (type) == TYPE_CODE_REF)
> Andrew> + {
> Andrew> + type = TYPE_TARGET_TYPE (type);
> Andrew> + type = check_typedef (type);
> Andrew> + }
> Andrew> +
> Andrew> + switch (TYPE_CODE (type))
> Andrew> + {
> Andrew> + case TYPE_CODE_ARRAY:
> Andrew> + {
> Andrew> + /* See if target type looks like a string. */
> Andrew> + struct type *array_target_type = TYPE_TARGET_TYPE (type);
> Andrew> + return (TYPE_LENGTH (type) > 0
> Andrew> + && TYPE_LENGTH (array_target_type) > 0
> Andrew> + && c_textual_element_type (array_target_type, 0));
> Andrew> + }
> Andrew> + case TYPE_CODE_STRING:
> Andrew> + return true;
>
> It seems to me that a "char *" should be considered a string in C;
> and probably a "wchar_t *" as well. Maybe see c-lang.c:classify_type.

Do you think that this applied on top of the previous would catch all
of the pointer cases?

I don't think we need to call classify_type as that seems to assume
that by the time it's called we know we have a character type of some
description, so I think calling c_textual_element_type is enough...

Let me know if you think I've missed something,

thanks,
Andrew

---

diff --git a/gdb/c-lang.c b/gdb/c-lang.c
index 4d5284e2e80..aeffefad55e 100644
--- a/gdb/c-lang.c
+++ b/gdb/c-lang.c
@@ -739,6 +739,11 @@ c_is_string_type_p (struct type *type)
}
case TYPE_CODE_STRING:
return true;
+ case TYPE_CODE_PTR:
+ {
+ struct type *element_type = TYPE_TARGET_TYPE (type);
+ return c_textual_element_type (element_type, 0);
+ }
default:
break;
}