All of lore.kernel.org
 help / color / mirror / Atom feed
From: Willy Tarreau <w@1wt.eu>
To: Zhangjin Wu <falcon@tinylab.org>
Cc: arnd@arndb.de, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org,
	thomas@t-8ch.de
Subject: Re: [PATCH v2 3/7] selftests/nolibc: fix up compile warning with glibc on x86_64
Date: Mon, 29 May 2023 15:04:49 +0200	[thread overview]
Message-ID: <20230529130449.GA2813@1wt.eu> (raw)
In-Reply-To: <aeb48b9cf6fc4674f7560166f22c7dc87d02302d.1685362482.git.falcon@tinylab.org>

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

WARNING: multiple messages have this Message-ID (diff)
From: Willy Tarreau <w@1wt.eu>
To: Zhangjin Wu <falcon@tinylab.org>
Cc: arnd@arndb.de, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org,
	thomas@t-8ch.de
Subject: Re: [PATCH v2 3/7] selftests/nolibc: fix up compile warning with glibc on x86_64
Date: Mon, 29 May 2023 15:04:49 +0200	[thread overview]
Message-ID: <20230529130449.GA2813@1wt.eu> (raw)
In-Reply-To: <aeb48b9cf6fc4674f7560166f22c7dc87d02302d.1685362482.git.falcon@tinylab.org>

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

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  reply	other threads:[~2023-05-29 13:06 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-29 12:52 [PATCH v2 0/7] nolibc: add generic part1 of prepare for rv32 Zhangjin Wu
2023-05-29 12:52 ` Zhangjin Wu
2023-05-29 12:54 ` [PATCH v2 1/7] selftests/nolibc: syscall_args: use generic __NR_statx Zhangjin Wu
2023-05-29 12:54   ` Zhangjin Wu
2023-05-29 12:56 ` [PATCH v2 2/7] selftests/nolibc: allow specify extra arguments for qemu Zhangjin Wu
2023-05-29 12:56   ` Zhangjin Wu
2023-05-29 13:00 ` [PATCH v2 3/7] selftests/nolibc: fix up compile warning with glibc on x86_64 Zhangjin Wu
2023-05-29 13:00   ` Zhangjin Wu
2023-05-29 13:04   ` Willy Tarreau [this message]
2023-05-29 13:04     ` Willy Tarreau
2023-05-29 13:15     ` Zhangjin Wu
2023-05-29 13:15       ` Zhangjin Wu
2023-05-29 13:02 ` [PATCH v2 4/7] selftests/nolibc: not include limits.h for nolibc Zhangjin Wu
2023-05-29 13:02   ` Zhangjin Wu
2023-05-29 13:04 ` [PATCH v2 5/7] selftests/nolibc: use INT_MAX instead of __INT_MAX__ Zhangjin Wu
2023-05-29 13:04   ` Zhangjin Wu
2023-05-29 13:07 ` [PATCH v2 6/7] tools/nolibc: arm: add missing my_syscall6 Zhangjin Wu
2023-05-29 13:07   ` Zhangjin Wu
2023-05-29 13:09 ` [PATCH v2 7/7] tools/nolibc: open: fix up compile warning for arm Zhangjin Wu
2023-05-29 13:09   ` Zhangjin Wu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230529130449.GA2813@1wt.eu \
    --to=w@1wt.eu \
    --cc=arnd@arndb.de \
    --cc=falcon@tinylab.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=thomas@t-8ch.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.