--- /home/delana/gcc-20021001_0319/gcc/tree-dump.c 2002-10-01 03:19:37.000000000 +0000 +++ tree-dump.c 2002-10-01 06:29:44.000000000 +0000 @@ -35,6 +35,25 @@ static void dump_maybe_newline PARAMS ((dump_info_p)); static void dump_string_field PARAMS ((dump_info_p, const char *, const char *)); +#define XML_GXX_TU_DOCTYPE "GXXTranslationUnit" + +typedef enum { + xml_field_child_type, + xml_field_pointer_type, + xml_field_integer_type, + xml_field_string_type +} xml_field_type; + +#define XML_FIELD_TYPE_GET_NAME(_t_) \ +(((_t_) == xml_field_child_type) ? "_c_" : \ + (((_t_) == xml_field_pointer_type) ? "_p_" : \ + (((_t_) == xml_field_integer_type) ? "_i_" : \ + (((_t_) == xml_field_string_type) ? "_s_" : NULL)))) + +static void dump_xml_field PARAMS ((dump_info_p, xml_field_type, const char*, + const char*, ...)); +static void dump_xml_string PARAMS ((dump_info_p, const char*)); + /* Add T to the end of the queue of nodes to dump. Returns the index assigned to T. */ @@ -116,10 +135,15 @@ index = queue (di, t, flags); /* Print the index of the node. */ - dump_maybe_newline (di); - fprintf (di->stream, "%-4s: ", field); - di->column += 6; - dump_index (di, index); + if (di->flags == TDF_XML) + dump_xml_field (di, xml_field_child_type, field, "%u", index); + else + { + dump_maybe_newline (di); + fprintf (di->stream, "%-4s: ", field); + di->column += 6; + dump_index (di, index); + } } /* Dump the type of T. */ @@ -144,6 +168,7 @@ dump_new_line (di) dump_info_p di; { + if (di->flags == TDF_XML) return; fprintf (di->stream, "\n%*s", SOL_COLUMN, ""); di->column = SOL_COLUMN; } @@ -156,6 +181,8 @@ { int extra; + if (di->flags == TDF_XML) return; + /* See if we need a new line. */ if (di->column > EOL_COLUMN) dump_new_line (di); @@ -175,9 +202,14 @@ const char *field; void *ptr; { - dump_maybe_newline (di); - fprintf (di->stream, "%-4s: %-8lx ", field, (long) ptr); - di->column += 15; + if (di->flags == TDF_XML) + dump_xml_field (di, xml_field_pointer_type, field, "%lx", (long)ptr); + else + { + dump_maybe_newline (di); + fprintf (di->stream, "%-4s: %-8lx ", field, (long) ptr); + di->column += 15; + } } /* Dump integer I using FIELD to identify it. */ @@ -188,9 +220,14 @@ const char *field; int i; { - dump_maybe_newline (di); - fprintf (di->stream, "%-4s: %-7d ", field, i); - di->column += 14; + if (di->flags == TDF_XML) + dump_xml_field (di, xml_field_integer_type, field, "%d", i); + else + { + dump_maybe_newline (di); + fprintf (di->stream, "%-4s: %-7d ", field, i); + di->column += 14; + } } /* Dump the string S. */ @@ -200,12 +237,17 @@ dump_info_p di; const char *string; { - dump_maybe_newline (di); - fprintf (di->stream, "%-13s ", string); - if (strlen (string) > 13) - di->column += strlen (string) + 1; + if (di->flags == TDF_XML) + dump_xml_field (di, xml_field_string_type, NULL, "%s", string); else - di->column += 14; + { + dump_maybe_newline (di); + fprintf (di->stream, "%-13s ", string); + if (strlen (string) > 13) + di->column += strlen (string) + 1; + else + di->column += 14; + } } /* Dump the string field S. */ @@ -216,12 +258,110 @@ const char *field; const char *string; { - dump_maybe_newline (di); - fprintf (di->stream, "%-4s: %-7s ", field, string); - if (strlen (string) > 7) - di->column += 6 + strlen (string) + 1; + if (di->flags == TDF_XML) + dump_xml_field (di, xml_field_string_type, field, "%s", string); else - di->column += 14; + { + dump_maybe_newline (di); + fprintf (di->stream, "%-4s: %-7s ", field, string); + if (strlen (string) > 7) + di->column += 6 + strlen (string) + 1; + else + di->column += 14; + } +} + +/* Dump the given string in xml-escaped mode */ + +static void +dump_xml_string (di, str) + dump_info_p di; + const char* str; +{ + if (str == NULL) return; + while (*str) + { + switch (*str) + { + case '&': + fprintf (di->stream, "&"); + break; + case '\'': + fprintf (di->stream, "'"); + break; + case '>': + fprintf (di->stream, ">"); + break; + case '<': + fprintf (di->stream, "<"); + break; + case '"': + fprintf (di->stream, """); + break; + default: + fprintf (di->stream, "%c", *str); + } + str++; + } +} + +/* Dump the given xml field and its attributes */ + +static void +dump_xml_field (di, type, name, value) + dump_info_p di; + xml_field_type type; + const char* name; + const char* value; +{ + static char* tmpbuf = NULL; + static int tmpbuf_size_chunk = 25; + static int tmpbuf_size = 0; + int vsnprintf_ret; + + // dump the xml `field' element ( open mark ) + fprintf (di->stream, " stream, "type=\"%s\" ", XML_FIELD_TYPE_GET_NAME (type)); + + // dump the `name' attribute of the xml `field' element ( xml-unescaped ) + if (name) fprintf (di->stream, "name=\"%s\" ", name); + + // allocate a minimal string buffer, if needed + if (tmpbuf==NULL) + { + tmpbuf_size += tmpbuf_size_chunk; + tmpbuf = (char*)xmalloc(tmpbuf_size); + } + + // print in the string buffer, extending it if necessary, the + // `value' attribute of the xml `field' element ( xml-unescaped ) + VA_OPEN (ap, value); + VA_FIXEDARG (ap, dump_info_p, di); + VA_FIXEDARG (ap, xml_field_type, type); + VA_FIXEDARG (ap, const char*, name); + + while (1) + { + vsnprintf_ret = vsnprintf (tmpbuf, tmpbuf_size-1, value, ap); + if ((vsnprintf_ret == -1) || (vsnprintf_ret > tmpbuf_size-1)) + { + tmpbuf_size += tmpbuf_size_chunk; + tmpbuf = (char*)xrealloc (tmpbuf, tmpbuf_size); + } + else break; + } + + VA_CLOSE (ap); + + // then dump the `value' field attribute as xml-escaped string + fprintf (di->stream, "value=\""); + dump_xml_string (di, tmpbuf); + fprintf (di->stream, "\""); + + // dump the xml `field' element ( close mark ) + fprintf (di->stream, "/>\n"); } /* Dump the next node in the queue. */ @@ -253,15 +393,22 @@ dq->next = di->free_list; di->free_list = dq; - /* Print the node index. */ - dump_index (di, index); - /* And the type of node this is. */ + /* Determine the type of node this is */ if (dni->binfo_p) code_name = "binfo"; else code_name = tree_code_name[(int) TREE_CODE (t)]; - fprintf (di->stream, "%-16s ", code_name); - di->column = 25; + + if (di->flags != TDF_XML) + { + /* Print the node index. */ + dump_index (di, index); + /* And the type of node this is. */ + fprintf (di->stream, "%-16s ", code_name); + di->column = 25; + } + else + fprintf (di->stream, "\n \n", index, code_name ); /* Figure out what kind of node this is. */ code = TREE_CODE (t); @@ -334,10 +481,15 @@ /* Skip the slash. */ ++filename; - dump_maybe_newline (di); - fprintf (di->stream, "srcp: %s:%-6d ", filename, - DECL_SOURCE_LINE (t)); - di->column += 6 + strlen (filename) + 8; + if (di->flags != TDF_XML) + { + dump_maybe_newline (di); + fprintf (di->stream, "srcp: %s:%-6d ", filename, + DECL_SOURCE_LINE (t)); + di->column += 6 + strlen (filename) + 8; + } + else + dump_xml_field (di, xml_field_string_type, "srcp", "%s:%d", filename, DECL_SOURCE_LINE (t)); } /* And any declaration can be compiler-generated. */ if (DECL_ARTIFICIAL (t)) @@ -351,13 +503,21 @@ int quals = (*lang_hooks.tree_dump.type_quals) (t); if (quals != TYPE_UNQUALIFIED) - { - fprintf (di->stream, "qual: %c%c%c ", - (quals & TYPE_QUAL_CONST) ? 'c' : ' ', - (quals & TYPE_QUAL_VOLATILE) ? 'v' : ' ', - (quals & TYPE_QUAL_RESTRICT) ? 'r' : ' '); - di->column += 14; - } + { + if (di->flags != TDF_XML) + { + fprintf (di->stream, "qual: %c%c%c ", + (quals & TYPE_QUAL_CONST) ? 'c' : ' ', + (quals & TYPE_QUAL_VOLATILE) ? 'v' : ' ', + (quals & TYPE_QUAL_RESTRICT) ? 'r' : ' '); + di->column += 14; + } + else + dump_xml_field (di, xml_field_string_type, "qual", "%c%c%c", + (quals & TYPE_QUAL_CONST) ? 'c' : ' ', + (quals & TYPE_QUAL_VOLATILE) ? 'v' : ' ', + (quals & TYPE_QUAL_RESTRICT) ? 'r' : ' '); + } /* All types have associated declarations. */ dump_child ("name", TYPE_NAME (t)); @@ -507,7 +667,10 @@ break; case STRING_CST: - fprintf (di->stream, "strg: %-7s ", TREE_STRING_POINTER (t)); + if (di->flags != TDF_XML) + fprintf (di->stream, "strg: %-7s ", TREE_STRING_POINTER (t)); + else + dump_xml_field (di, xml_field_string_type, "strg", "%s", TREE_STRING_POINTER (t)); dump_int (di, "lngt", TREE_STRING_LENGTH (t)); break; @@ -588,8 +751,11 @@ if (dump_flag (di, TDF_ADDRESS, NULL)) dump_pointer (di, "addr", (void *)t); - /* Terminate the line. */ - fprintf (di->stream, "\n"); + /* Terminate the xml node / line. */ + if (di->flags == TDF_XML) + fprintf (di->stream, " \n"); + else + fprintf (di->stream, "\n"); } /* Return nonzero if FLAG has been specified for the dump, and NODE @@ -627,6 +793,14 @@ di.nodes = splay_tree_new (splay_tree_compare_pointers, 0, (splay_tree_delete_value_fn) &free); + if ( di.flags == TDF_XML ) { + /* Dump XML header and root node ( open mark ) */ + fprintf( stream, "\n" ); + fprintf( stream, "\n", + XML_GXX_TU_DOCTYPE, XML_GXX_TU_DOCTYPE ); + fprintf( stream, "<%s>\n", XML_GXX_TU_DOCTYPE ); + } + /* Queue up the first node. */ queue (&di, t, DUMP_NONE); @@ -634,6 +808,10 @@ while (di.queue) dequeue_and_dump (&di); + if ( di.flags == TDF_XML ) + /* Dump the xml root node ( close mark ) */ + fprintf( stream, "\n\n", XML_GXX_TU_DOCTYPE ); + /* Now, clean up. */ for (dq = di.free_list; dq; dq = next_dq) { @@ -676,6 +854,7 @@ { {"address", TDF_ADDRESS}, {"slim", TDF_SLIM}, + {"xml", TDF_XML}, {"all", ~0}, {NULL, 0} };