nano printf + powerpc gcc

Message ID CAN8C2CpXVH8iq=9XMV0JxWPJihRi_vHDwHwMC830=YCxV-ZjNQ@mail.gmail.com
State New
Headers show
Series
  • nano printf + powerpc gcc
Related show

Commit Message

Alexander Fedotov Dec. 25, 2017, 1:39 p.m.
Hi

I'm experienced a strange printf() for float variables behaviour with
Nano version while regular is fine. It just prins out a garbage (even
with enabled -u _printf_float). After some digging I've managed to
track down issue until this line:

n = _printf_float (data, &prt_data, fp, pfunc, &ap);


Moreover there are number of warnings also in build log:

src_newlib/newlib/libc/stdio/nano-vfprintf.c -o lib_a-nano-svfprintf.o
../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf.c: In
function '_svfprintf_r':
../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf.c:645:55:
warning: passing argument 5 of '_printf_float' from incompatible
pointer type
        n = _printf_float (data, &prt_data, fp, pfunc, &ap);
                                                                       ^
In file included from
../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf.c:169:0:
../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf_local.h:228:1:
note: expected 'struct __va_list_tag (*)[1]' but argument is of type
'struct __va_list_tag **'
 _printf_float (struct _reent *data,
 ^
../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf.c:650:45:
warning: passing argument 5 of '_printf_i' from incompatible pointer
type
  n = _printf_i (data, &prt_data, fp, pfunc, &ap);
                                                            ^
In file included from
../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf.c:169:0:
../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf_local.h:221:1:
note: expected 'struct __va_list_tag (*)[1]' but argument is of type
'struct __va_list_tag **'
 _printf_i (struct _reent *data, struct _prt_data_t *pdata, FILE *fp,


It looks like nano written without taking in mind such targets. Here I
found workaround https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14557
that's working.

I'm attaching a patch.

Alex

Comments

Corinna Vinschen Jan. 8, 2018, 10:07 a.m. | #1
On Dec 25 16:39, Alexander Fedotov wrote:
> Hi

> 

> I'm experienced a strange printf() for float variables behaviour with

> Nano version while regular is fine. It just prins out a garbage (even

> with enabled -u _printf_float). After some digging I've managed to

> track down issue until this line:

> 

> n = _printf_float (data, &prt_data, fp, pfunc, &ap);

> 

> 

> Moreover there are number of warnings also in build log:

> 

> src_newlib/newlib/libc/stdio/nano-vfprintf.c -o lib_a-nano-svfprintf.o

> ../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf.c: In

> function '_svfprintf_r':

> ../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf.c:645:55:

> warning: passing argument 5 of '_printf_float' from incompatible

> pointer type

>         n = _printf_float (data, &prt_data, fp, pfunc, &ap);

>                                                                        ^

> In file included from

> ../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf.c:169:0:

> ../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf_local.h:228:1:

> note: expected 'struct __va_list_tag (*)[1]' but argument is of type

> 'struct __va_list_tag **'

>  _printf_float (struct _reent *data,

>  ^

> ../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf.c:650:45:

> warning: passing argument 5 of '_printf_i' from incompatible pointer

> type

>   n = _printf_i (data, &prt_data, fp, pfunc, &ap);

>                                                             ^

> In file included from

> ../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf.c:169:0:

> ../../../../../../src_newlib/newlib/libc/stdio/nano-vfprintf_local.h:221:1:

> note: expected 'struct __va_list_tag (*)[1]' but argument is of type

> 'struct __va_list_tag **'

>  _printf_i (struct _reent *data, struct _prt_data_t *pdata, FILE *fp,

> 

> 

> It looks like nano written without taking in mind such targets. Here I

> found workaround https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14557

> that's working.

> 

> I'm attaching a patch.

> 

> Alex


Pushed.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat

Patch

From 47ee6b7a46e6331ae0840a2262258d2c868ccbc5 Mon Sep 17 00:00:00 2001
From: Alexander Fedotov <alfedotov@gmail.com>
Date: Mon, 25 Dec 2017 16:28:22 +0300
Subject: [PATCH] fix incompatible pointer type for va_list in nano versions of
 printf and scanf for target like PowerPC

---
 newlib/libc/stdio/nano-vfprintf.c | 14 ++++++++++++--
 newlib/libc/stdio/nano-vfscanf.c  | 15 ++++++++++++---
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/newlib/libc/stdio/nano-vfprintf.c b/newlib/libc/stdio/nano-vfprintf.c
index e6604e7..663eb71 100644
--- a/newlib/libc/stdio/nano-vfprintf.c
+++ b/newlib/libc/stdio/nano-vfprintf.c
@@ -168,6 +168,16 @@  static char *rcsid = "$Id$";
 #include "vfieeefp.h"
 #include "nano-vfprintf_local.h"
 
+
+/* GCC PR 14577 at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14557 */
+#if __STDC_VERSION__ >= 201112L
+#define va_ptr(ap) _Generic(&(ap), va_list *: &(ap), default: (va_list *)(ap))
+#elif __GNUC__ >= 4
+#define va_ptr(ap) __builtin_choose_expr(__builtin_types_compatible_p(__typeof__(&(ap)), va_list *), &(ap), (va_list *)(ap))
+#else
+#define va_ptr(ap) (sizeof(ap) == sizeof(va_list) ? (va_list *)&(ap) : (va_list *)(ap))
+#endif
+
 /* The __ssputs_r function is shared between all versions of vfprintf
    and vfwprintf.  */
 #ifdef STRING_ONLY
@@ -633,12 +643,12 @@  _DEFUN(_VFPRINTF_R, (data, fp, fmt0, ap),
 	    }
 	  else
 	    {
-	      n = _printf_float (data, &prt_data, fp, pfunc, &ap);
+	      n = _printf_float (data, &prt_data, fp, pfunc, va_ptr(ap));
 	    }
 	}
       else
 #endif
-	n = _printf_i (data, &prt_data, fp, pfunc, &ap);
+	n = _printf_i (data, &prt_data, fp, pfunc, va_ptr(ap));
 
       if (n == -1)
 	goto error;
diff --git a/newlib/libc/stdio/nano-vfscanf.c b/newlib/libc/stdio/nano-vfscanf.c
index 564f291..6467e54 100644
--- a/newlib/libc/stdio/nano-vfscanf.c
+++ b/newlib/libc/stdio/nano-vfscanf.c
@@ -119,6 +119,15 @@  Supporting OS subroutines required:
 #include "../stdlib/local.h"
 #include "nano-vfscanf_local.h"
 
+/* GCC PR 14577 at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14557 */
+#if __STDC_VERSION__ >= 201112L
+#define va_ptr(ap) _Generic(&(ap), va_list *: &(ap), default: (va_list *)(ap))
+#elif __GNUC__ >= 4
+#define va_ptr(ap) __builtin_choose_expr(__builtin_types_compatible_p(__typeof__(&(ap)), va_list *), &(ap), (va_list *)(ap))
+#else
+#define va_ptr(ap) (sizeof(ap) == sizeof(va_list) ? (va_list *)&(ap) : (va_list *)(ap))
+#endif
+
 #define VFSCANF vfscanf
 #define _VFSCANF_R _vfscanf_r
 #define __SVFSCANF __svfscanf
@@ -424,12 +433,12 @@  _DEFUN(__SVFSCANF_R, (rptr, fp, fmt0, ap),
 	}
       ret = 0;
       if (scan_data.code < CT_INT)
-	ret = _scanf_chars (rptr, &scan_data, fp, &ap);
+	ret = _scanf_chars (rptr, &scan_data, fp, va_ptr(ap));
       else if (scan_data.code < CT_FLOAT)
-	ret = _scanf_i (rptr, &scan_data, fp, &ap);
+	ret = _scanf_i (rptr, &scan_data, fp, va_ptr(ap));
 #ifdef FLOATING_POINT
       else if (_scanf_float)
-	ret = _scanf_float (rptr, &scan_data, fp, &ap);
+	ret = _scanf_float (rptr, &scan_data, fp, va_ptr(ap));
 #endif
 
       if (ret == MATCH_FAILURE)
-- 
2.7.4