Re: [patch 1/3] Make obconcat use stdarg

Jan Kratochvil
2010-04-30T19:07:52Z

On Fri, 30 Apr 2010 20:38:49 +0200, Tom Tromey wrote:
> I think it is better to just do the copying in a single loop.
> Obstacks already handle growing objects well enough, there's no need to
> work around this.

True, fixed.

Thanks,
Jan

2010-04-30 Jan Kratochvil <jan.kratochvil@redhat.com>

* defs.h (ATTR_SENTINEL): New.
* dwarf2read.c (typename_concat): Use NULL-terminated stdarg list for
the obconcat call.
* mdebugread.c (parse_symbol): Likewise.
* stabsread.c (define_symbol, read_member_functions, read_cpp_abbrev):
Likewise.
* symfile.c (obconcat): Replace the s1, s2 and s3 parameters by `...'.
New variable ap. Remove variables len and val.
* symfile.h (obconcat): Likewise for the prototype.

--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -304,6 +304,14 @@ struct cleanup
#endif
#endif

+#ifndef ATTR_SENTINEL
+#if defined(__GNUC__) && __GNUC__ >= 4
+#define ATTR_SENTINEL __attribute__ ((sentinel))
+#else
+#define ATTR_SENTINEL /* nothing */
+#endif
+#endif
+
/* Be conservative and use enum bitfields only with GCC.
This is copied from gcc 3.3.1, system.h. */

--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -9223,7 +9223,7 @@ typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
else
{
/* We have an obstack. */
- return obconcat (obs, prefix, sep, suffix);
+ return obconcat (obs, prefix, sep, suffix, NULL);
}
}

--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -998,8 +998,8 @@ parse_symbol (SYMR *sh, union aux_ext *ax, char *ext_sh, int bigend,
if (sh->iss == 0 || name[0] == '.' || name[0] == '\0')
TYPE_TAG_NAME (t) = NULL;
else
- TYPE_TAG_NAME (t) = obconcat (&current_objfile->objfile_obstack,
- "", "", name);
+ TYPE_TAG_NAME (t) = obconcat (&current_objfile->objfile_obstack, name,
+ NULL);

TYPE_CODE (t) = type_code;
TYPE_LENGTH (t) = sh->value;
--- a/gdb/stabsread.c
+++ b/gdb/stabsread.c
@@ -1280,9 +1280,9 @@ define_symbol (CORE_ADDR valu, char *string, int desc, int type,
SYMBOL_VALUE (struct_sym) = valu;
SYMBOL_DOMAIN (struct_sym) = STRUCT_DOMAIN;
if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
- TYPE_NAME (SYMBOL_TYPE (sym))
- = obconcat (&objfile->objfile_obstack, "", "",
- SYMBOL_LINKAGE_NAME (sym));
+ TYPE_NAME (SYMBOL_TYPE (sym)) = obconcat (&objfile->objfile_obstack,
+ SYMBOL_LINKAGE_NAME (sym),
+ NULL);
add_symbol_to_list (struct_sym, &file_symbols);
}

@@ -1307,9 +1307,9 @@ define_symbol (CORE_ADDR valu, char *string, int desc, int type,
SYMBOL_VALUE (sym) = valu;
SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
- TYPE_TAG_NAME (SYMBOL_TYPE (sym))
- = obconcat (&objfile->objfile_obstack, "", "",
- SYMBOL_LINKAGE_NAME (sym));
+ TYPE_TAG_NAME (SYMBOL_TYPE (sym)) = obconcat (&objfile->objfile_obstack,
+ SYMBOL_LINKAGE_NAME (sym),
+ NULL);
add_symbol_to_list (sym, &file_symbols);

if (synonym)
@@ -1322,9 +1322,9 @@ define_symbol (CORE_ADDR valu, char *string, int desc, int type,
SYMBOL_VALUE (typedef_sym) = valu;
SYMBOL_DOMAIN (typedef_sym) = VAR_DOMAIN;
if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
- TYPE_NAME (SYMBOL_TYPE (sym))
- = obconcat (&objfile->objfile_obstack, "", "",
- SYMBOL_LINKAGE_NAME (sym));
+ TYPE_NAME (SYMBOL_TYPE (sym)) = obconcat (&objfile->objfile_obstack,
+ SYMBOL_LINKAGE_NAME (sym),
+ NULL);
add_symbol_to_list (typedef_sym, &file_symbols);
}
break;
@@ -2610,8 +2610,8 @@ read_member_functions (struct field_info *fip, char **pp, struct type *type,
make_cleanup (xfree, destr_fnlist);
memset (destr_fnlist, 0, sizeof (struct next_fnfieldlist));
destr_fnlist->fn_fieldlist.name
- = obconcat (&objfile->objfile_obstack, "", "~",
- new_fnlist->fn_fieldlist.name);
+ = obconcat (&objfile->objfile_obstack, "~",
+ new_fnlist->fn_fieldlist.name, NULL);

destr_fnlist->fn_fieldlist.fn_fields = (struct fn_field *)
obstack_alloc (&objfile->objfile_obstack,
@@ -2748,8 +2748,8 @@ read_cpp_abbrev (struct field_info *fip, char **pp, struct type *type,
{
name = "";
}
- fip->list->field.name =
- obconcat (&objfile->objfile_obstack, vptr_name, name, "");
+ fip->list->field.name = obconcat (&objfile->objfile_obstack,
+ vptr_name, name, NULL);
break;

case 'b': /* $vb -- a virtual bsomethingorother */
@@ -2761,15 +2761,14 @@ read_cpp_abbrev (struct field_info *fip, char **pp, struct type *type,
symnum);
name = "FOO";
}
- fip->list->field.name =
- obconcat (&objfile->objfile_obstack, vb_name, name, "");
+ fip->list->field.name = obconcat (&objfile->objfile_obstack, vb_name,
+ name, NULL);
break;

default:
invalid_cpp_abbrev_complaint (*pp);
- fip->list->field.name =
- obconcat (&objfile->objfile_obstack,
- "INVALID_CPLUSPLUS_ABBREV", "", "");
+ fip->list->field.name = obconcat (&objfile->objfile_obstack,
+ "INVALID_CPLUSPLUS_ABBREV", NULL);
break;
}

--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -212,19 +212,28 @@ obsavestring (const char *ptr, int size, struct obstack *obstackp)
return p;
}

-/* Concatenate strings S1, S2 and S3; return the new string. Space is found
- in the obstack pointed to by OBSTACKP. */
+/* Concatenate NULL terminated variable argument list of `const char *' strings;
+ return the new string. Space is found in the OBSTACKP */

char *
-obconcat (struct obstack *obstackp, const char *s1, const char *s2,
- const char *s3)
-{
- int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
- char *val = (char *) obstack_alloc (obstackp, len);
- strcpy (val, s1);
- strcat (val, s2);
- strcat (val, s3);
- return val;
+obconcat (struct obstack *obstackp, ...)
+{
+ va_list ap;
+
+ va_start (ap, obstackp);
+ for (;;)
+ {
+ const char *s = va_arg (ap, const char *);
+
+ if (s == NULL)
+ break;
+
+ obstack_grow_str (obstackp, s);
+ }
+ va_end (ap);
+ obstack_1grow (obstackp, 0);
+
+ return obstack_finish (obstackp);
}

/* True if we are reading a symbol table. */
--- a/gdb/symfile.h
+++ b/gdb/symfile.h
@@ -458,11 +458,10 @@ extern struct partial_symtab *start_psymtab_common (struct objfile *,

extern char *obsavestring (const char *, int, struct obstack *);

-/* Concatenate strings S1, S2 and S3; return the new string. Space is
- found in the OBSTACKP */
+/* Concatenate NULL terminated variable argument list of `const char *' strings;
+ return the new string. Space is found in the OBSTACKP */

-extern char *obconcat (struct obstack *obstackp, const char *, const char *,
- const char *);
+extern char *obconcat (struct obstack *obstackp, ...) ATTR_SENTINEL;

/* Variables */