From: Douglas Raillard <douglas.raillard@xxxxxxx> Allow making inner struct enums and union anonymous. Signed-off-by: Douglas Raillard <douglas.raillard@xxxxxxx> --- dwarves.h | 4 +++- dwarves_emit.c | 2 +- dwarves_fprintf.c | 52 ++++++++++++++++++++++++++++++----------------- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/dwarves.h b/dwarves.h index 30d33fa..be334cb 100644 --- a/dwarves.h +++ b/dwarves.h @@ -99,6 +99,7 @@ struct conf_fprintf { uint8_t indent; uint8_t expand_types:1; uint8_t expand_pointers:1; + uint8_t inner_anonymous:1; uint8_t rel_offset:1; uint8_t emit_stats:1; uint8_t suppress_comments:1; @@ -1327,7 +1328,8 @@ static inline const char *enumerator__name(const struct enumerator *enumerator) void enumeration__delete(struct type *type); void enumeration__add(struct type *type, struct enumerator *enumerator); size_t enumeration__fprintf(const struct tag *tag_enum, - const struct conf_fprintf *conf, FILE *fp); + const struct conf_fprintf *conf, FILE *fp, + bool anonymous); int dwarves__init(uint16_t user_cacheline_size); void dwarves__exit(void); diff --git a/dwarves_emit.c b/dwarves_emit.c index 5bf7946..5c67559 100644 --- a/dwarves_emit.c +++ b/dwarves_emit.c @@ -91,7 +91,7 @@ static int enumeration__emit_definitions(struct tag *tag, return 0; } - enumeration__fprintf(tag, conf, fp); + enumeration__fprintf(tag, conf, fp, false); fputs(";\n", fp); type_emissions__add_definition(emissions, etype); return 1; diff --git a/dwarves_fprintf.c b/dwarves_fprintf.c index c35868a..803cdab 100644 --- a/dwarves_fprintf.c +++ b/dwarves_fprintf.c @@ -186,7 +186,8 @@ size_t tag__fprintf_decl_info(const struct tag *tag, } static size_t __class__fprintf(struct class *class, const struct cu *cu, - const struct conf_fprintf *conf, FILE *fp); + const struct conf_fprintf *conf, FILE *fp, + bool anonymous); static size_t type__fprintf(struct tag *type, const struct cu *cu, const char *name, const struct conf_fprintf *conf, FILE *fp); @@ -318,7 +319,7 @@ size_t typedef__fprintf(const struct tag *tag, const struct cu *cu, struct conf_fprintf tconf = *pconf; tconf.suffix = type__name(type); - return fprintf(fp, "typedef ") + __class__fprintf(tag__class(tag_type), cu, &tconf, fp); + return fprintf(fp, "typedef ") + __class__fprintf(tag__class(tag_type), cu, &tconf, fp, false); } case DW_TAG_enumeration_type: { struct type *ctype = tag__type(tag_type); @@ -329,7 +330,7 @@ size_t typedef__fprintf(const struct tag *tag, const struct cu *cu, struct conf_fprintf tconf = *pconf; tconf.suffix = type__name(type); - return fprintf(fp, "typedef ") + enumeration__fprintf(tag_type, &tconf, fp); + return fprintf(fp, "typedef ") + enumeration__fprintf(tag_type, &tconf, fp, false); } } @@ -382,12 +383,16 @@ out: return type->max_tag_name_len; } -size_t enumeration__fprintf(const struct tag *tag, const struct conf_fprintf *conf, FILE *fp) +size_t enumeration__fprintf(const struct tag *tag, const struct conf_fprintf *conf, FILE *fp, bool anonymous) { struct type *type = tag__type(tag); struct enumerator *pos; int max_entry_name_len = enumeration__max_entry_name_len(type); - size_t printed = fprintf(fp, "enum%s%s {\n", type__name(type) ? " " : "", type__name(type) ?: ""); + size_t printed = fprintf(fp, "enum%s%s%s%s {\n", + type__name(type) ? " " : "", + type__name(type) && anonymous ? "/* " : "", + type__name(type) ? type__name(type) : "", + type__name(type) && anonymous ? " */" : ""); int indent = conf->indent; if (indent >= (int)sizeof(tabs)) @@ -622,7 +627,8 @@ static size_t type__fprintf_stats(struct type *type, const struct cu *cu, } static size_t union__fprintf(struct type *type, const struct cu *cu, - const struct conf_fprintf *conf, FILE *fp); + const struct conf_fprintf *conf, FILE *fp, + bool anonymous); static size_t type__fprintf(struct tag *type, const struct cu *cu, const char *name, const struct conf_fprintf *conf, @@ -638,6 +644,7 @@ static size_t type__fprintf(struct tag *type, const struct cu *cu, }; size_t printed = 0; int expand_types = conf->expand_types; + int inner_anonymous = conf->inner_anonymous; int suppress_offset_comment = conf->suppress_offset_comment; if (type == NULL) @@ -795,7 +802,7 @@ print_default: class__find_holes(cclass); tconf.type_spacing -= 8; - printed += __class__fprintf(cclass, cu, &tconf, fp); + printed += __class__fprintf(cclass, cu, &tconf, fp, inner_anonymous); } break; case DW_TAG_union_type: @@ -805,7 +812,7 @@ print_default: printed += fprintf(fp, "union %-*s %s", tconf.type_spacing - 6, type__name(ctype), name); } else { tconf.type_spacing -= 8; - printed += union__fprintf(ctype, cu, &tconf, fp); + printed += union__fprintf(ctype, cu, &tconf, fp, inner_anonymous); } break; case DW_TAG_enumeration_type: @@ -814,7 +821,7 @@ print_default: if (type__name(ctype) != NULL) printed += fprintf(fp, "enum %-*s %s", tconf.type_spacing - 5, type__name(ctype), name); else - printed += enumeration__fprintf(type, &tconf, fp); + printed += enumeration__fprintf(type, &tconf, fp, inner_anonymous); break; } out: @@ -968,7 +975,8 @@ static size_t union_member__fprintf(struct class_member *member, } static size_t union__fprintf(struct type *type, const struct cu *cu, - const struct conf_fprintf *conf, FILE *fp) + const struct conf_fprintf *conf, FILE *fp, + bool anonymous) { struct class_member *pos; size_t printed = 0; @@ -982,8 +990,11 @@ static size_t union__fprintf(struct type *type, const struct cu *cu, if (conf->prefix != NULL) printed += fprintf(fp, "%s ", conf->prefix); - printed += fprintf(fp, "union%s%s {\n", type__name(type) ? " " : "", - type__name(type) ?: ""); + printed += fprintf(fp, "union%s%s%s%s {\n", + type__name(type) ? " " : "", + type__name(type) && anonymous ? "/* " : "", + type__name(type) ?: "", + type__name(type) && anonymous ? " */" : ""); uconf = *conf; uconf.indent = indent + 1; @@ -1341,7 +1352,8 @@ out: } static size_t __class__fprintf(struct class *class, const struct cu *cu, - const struct conf_fprintf *conf, FILE *fp) + const struct conf_fprintf *conf, FILE *fp, + bool anonymous) { struct type *type = &class->type; size_t last_size = 0, size; @@ -1361,14 +1373,16 @@ static size_t __class__fprintf(struct class *class, const struct cu *cu, const char *current_accessibility = NULL; struct conf_fprintf cconf = conf ? *conf : conf_fprintf__defaults; const uint16_t t = type->namespace.tag.tag; - size_t printed = fprintf(fp, "%s%s%s%s%s", + size_t printed = fprintf(fp, "%s%s%s%s%s%s%s", cconf.prefix ?: "", cconf.prefix ? " " : "", ((cconf.classes_as_structs || t == DW_TAG_structure_type) ? "struct" : t == DW_TAG_class_type ? "class" : "interface"), type__name(type) ? " " : "", - type__name(type) ?: ""); + anonymous && type__name(type) ? "/* " : "", + type__name(type) ?: "", + anonymous && type__name(type) ? " */" : ""); int indent = cconf.indent; if (indent >= (int)sizeof(tabs)) @@ -1785,7 +1799,7 @@ out: size_t class__fprintf(struct class *class, const struct cu *cu, FILE *fp) { - return __class__fprintf(class, cu, NULL, fp); + return __class__fprintf(class, cu, NULL, fp, false); } static size_t variable__fprintf(const struct tag *tag, const struct cu *cu, @@ -1872,7 +1886,7 @@ size_t tag__fprintf(struct tag *tag, const struct cu *cu, printed += array_type__fprintf(tag, cu, "array", pconf, fp); break; case DW_TAG_enumeration_type: - printed += enumeration__fprintf(tag, pconf, fp); + printed += enumeration__fprintf(tag, pconf, fp, false); break; case DW_TAG_typedef: printed += typedef__fprintf(tag, cu, pconf, fp); @@ -1880,7 +1894,7 @@ size_t tag__fprintf(struct tag *tag, const struct cu *cu, case DW_TAG_class_type: case DW_TAG_interface_type: case DW_TAG_structure_type: - printed += __class__fprintf(tag__class(tag), cu, pconf, fp); + printed += __class__fprintf(tag__class(tag), cu, pconf, fp, false); break; case DW_TAG_subroutine_type: printed += ftype__fprintf(tag__ftype(tag), cu, NULL, false, false, 0, true, pconf, fp); @@ -1892,7 +1906,7 @@ size_t tag__fprintf(struct tag *tag, const struct cu *cu, printed += function__fprintf(tag, cu, pconf, fp); break; case DW_TAG_union_type: - printed += union__fprintf(tag__type(tag), cu, pconf, fp); + printed += union__fprintf(tag__type(tag), cu, pconf, fp, false); break; case DW_TAG_variable: printed += variable__fprintf(tag, cu, pconf, fp); -- 2.25.1