This is the mail archive of the
gdb-patches@sourceware.cygnus.com
mailing list for the GDB project.
RFA: Improved C++ type printing
- To: gdb-patches at sourceware dot cygnus dot com
- Subject: RFA: Improved C++ type printing
- From: Jim Blandy <jimb at cygnus dot com>
- Date: Fri, 4 Feb 2000 09:47:11 -0500 (EST)
These changes tighten up some of the munging of C++ names GDB does
when printing class methods. They remove a number of test suite
failures that appear when using Dwarf 2 debugging information (due to
permissible variation between GCC's stabs and Dwarf 2 output).
Specifically:
- In some circumstances, GDB's symbol tables hold qualified names
of operators or methods ("classname::methodname"), but since GDB is
printing them out as part of the class definition, the qualifiers
are redundant (and incorrect, I think). GDB used to try to peel off
all the qualifiers using strrchr to scan for colons, but this would
reach inside template arguments or the function's argument list
itself, generating garbage. (In other words, exactly the kind of
failure you'd expect when using simple-minded string hacking to
operate on structured data like C++ qualified names.)
Unfortunately, GDB only has this data in string form, so every
solution involves some on-the-fly parsing. My change simply does it
better.
- C++ supports `type conversion operators', which are operators that
allow you to specify conversions from user-defined types to basic
types. They have names that look like `operator int ()'. GDB wants
to avoid printing out the return types for such operators, because
they are redundant. However, it has no good way to recognize such
operators, other than by looking at their name. Without this
change, GDB uses the rather reckless heuristic of checking for a
space after the word "operator". This assumes that the debug info
will never provide names like "operator =", and is flat-out wrong
for things like "operator new" and "operator delete". Again, my
change replaces an inadequate parser with something that actually
does the job according to the C++ grammar.
2000-02-04 Jim Blandy <jimb@redhat.com>
* c-typeprint.c (remove_qualifiers): New function.
(c_type_print_base): Use it to remove qualifiers from C++
qualified names, not strrchr.
* c-typeprint.c (c_type_print_base): Recognize type conversion
operators by calling is_type_conversion_operator.
(is_type_conversion_operator): New function.
Index: c-typeprint.c
===================================================================
RCS file: /cvs/cvsfiles/devo/gdb/c-typeprint.c,v
retrieving revision 2.41
diff -c -r2.41 c-typeprint.c
*** c-typeprint.c 2000/02/01 03:09:00 2.41
--- c-typeprint.c 2000/02/04 14:31:18
***************
*** 433,438 ****
--- 433,567 ----
fprintf_filtered (stream, ")");
}
+
+ /* Return true iff the j'th overloading of the i'th method of TYPE
+ is a type conversion operator, like `operator int () { ... }'.
+ When listing a class's methods, we don't print the return type of
+ such operators. */
+ static int
+ is_type_conversion_operator (struct type *type, int i, int j)
+ {
+ /* I think the whole idea of recognizing type conversion operators
+ by their name is pretty terrible. But I don't think our present
+ data structure gives us any other way to tell. If you know of
+ some other way, feel free to rewrite this function. */
+ char *name = TYPE_FN_FIELDLIST_NAME (type, i);
+
+ if (strncmp (name, "operator", 8) != 0)
+ return 0;
+
+ name += 8;
+ if (! strchr (" \t\f\n\r", *name))
+ return 0;
+
+ while (strchr (" \t\f\n\r", *name))
+ name++;
+
+ if (strncmp (name, "new", 3) == 0)
+ name += 3;
+ else if (strncmp (name, "delete", 6) == 0)
+ name += 6;
+ else
+ return 0;
+
+ /* Is that really the end of the name? */
+ if (('a' <= *name && *name <= 'z')
+ || ('A' <= *name && *name <= 'Z')
+ || ('0' <= *name && *name <= '9')
+ || *name == '_')
+ /* No, so the identifier following "operator" must be a type name,
+ and this is a type conversion operator. */
+ return 1;
+
+ /* That was indeed the end of the name, so it was `operator new' or
+ `operator delete', neither of which are type conversion operators. */
+ return 0;
+ }
+
+
+ /* Given a C++ qualified identifier QID, strip off the qualifiers,
+ yielding the unqualified name. The return value is a pointer into
+ the original string.
+
+ It's a pity we don't have this information in some more structured
+ form. Even the author of this function feels that writing little
+ parsers like this everywhere is stupid. */
+ static char *
+ remove_qualifiers (char *qid)
+ {
+ int quoted = 0; /* zero if we're not in quotes;
+ '"' if we're in a double-quoted string;
+ '\'' if we're in a single-quoted string. */
+ int depth = 0; /* number of unclosed parens we've seen */
+ char *parenstack = (char *) alloca (strlen (qid));
+ char *scan;
+ char *last = 0; /* The character after the rightmost
+ `::' token we've seen so far. */
+
+ for (scan = qid; *scan; scan++)
+ {
+ if (quoted)
+ {
+ if (*scan == quoted)
+ quoted = 0;
+ else if (*scan == '\\' && *(scan + 1))
+ scan++;
+ }
+ else if (scan[0] == ':' && scan[1] == ':')
+ {
+ /* If we're inside parenthesis (i.e., an argument list) or
+ angle brackets (i.e., a list of template arguments), then
+ we don't record the position of this :: token, since it's
+ not relevant to the top-level structure we're trying
+ to operate on. */
+ if (depth == 0)
+ {
+ last = scan + 2;
+ scan++;
+ }
+ }
+ else if (*scan == '"' || *scan == '\'')
+ quoted = *scan;
+ else if (*scan == '(')
+ parenstack[depth++] = ')';
+ else if (*scan == '[')
+ parenstack[depth++] = ']';
+ /* We're going to treat <> as a pair of matching characters,
+ since we're more likely to see those in template id's than
+ real less-than characters. What a crock. */
+ else if (*scan == '<')
+ parenstack[depth++] = '>';
+ else if (*scan == ')' || *scan == ']' || *scan == '>')
+ {
+ if (depth > 0 && parenstack[depth - 1] == *scan)
+ depth--;
+ else
+ {
+ /* We're going to do a little error recovery here. If we
+ don't find a match for *scan on the paren stack, but
+ there is something lower on the stack that does match, we
+ pop the stack to that point. */
+ int i;
+
+ for (i = depth - 1; i >= 0; i--)
+ if (parenstack[i] == *scan)
+ {
+ depth = i;
+ break;
+ }
+ }
+ }
+ }
+
+ if (last)
+ return last;
+ else
+ /* We didn't find any :: tokens at the top level, so declare the
+ whole thing an unqualified identifier. */
+ return qid;
+ }
+
+
/* Print any array sizes, function arguments or close parentheses
needed after the variable name (to describe its type).
Args work like c_type_print_varspec_prefix. */
***************
*** 896,903 ****
}
else if (!is_constructor && /* constructors don't have declared types */
!is_full_physname_constructor && /* " " */
! !strstr (method_name, "operator ")) /* Not a type conversion operator */
! /* (note space -- other operators don't have it) */
{
type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
"", stream, -1);
--- 1025,1031 ----
}
else if (!is_constructor && /* constructors don't have declared types */
!is_full_physname_constructor && /* " " */
! !is_type_conversion_operator (type, i, j))
{
type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
"", stream, -1);
***************
*** 931,945 ****
else
{
char *p;
! char *demangled_no_class = strrchr (demangled_name, ':');
! if (demangled_no_class == NULL)
! demangled_no_class = demangled_name;
! else
! {
! ++demangled_no_class; /* skip over last ':' */
! }
! /* get rid of the static word appended by the demangler */
p = strstr (demangled_no_class, " static");
if (p != NULL)
{
--- 1059,1068 ----
else
{
char *p;
! char *demangled_no_class
! = remove_qualifiers (demangled_name);
! /* get rid of the `static' appended by the demangler */
p = strstr (demangled_no_class, " static");
if (p != NULL)
{