diff mbox series

[v2,3/7] selftests/nolibc: fix up compile warning with glibc on x86_64

Message ID aeb48b9cf6fc4674f7560166f22c7dc87d02302d.1685362482.git.falcon@tinylab.org (mailing list archive)
State Superseded
Headers show
Series nolibc: add generic part1 of prepare for rv32 | expand

Checks

Context Check Description
conchuod/tree_selection fail Failed to apply to next/pending-fixes, riscv/for-next or riscv/master

Commit Message

Zhangjin Wu May 29, 2023, 1 p.m. UTC
Compiling nolibc-test.c with gcc on x86_64 got such warning:

tools/testing/selftests/nolibc/nolibc-test.c: In function ‘expect_eq’:
tools/testing/selftests/nolibc/nolibc-test.c:177:24: warning: format ‘%lld’ expects argument of type ‘long long int’, but argument 2 has type ‘uint64_t’ {aka ‘long unsigned int’} [-Wformat=]
  177 |  llen += printf(" = %lld ", expr);
      |                     ~~~^    ~~~~
      |                        |    |
      |                        |    uint64_t {aka long unsigned int}
      |                        long long int
      |                     %ld

It because that glibc defines uint64_t as "unsigned long int" when word
size (means sizeof(long)) is 64bit (see include/bits/types.h), but
nolibc directly use the 64bit "unsigned long long" (see
tools/include/nolibc/stdint.h), which is simpler, seems kernel uses it
too (include/uapi/asm-generic/int-ll64.h).

It is able to do like glibc, defining __WORDSIZE for all of platforms
and using "unsigned long int" to define uint64_t when __WORDSIZE is
64bits, but here uses a simpler solution: nolibc always requires %lld to
match "unsigned long long", for others, only require %lld when word size
is 32bit.

Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
 tools/testing/selftests/nolibc/nolibc-test.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Willy Tarreau May 29, 2023, 1:04 p.m. UTC | #1
On Mon, May 29, 2023 at 09:00:01PM +0800, Zhangjin Wu wrote:
> Compiling nolibc-test.c with gcc on x86_64 got such warning:
> 
> tools/testing/selftests/nolibc/nolibc-test.c: In function 'expect_eq':
> tools/testing/selftests/nolibc/nolibc-test.c:177:24: warning: format '%lld' expects argument of type 'long long int', but argument 2 has type 'uint64_t' {aka 'long unsigned int'} [-Wformat=]
>   177 |  llen += printf(" = %lld ", expr);
>       |                     ~~~^    ~~~~
>       |                        |    |
>       |                        |    uint64_t {aka long unsigned int}
>       |                        long long int
>       |                     %ld
> 
> It because that glibc defines uint64_t as "unsigned long int" when word
> size (means sizeof(long)) is 64bit (see include/bits/types.h), but
> nolibc directly use the 64bit "unsigned long long" (see
> tools/include/nolibc/stdint.h), which is simpler, seems kernel uses it
> too (include/uapi/asm-generic/int-ll64.h).
> 
> It is able to do like glibc, defining __WORDSIZE for all of platforms
> and using "unsigned long int" to define uint64_t when __WORDSIZE is
> 64bits, but here uses a simpler solution: nolibc always requires %lld to
> match "unsigned long long", for others, only require %lld when word size
> is 32bit.
> 
> Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> ---
>  tools/testing/selftests/nolibc/nolibc-test.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> index d417ca5d976f..7f9b716fd9b1 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -174,7 +174,11 @@ static int expect_eq(uint64_t expr, int llen, uint64_t val)
>  {
>  	int ret = !(expr == val);
>  
> +#if __SIZEOF_LONG__ == 4 || defined(NOLIBC)
>  	llen += printf(" = %lld ", expr);
> +#else
> +	llen += printf(" = %ld ", expr);
> +#endif
>  	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
>  	return ret;
>  }

Please don't proceed like this. There's much easier to do here for a printf,
just cast the expression to the type printf expects:

-  	llen += printf(" = %lld ", expr);
+  	llen += printf(" = %lld ", (long long)expr);

Willy
Zhangjin Wu May 29, 2023, 1:15 p.m. UTC | #2
> On Mon, May 29, 2023 at 09:00:01PM +0800, Zhangjin Wu wrote:
> > Compiling nolibc-test.c with gcc on x86_64 got such warning:
> > 
> > tools/testing/selftests/nolibc/nolibc-test.c: In function 'expect_eq':
> > tools/testing/selftests/nolibc/nolibc-test.c:177:24: warning: format '%lld' expects argument of type 'long long int', but argument 2 has type 'uint64_t' {aka 'long unsigned int'} [-Wformat=]
> >   177 |  llen += printf(" = %lld ", expr);
> >       |                     ~~~^    ~~~~
> >       |                        |    |
> >       |                        |    uint64_t {aka long unsigned int}
> >       |                        long long int
> >       |                     %ld
> > 
> > It because that glibc defines uint64_t as "unsigned long int" when word
> > size (means sizeof(long)) is 64bit (see include/bits/types.h), but
> > nolibc directly use the 64bit "unsigned long long" (see
> > tools/include/nolibc/stdint.h), which is simpler, seems kernel uses it
> > too (include/uapi/asm-generic/int-ll64.h).
> > 
> > It is able to do like glibc, defining __WORDSIZE for all of platforms
> > and using "unsigned long int" to define uint64_t when __WORDSIZE is
> > 64bits, but here uses a simpler solution: nolibc always requires %lld to
> > match "unsigned long long", for others, only require %lld when word size
> > is 32bit.
> > 
> > Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
> > ---
> >  tools/testing/selftests/nolibc/nolibc-test.c | 4 ++++
> >  1 file changed, 4 insertions(+)
> > 
> > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> > index d417ca5d976f..7f9b716fd9b1 100644
> > --- a/tools/testing/selftests/nolibc/nolibc-test.c
> > +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> > @@ -174,7 +174,11 @@ static int expect_eq(uint64_t expr, int llen, uint64_t val)
> >  {
> >  	int ret = !(expr == val);
> >  
> > +#if __SIZEOF_LONG__ == 4 || defined(NOLIBC)
> >  	llen += printf(" = %lld ", expr);
> > +#else
> > +	llen += printf(" = %ld ", expr);
> > +#endif
> >  	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
> >  	return ret;
> >  }
> 
> Please don't proceed like this. There's much easier to do here for a printf,
> just cast the expression to the type printf expects:
> 
> -  	llen += printf(" = %lld ", expr);
> +  	llen += printf(" = %lld ", (long long)expr);

Yes, this conversion is better, my method make things more complex ;-)

Thanks,
Zhangjin

> 
> Willy
diff mbox series

Patch

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index d417ca5d976f..7f9b716fd9b1 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -174,7 +174,11 @@  static int expect_eq(uint64_t expr, int llen, uint64_t val)
 {
 	int ret = !(expr == val);
 
+#if __SIZEOF_LONG__ == 4 || defined(NOLIBC)
 	llen += printf(" = %lld ", expr);
+#else
+	llen += printf(" = %ld ", expr);
+#endif
 	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
 	return ret;
 }