From 264d8884d5d3c6ee4cfd5df5650293d841364166 Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Sat, 2 Nov 2019 13:34:01 +0100 Subject: [PATCH] widl: Add strmake() helper. Signed-off-by: Alexandre Julliard --- tools/widl/parser.y | 24 ++++-------------------- tools/widl/register.c | 3 +-- tools/widl/utils.c | 19 +++++++++++++++++++ tools/widl/utils.h | 1 + 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/tools/widl/parser.y b/tools/widl/parser.y index 59875a5b160..5f6eb508780 100644 --- a/tools/widl/parser.y +++ b/tools/widl/parser.y @@ -2038,11 +2038,8 @@ var_t *find_const(const char *name, int f) char *gen_name(void) { - static const char format[] = "__WIDL_%s_generated_name_%08lX"; static unsigned long n = 0; static const char *file_id; - static size_t size; - char *name; if (! file_id) { @@ -2052,13 +2049,8 @@ char *gen_name(void) for (; *dst; ++dst) if (! isalnum((unsigned char) *dst)) *dst = '_'; - - size = sizeof format - 7 + strlen(file_id) + 8; } - - name = xmalloc(size); - sprintf(name, format, file_id, n++); - return name; + return strmake("__WIDL_%s_generated_name_%08lX", file_id, n++); } struct allowed_attr @@ -2727,14 +2719,6 @@ static void check_functions(const type_t *iface, int is_inside_library) } } -static char *concat_str(const char *prefix, const char *str) -{ - char *ret = xmalloc(strlen(prefix) + strlen(str) + 1); - strcpy(ret, prefix); - strcat(ret, str); - return ret; -} - static int async_iface_attrs(attr_list_t *attrs, const attr_t *attr) { switch(attr->type) @@ -2774,7 +2758,7 @@ static void check_async_uuid(type_t *iface) if (!inherit) error_loc("async_uuid applied to an interface with incompatible parent\n"); - async_iface = get_type(TYPE_INTERFACE, concat_str("Async", iface->name), iface->namespace, 0); + async_iface = get_type(TYPE_INTERFACE, strmake("Async%s", iface->name), iface->namespace, 0); async_iface->attrs = map_attrs(iface->attrs, async_iface_attrs); STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) ) @@ -2791,13 +2775,13 @@ static void check_async_uuid(type_t *iface) finish_args = append_var(finish_args, copy_var(arg, strdup(arg->name), arg_out_attrs)); } - begin_func = copy_var(func, concat_str("Begin_", func->name), NULL); + begin_func = copy_var(func, strmake("Begin_%s", func->name), NULL); begin_func->declspec.type = type_new_function(begin_args); begin_func->declspec.type->attrs = func->attrs; begin_func->declspec.type->details.function->retval = func->declspec.type->details.function->retval; stmts = append_statement(stmts, make_statement_declaration(begin_func)); - finish_func = copy_var(func, concat_str("Finish_", func->name), NULL); + finish_func = copy_var(func, strmake("Finish_%s", func->name), NULL); finish_func->declspec.type = type_new_function(finish_args); finish_func->declspec.type->attrs = func->attrs; finish_func->declspec.type->details.function->retval = func->declspec.type->details.function->retval; diff --git a/tools/widl/register.c b/tools/widl/register.c index 1c369e7c5ee..f65cbddf531 100644 --- a/tools/widl/register.c +++ b/tools/widl/register.c @@ -321,8 +321,7 @@ void output_typelib_regscript( const typelib_t *typelib ) if (expr) { sprintf(id_part, "\\%d", expr->cval); - resname = xmalloc( strlen(typelib_name) + 20 ); - sprintf(resname, "%s\\%d", typelib_name, expr->cval); + resname = strmake("%s\\%d", typelib_name, expr->cval); } put_str( indent, "'%x' { %s = s '%%MODULE%%%s' }\n", lcid_expr ? lcid_expr->cval : 0, pointer_size == 8 ? "win64" : "win32", id_part ); diff --git a/tools/widl/utils.c b/tools/widl/utils.c index 20c63064c7a..ea92372c8c7 100644 --- a/tools/widl/utils.c +++ b/tools/widl/utils.c @@ -226,6 +226,25 @@ void *xrealloc(void *p, size_t size) return res; } +char *strmake( const char* fmt, ... ) +{ + int n; + size_t size = 100; + va_list ap; + + for (;;) + { + char *p = xmalloc( size ); + va_start( ap, fmt ); + n = vsnprintf( p, size, fmt, ap ); + va_end( ap ); + if (n == -1) size *= 2; + else if ((size_t)n >= size) size = n + 1; + else return p; + free( p ); + } +} + char *xstrdup(const char *str) { char *s; diff --git a/tools/widl/utils.h b/tools/widl/utils.h index 74dba11d3c6..37406656504 100644 --- a/tools/widl/utils.h +++ b/tools/widl/utils.h @@ -44,6 +44,7 @@ void error_loc_info(const loc_info_t *, const char *s, ...) __attribute__((forma void warning(const char *s, ...) __attribute__((format (printf, 1, 2))); void warning_loc_info(const loc_info_t *, const char *s, ...) __attribute__((format (printf, 2, 3))); void chat(const char *s, ...) __attribute__((format (printf, 1, 2))); +char *strmake(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2 ))); char *dup_basename(const char *name, const char *ext); size_t widl_getline(char **linep, size_t *lenp, FILE *fp);