From b2bebdb0f184e8c09c59f9efdc06aea8514939de Mon Sep 17 00:00:00 2001 From: Jim Kingdon Date: Fri, 2 Jul 1993 02:57:06 +0000 Subject: [PATCH] * gdbtypes.h (struct type): Add field tag_name. * gdbtypes.c (type_name_no_tag), c-typeprint.c (c_type_print_base): Use it. * {coff,dwarf,mips,stabs}read.c: Set it. --- gdb/c-typeprint.c | 91 +++++++++++++++-------------------------------- gdb/dwarfread.c | 13 +++---- gdb/gdbtypes.c | 39 +++++--------------- gdb/gdbtypes.h | 7 ++++ gdb/stabsread.c | 51 ++++++-------------------- 5 files changed, 58 insertions(+), 143 deletions(-) diff --git a/gdb/c-typeprint.c b/gdb/c-typeprint.c index cf108d26005..dffa048ddc9 100644 --- a/gdb/c-typeprint.c +++ b/gdb/c-typeprint.c @@ -455,7 +455,6 @@ c_type_print_base (type, stream, show, level) int show; int level; { - char *name; register int i; register int len; register int lastval; @@ -472,9 +471,13 @@ c_type_print_base (type, stream, show, level) } /* When SHOW is zero or less, and there is a valid type name, then always - just print the type name directly from the type. */ + just print the type name directly from the type. */ + /* If we have "typedef struct foo {. . .} bar;" do we want to print it + as "struct foo" or as "bar"? Pick the latter, because C++ folk tend + to expect things like "class5 *foo" rather than "struct class5 *foo". */ - if ((show <= 0) && (TYPE_NAME (type) != NULL)) + if (show <= 0 + && TYPE_NAME (type) != NULL) { fputs_filtered (TYPE_NAME (type), stream); return; @@ -494,67 +497,32 @@ c_type_print_base (type, stream, show, level) case TYPE_CODE_STRUCT: if (HAVE_CPLUS_STRUCT (type)) { - /* Always print it as "class foo" even if foo is a typedef'd - name, not a tag. */ fprintf_filtered (stream, "class "); - name = type_name_no_tag (type); - if (name != NULL) - { - fputs_filtered (name, stream); - fputs_filtered (" ", stream); - wrap_here (" "); - } } else { fprintf_filtered (stream, "struct "); - name = TYPE_NAME (type); - /* If the name does not start with "struct " it means that the - type was defined without a tag, so don't print a tag. It is - possible that we should have a better way of distinguising - tag names from typedef'd names. (e.g. a new tagname field in - the struct type). */ - if (name != NULL && strncmp (name, "struct ", 7) == 0) - { - fputs_filtered (name + 7, stream); - fputs_filtered (" ", stream); - } } goto struct_union; case TYPE_CODE_UNION: fprintf_filtered (stream, "union "); - if (HAVE_CPLUS_STRUCT (type)) - { - /* Always print it as "union foo" even if foo is a typedef'd - name, not a tag. */ - name = type_name_no_tag (type); - if (name != NULL) - { - fputs_filtered (name, stream); - fputs_filtered (" ", stream); - wrap_here (" "); - } - } - else + + struct_union: + if (TYPE_TAG_NAME (type) != NULL) { - name = TYPE_NAME (type); - /* If the name does not start with "union " it means that the - type was defined without a tag, so don't print a tag. It is - possible that we should have a better way of distinguising - tag names from typedef'd names. (e.g. a new tagname field in - the struct type). */ - if (name != NULL && strncmp (name, "union ", 6) == 0) - { - fputs_filtered (name + 6, stream); - fputs_filtered (" ", stream); - } + fputs_filtered (TYPE_TAG_NAME (type), stream); + if (show > 0) + fputs_filtered (" ", stream); } - struct_union: wrap_here (" "); if (show < 0) - fprintf_filtered (stream, "{...}"); - else + { + /* If we just printed a tag name, no need to print anything else. */ + if (TYPE_TAG_NAME (type) == NULL) + fprintf_filtered (stream, "{...}"); + } + else if (show > 0) { check_stub_type (type); @@ -653,6 +621,7 @@ c_type_print_base (type, stream, show, level) struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i); int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i); char *method_name = TYPE_FN_FIELDLIST_NAME (type, i); + char *name = type_name_no_tag (type); int is_constructor = name && STREQ(method_name, name); for (j = 0; j < len2; j++) { @@ -739,23 +708,21 @@ c_type_print_base (type, stream, show, level) case TYPE_CODE_ENUM: fprintf_filtered (stream, "enum "); - name = TYPE_NAME (type); - - /* If the name does not start with "enum " it means that the - type was defined without a tag, so don't print a tag. It is - possible that we should have a better way of distinguising - tag names from typedef'd names. (e.g. a new tagname field in - the struct type). */ - if (name != NULL && strncmp (name, "enum ", 5) == 0) + if (TYPE_TAG_NAME (type) != NULL) { - fputs_filtered (name + 5, stream); - fputs_filtered (" ", stream); + fputs_filtered (TYPE_TAG_NAME (type), stream); + if (show > 0) + fputs_filtered (" ", stream); } wrap_here (" "); if (show < 0) - fprintf_filtered (stream, "{...}"); - else + { + /* If we just printed a tag name, no need to print anything else. */ + if (TYPE_TAG_NAME (type) == NULL) + fprintf_filtered (stream, "{...}"); + } + else if (show > 0) { fprintf_filtered (stream, "{"); len = TYPE_NFIELDS (type); diff --git a/gdb/dwarfread.c b/gdb/dwarfread.c index 1e7a6d0a37d..709b9584e72 100644 --- a/gdb/dwarfread.c +++ b/gdb/dwarfread.c @@ -1026,7 +1026,6 @@ struct_type (dip, thisdie, enddie, objfile) struct nextfield *new; int nfields = 0; int n; - char *tpart1; struct dieinfo mbr; char *nextdie; #if !BITS_BIG_ENDIAN @@ -1043,20 +1042,16 @@ struct_type (dip, thisdie, enddie, objfile) { case TAG_class_type: TYPE_CODE (type) = TYPE_CODE_CLASS; - tpart1 = "class"; break; case TAG_structure_type: TYPE_CODE (type) = TYPE_CODE_STRUCT; - tpart1 = "struct"; break; case TAG_union_type: TYPE_CODE (type) = TYPE_CODE_UNION; - tpart1 = "union"; break; default: /* Should never happen */ TYPE_CODE (type) = TYPE_CODE_UNDEF; - tpart1 = "???"; complain (&missing_tag, DIE_ID, DIE_NAME); break; } @@ -1067,8 +1062,8 @@ struct_type (dip, thisdie, enddie, objfile) && *dip -> at_name != '~' && *dip -> at_name != '.') { - TYPE_NAME (type) = obconcat (&objfile -> type_obstack, - tpart1, " ", dip -> at_name); + TYPE_TAG_NAME (type) = obconcat (&objfile -> type_obstack, + "", "", dip -> at_name); } /* Use whatever size is known. Zero is a valid size. We might however wish to check has_at_byte_size to make sure that some byte size was @@ -1756,8 +1751,8 @@ enum_type (dip, objfile) && *dip -> at_name != '~' && *dip -> at_name != '.') { - TYPE_NAME (type) = obconcat (&objfile -> type_obstack, "enum", - " ", dip -> at_name); + TYPE_TAG_NAME (type) = obconcat (&objfile -> type_obstack, + "", "", dip -> at_name); } if (dip -> at_byte_size != 0) { diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index 9bcdec00467..3710f39fd42 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -472,43 +472,20 @@ smash_to_method_type (type, domain, to_type, args) TYPE_CODE (type) = TYPE_CODE_METHOD; } -/* Return a typename for a struct/union/enum type - without the tag qualifier. If the type has a NULL name, - NULL is returned. */ +/* Return a typename for a struct/union/enum type without "struct ", + "union ", or "enum ". If the type has a NULL name, return NULL. */ char * type_name_no_tag (type) register const struct type *type; { - register char *name; + if (TYPE_TAG_NAME (type) != NULL) + return TYPE_TAG_NAME (type); - if ((name = TYPE_NAME (type)) != NULL) - { - switch (TYPE_CODE (type)) - { - case TYPE_CODE_STRUCT: - if(!strncmp (name, "struct ", 7)) - { - name += 7; - } - break; - case TYPE_CODE_UNION: - if(!strncmp (name, "union ", 6)) - { - name += 6; - } - break; - case TYPE_CODE_ENUM: - if(!strncmp (name, "enum ", 5)) - { - name += 5; - } - break; - default: /* To avoid -Wall warnings */ - break; - } - } - return (name); + /* Is there code which expects this to return the name if there is no + tag name? My guess is that this is mainly used for C++ in cases where + the two will always be the same. */ + return TYPE_NAME (type); } /* Lookup a primitive type named NAME. diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h index 667a8f56cb9..848ce5fae20 100644 --- a/gdb/gdbtypes.h +++ b/gdb/gdbtypes.h @@ -142,6 +142,12 @@ struct type char *name; + /* Tag name for this type, or NULL if none. This is a feature which is + specific to C/C++ for structs, unions, or enums. + This is used for printing only, except by poorly designed C++ code. */ + + char *tag_name; + /* Length, in units of TARGET_CHAR_BIT bits, of storage for a value of this type */ @@ -399,6 +405,7 @@ allocate_cplus_struct_type PARAMS ((struct type *)); (TYPE_CPLUS_SPECIFIC(type) != &cplus_struct_default) #define TYPE_NAME(thistype) (thistype)->name +#define TYPE_TAG_NAME(type) ((type)->tag_name) #define TYPE_TARGET_TYPE(thistype) (thistype)->target_type #define TYPE_POINTER_TYPE(thistype) (thistype)->pointer_type #define TYPE_REFERENCE_TYPE(thistype) (thistype)->reference_type diff --git a/gdb/stabsread.c b/gdb/stabsread.c index 4f862156d13..7e8f10a99bc 100644 --- a/gdb/stabsread.c +++ b/gdb/stabsread.c @@ -1086,14 +1086,9 @@ define_symbol (valu, string, desc, type, objfile) SYMBOL_CLASS (sym) = LOC_TYPEDEF; SYMBOL_VALUE (sym) = valu; SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE; - if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0) - TYPE_NAME (SYMBOL_TYPE (sym)) - = obconcat (&objfile -> type_obstack, "", - (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_ENUM - ? "enum " - : (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT - ? "struct " : "union ")), - SYMBOL_NAME (sym)); + if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0) + TYPE_TAG_NAME (SYMBOL_TYPE (sym)) + = obconcat (&objfile -> type_obstack, "", "", SYMBOL_NAME (sym)); add_symbol_to_list (sym, &file_symbols); if (synonym) @@ -1105,6 +1100,9 @@ define_symbol (valu, string, desc, type, objfile) SYMBOL_CLASS (typedef_sym) = LOC_TYPEDEF; SYMBOL_VALUE (typedef_sym) = valu; SYMBOL_NAMESPACE (typedef_sym) = VAR_NAMESPACE; + if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0) + TYPE_NAME (SYMBOL_TYPE (sym)) + = obconcat (&objfile -> type_obstack, "", "", SYMBOL_NAME (sym)); add_symbol_to_list (typedef_sym, &file_symbols); } break; @@ -1317,15 +1315,12 @@ read_type (pp, objfile) { case 's': code = TYPE_CODE_STRUCT; - prefix = "struct "; break; case 'u': code = TYPE_CODE_UNION; - prefix = "union "; break; case 'e': code = TYPE_CODE_ENUM; - prefix = "enum "; break; default: return error_type (pp); @@ -1333,17 +1328,8 @@ read_type (pp, objfile) to = type_name = (char *) obstack_alloc (&objfile -> type_obstack, - (strlen (prefix) + - ((char *) strchr (*pp, ':') - (*pp)) + 1)); + (((char *) strchr (*pp, ':') - (*pp)) + 1)); - /* Copy the prefix. */ - from = prefix; - while ((*to++ = *from++) != '\0') - ; - to--; - - type_name_only = to; - /* Copy the name. */ from = *pp + 1; while ((*to++ = *from++) != ':') @@ -1352,23 +1338,6 @@ read_type (pp, objfile) /* Set the pointer ahead of the name which we just read. */ *pp = from; - -#if 0 - /* The following hack is clearly wrong, because it doesn't - check whether we are in a baseclass. I tried to reproduce - the case that it is trying to fix, but I couldn't get - g++ to put out a cross reference to a basetype. Perhaps - it doesn't do it anymore. */ - /* Note: for C++, the cross reference may be to a base type which - has not yet been seen. In this case, we skip to the comma, - which will mark the end of the base class name. (The ':' - at the end of the base class name will be skipped as well.) - But sometimes (ie. when the cross ref is the last thing on - the line) there will be no ','. */ - from = (char *) strchr (*pp, ','); - if (from) - *pp = from; -#endif /* 0 */ } /* Now check to see whether the type has already been declared. */ @@ -1386,7 +1355,7 @@ read_type (pp, objfile) if (SYMBOL_CLASS (sym) == LOC_TYPEDEF && SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE && (TYPE_CODE (SYMBOL_TYPE (sym)) == code) - && STREQ (SYMBOL_NAME (sym), type_name_only)) + && STREQ (SYMBOL_NAME (sym), type_name)) { obstack_free (&objfile -> type_obstack, type_name); type = SYMBOL_TYPE (sym); @@ -1401,7 +1370,7 @@ read_type (pp, objfile) type. */ type = dbx_alloc_type (typenums, objfile); TYPE_CODE (type) = code; - TYPE_NAME (type) = type_name; + TYPE_TAG_NAME (type) = type_name; INIT_CPLUS_SPECIFIC(type); TYPE_FLAGS (type) |= TYPE_FLAG_STUB; @@ -3482,7 +3451,7 @@ cleanup_undefined_types () struct pending *ppt; int i; /* Name of the type, without "struct" or "union" */ - char *typename = type_name_no_tag (*type); + char *typename = TYPE_TAG_NAME (*type); if (typename == NULL) { -- 2.30.2