This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: implement fdiagnostics-print-source-range-info


2009/8/5 Joseph S. Myers <joseph@codesourcery.com>:
>
> Fail the same way as xmalloc. ?The right way is probably to add xasprintf
> and xvasprintf functions to libiberty (gengtype has its own xasprintf
> function; gnulib has xasprintf and xvasprintf so there's precedent for
> those names) and make the existing callers use them.

I implemented xasprintf and xvasprintf within asprintf.c and
vasprintf.c, however, I am afraid the attached patch is not enough,
since it seems in x86_64-linux-gnu those files are not actually
compiled into libiberty.a.Probably I have to modify some configure or
Makefile magic stuff. Any hints on this?

Cheers,

Manuel.
Index: vasprintf.c
===================================================================
--- vasprintf.c	(revision 150311)
+++ vasprintf.c	(working copy)
@@ -40,30 +40,14 @@ extern PTR malloc ();
 
 #ifdef TEST
 int global_total_width;
 #endif
 
-/*
-
-@deftypefn Extension int vasprintf (char **@var{resptr}, const char *@var{format}, va_list @var{args})
-
-Like @code{vsprintf}, but instead of passing a pointer to a buffer,
-you pass a pointer to a pointer.  This function will compute the size
-of the buffer needed, allocate memory with @code{malloc}, and store a
-pointer to the allocated memory in @code{*@var{resptr}}.  The value
-returned is the same as @code{vsprintf} would return.  If memory could
-not be allocated, minus one is returned and @code{NULL} is stored in
-@code{*@var{resptr}}.
-
-@end deftypefn
-
-*/
-
-static int int_vasprintf (char **, const char *, va_list);
+static int total_width_asprintf (const char *, va_list);
 
 static int
-int_vasprintf (char **result, const char *format, va_list args)
+total_width_asprintf (const char *format, va_list args)
 {
   const char *p = format;
   /* Add one to make sure that it is never zero, which might cause malloc
      to return NULL.  */
   int total_width = strlen (format) + 1;
@@ -139,17 +123,42 @@ int_vasprintf (char **result, const char
   va_end (ap);
 #endif
 #ifdef TEST
   global_total_width = total_width;
 #endif
+  return total_width;
+}
+
+static int int_vasprintf (char **, const char *, va_list);
+
+static int
+int_vasprintf (char **result, const char *format, va_list args)
+{
+  int total_width = total_width_asprintf (format, args);
   *result = (char *) malloc (total_width);
   if (*result != NULL)
     return vsprintf (*result, format, args);
   else
     return -1;
 }
 
+/*
+
+@deftypefn Extension int vasprintf (char **@var{resptr}, const char *@var{format}, va_list @var{args})
+
+Like @code{vsprintf}, but instead of passing a pointer to a buffer,
+you pass a pointer to a pointer.  This function will compute the size
+of the buffer needed, allocate memory with @code{malloc}, and store a
+pointer to the allocated memory in @code{*@var{resptr}}.  The value
+returned is the same as @code{vsprintf} would return.  If memory could
+not be allocated, minus one is returned and @code{NULL} is stored in
+@code{*@var{resptr}}.
+
+@end deftypefn
+
+*/
+
 int
 vasprintf (char **result, const char *format,
 #if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
            _BSD_VA_LIST_ args)
 #else
@@ -157,10 +166,45 @@ vasprintf (char **result, const char *fo
 #endif
 {
   return int_vasprintf (result, format, args);
 }
 
+static char * int_xvasprintf (const char *, va_list);
+
+static char *
+int_xvasprintf (const char *format, va_list args)
+{
+  char * result;
+  int total_width = total_width_asprintf (format, args);
+  result = (char *) xmalloc (total_width);
+  vsprintf (result, format, args);
+  return result;
+}
+
+/*
+
+@deftypefn Replacement char * xvasprintf (const char *@var{format}, va_list @var{args})
+
+Print to allocated string without fail.  If @code{vasprintf} fails,
+this will print a message to @code{stderr} (using the name set by
+@code{xmalloc_set_program_name}, if any) and then call @code{xexit}.
+
+@end deftypefn
+
+*/
+
+char *
+xvasprintf (const char *format,
+#if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
+           _BSD_VA_LIST_ args)
+#else
+           va_list args)
+#endif
+{
+  return int_xvasprintf (format, args);
+}
+
 #ifdef TEST
 static void ATTRIBUTE_PRINTF_1
 checkit (const char *format, ...)
 {
   char *result;
Index: asprintf.c
===================================================================
--- asprintf.c	(revision 150311)
+++ asprintf.c	(working copy)
@@ -52,5 +52,29 @@ asprintf (char **buf, const char *fmt, .
   VA_FIXEDARG (ap, const char *, fmt);
   status = vasprintf (buf, fmt, ap);
   VA_CLOSE (ap);
   return status;
 }
+
+/*
+
+@deftypefn Replacement char * xasprintf (const char *@var{format}, ...)
+
+Print to allocated string without fail.  If @code{asprintf} fails,
+this will print a message to @code{stderr} (using the name set by
+@code{xmalloc_set_program_name}, if any) and then call @code{xexit}.
+
+@end deftypefn
+
+*/
+
+char *
+xasprintf (const char *fmt, ...)
+{
+  char *buf;
+  int status;
+  VA_OPEN (ap, fmt);
+  VA_FIXEDARG (ap, const char *, fmt);
+  buf = xvasprintf (fmt, ap);
+  VA_CLOSE (ap);
+  return buf;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]