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

Tom Tromey
2019-04-19T14:45:26Z

>>>>> "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.

Andrew> +/* Return true if TYPE is a string type. */
Andrew> +static bool
Andrew> +rust_is_string_type_p (struct type *type)
Andrew> +{
Andrew> + LONGEST low_bound, high_bound;
Andrew> +
Andrew> + type = check_typedef (type);
Andrew> + return ((TYPE_CODE (type) == TYPE_CODE_STRING)
Andrew> + || (TYPE_CODE (type) == TYPE_CODE_PTR
Andrew> + && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
Andrew> + && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
Andrew> + && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
Andrew> + &high_bound)))
Andrew> + || ((TYPE_CODE (type) == TYPE_CODE_UNION
Andrew> + || (TYPE_CODE (type) == TYPE_CODE_STRUCT
Andrew> + && !rust_enum_p (type)))
Andrew> + && rust_slice_type_p (type)
Andrew> + && strcmp (TYPE_NAME (type), "&str") == 0));

I didn't understand the reason for TYPE_CODE_UNION here.

Also, I think an array or slice of 'char' should probably be considered
a string in Rust. See rust_chartype_p.

thanks,
Tom