Skip to contentSkip to conversation

tools build: Fix a number of Wconversion warnings

PATCH Applied Ian Rogers 3 participants 6 messages Jan 6, 2025 – Feb 13, 2025 BPF

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 tools build: Fix a number of Wconversion warnings

There's some expressed interest in having the compiler flag -Wconversion detect at build time certain kinds of potential problems:
https://lore.kernel.org/lkml/[email protected]/

As feature detection passes -Wconversion from CFLAGS when set, the feature detection compile tests need to not fail because of -Wconversion as the failure will be interpretted as a missing feature. Switch various types to avoid the -Wconversion issue, the exact meaning of the code is unimportant as it is typically looking for header file definitions.

---
 tools/build/feature/test-backtrace.c           | 2 +-
 tools/build/feature/test-bpf.c                 | 2 +-
 tools/build/feature/test-glibc.c               | 2 +-
 tools/build/feature/test-libdebuginfod.c       | 2 +-
 tools/build/feature/test-libdw.c               | 2 +-
 tools/build/feature/test-libelf-gelf_getnote.c | 2 +-
 tools/build/feature/test-libelf.c              | 2 +-
 tools/build/feature/test-lzma.c                | 2 +-
 8 files changed, 8 insertions(+), 8 deletions(-)
tools/build/feature/test-backtrace.c +1 -1
index e9ddd27c69c3..7962fbad6401 100644
@@ -5,7 +5,7 @@
 int main(void)
 {
 	void *backtrace_fns[10];
-	size_t entries;
+	int entries;
 
 	entries = backtrace(backtrace_fns, 10);
 	backtrace_symbols_fd(backtrace_fns, entries, 1);
tools/build/feature/test-bpf.c +1 -1
index 727d22e34a6e..e7a405f83af6 100644
@@ -44,5 +44,5 @@ int main(void)
 	 * Test existence of __NR_bpf and BPF_PROG_LOAD.
 	 * This call should fail if we run the testcase.
 	 */
-	return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
+	return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)) == 0;
 }
tools/build/feature/test-glibc.c +1 -1
index 9ab8e90e7b88..20a250419f31 100644
@@ -16,5 +16,5 @@ int main(void)
 	const char *version = XSTR(__GLIBC__) "." XSTR(__GLIBC_MINOR__);
 #endif
 
-	return (long)version;
+	return version == NULL;
 }
tools/build/feature/test-libdebuginfod.c +1 -1
index da22548b8413..823f9fa9391d 100644
@@ -4,5 +4,5 @@
 int main(void)
 {
 	debuginfod_client* c = debuginfod_begin();
-	return (long)c;
+	return !!c;
 }
tools/build/feature/test-libdw.c +1 -1
index 2fb59479ab77..aabd63ca76b4 100644
@@ -9,7 +9,7 @@ int test_libdw(void)
 {
 	Dwarf *dbg = dwarf_begin(0, DWARF_C_READ);
 
-	return (long)dbg;
+	return dbg == NULL;
 }
 
 int test_libdw_unwind(void)
tools/build/feature/test-libelf-gelf_getnote.c +1 -1
index 075d062fe841..e06121161161 100644
@@ -4,5 +4,5 @@
 
 int main(void)
 {
-	return gelf_getnote(NULL, 0, NULL, NULL, NULL);
+	return gelf_getnote(NULL, 0, NULL, NULL, NULL) == 0;
 }
tools/build/feature/test-libelf.c +1 -1
index 905044127d56..2dbb6ea870f3 100644
@@ -5,5 +5,5 @@ int main(void)
 {
 	Elf *elf = elf_begin(0, ELF_C_READ, 0);
 
-	return (long)elf;
+	return !!elf;
 }
tools/build/feature/test-lzma.c +1 -1
index 78682bb01d57..b57103774e8e 100644
@@ -4,7 +4,7 @@
 int main(void)
 {
 	lzma_stream strm = LZMA_STREAM_INIT;
-	int ret;
+	lzma_ret ret;
 
 	ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED);
 	return ret ? -1 : 0;
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Ian Rogers

There's some expressed interest in having the compiler flag -Wconversion detect at build time certain kinds of potential problems:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

As feature detection passes -Wconversion from CFLAGS when set, the feature detection compile tests need to not fail because of -Wconversion as the failure will be interpretted as a missing feature. Switch various types to avoid the -Wconversion issue, the exact meaning of the code is unimportant as it is typically looking for header file definitions.

What's the plan for errors in #includes that we can't modify? I noticed the Perl feature test fails with -Wconversion but can be fixed by disabling the warning:

  #pragma GCC diagnostic push
  #pragma GCC diagnostic ignored "-Wsign-conversion"
  #pragma GCC diagnostic ignored "-Wconversion"
  #include <EXTERN.h>
  #include <perl.h>
  #pragma GCC diagnostic pop

Not sure why it needs both those things to be disabled when I only enabled -Wconversion, but it does.

Quoted 29 lines
---
  tools/build/feature/test-backtrace.c           | 2 +-
  tools/build/feature/test-bpf.c                 | 2 +-
  tools/build/feature/test-glibc.c               | 2 +-
  tools/build/feature/test-libdebuginfod.c       | 2 +-
  tools/build/feature/test-libdw.c               | 2 +-
  tools/build/feature/test-libelf-gelf_getnote.c | 2 +-
  tools/build/feature/test-libelf.c              | 2 +-
  tools/build/feature/test-lzma.c                | 2 +-
  8 files changed, 8 insertions(+), 8 deletions(-)
tools/build/feature/test-backtrace.c +1 -1
index e9ddd27c69c3..7962fbad6401 100644
@@ -5,7 +5,7 @@
  int main(void)
  {
  	void *backtrace_fns[10];
-	size_t entries;
+	int entries;
 
  	entries = backtrace(backtrace_fns, 10);
  	backtrace_symbols_fd(backtrace_fns, entries, 1);
tools/build/feature/test-bpf.c +0 -0
index 727d22e34a6e..e7a405f83af6 100644

tools/build/feature/test-bpf.c int main(void) · line 47

Quoted 5 lines
tools/build/feature/test-bpf.c +1 -1
@@ -44,5 +44,5 @@ int main(void)
  	 * Test existence of __NR_bpf and BPF_PROG_LOAD.
  	 * This call should fail if we run the testcase.
  	 */
-	return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
+	return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)) == 0;

Seems a bit weird to invert some of the return values rather than doing != 0, but as you say, the actual values seem to be unimportant.

Quoted 71 lines
tools/build/feature/test-bpf.c +0 -0
  }
tools/build/feature/test-glibc.c +1 -1
index 9ab8e90e7b88..20a250419f31 100644
@@ -16,5 +16,5 @@ int main(void)
  	const char *version = XSTR(__GLIBC__) "." XSTR(__GLIBC_MINOR__);
  #endif
 
-	return (long)version;
+	return version == NULL;
  }
tools/build/feature/test-libdebuginfod.c +1 -1
index da22548b8413..823f9fa9391d 100644
@@ -4,5 +4,5 @@
  int main(void)
  {
  	debuginfod_client* c = debuginfod_begin();
-	return (long)c;
+	return !!c;
  }
tools/build/feature/test-libdw.c +1 -1
index 2fb59479ab77..aabd63ca76b4 100644
@@ -9,7 +9,7 @@ int test_libdw(void)
  {
  	Dwarf *dbg = dwarf_begin(0, DWARF_C_READ);
 
-	return (long)dbg;
+	return dbg == NULL;
  }
 
  int test_libdw_unwind(void)
tools/build/feature/test-libelf-gelf_getnote.c +1 -1
index 075d062fe841..e06121161161 100644
@@ -4,5 +4,5 @@
 
  int main(void)
  {
-	return gelf_getnote(NULL, 0, NULL, NULL, NULL);
+	return gelf_getnote(NULL, 0, NULL, NULL, NULL) == 0;
  }
tools/build/feature/test-libelf.c +1 -1
index 905044127d56..2dbb6ea870f3 100644
@@ -5,5 +5,5 @@ int main(void)
  {
  	Elf *elf = elf_begin(0, ELF_C_READ, 0);
 
-	return (long)elf;
+	return !!elf;
  }
tools/build/feature/test-lzma.c +1 -1
index 78682bb01d57..b57103774e8e 100644
@@ -4,7 +4,7 @@
  int main(void)
  {
  	lzma_stream strm = LZMA_STREAM_INIT;
-	int ret;
+	lzma_ret ret;
 
  	ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED);
  	return ret ? -1 : 0;
author
#2 lore raw
Quoted 26 lines · James Clark
Quoted 4 lines · Ian Rogers

There's some expressed interest in having the compiler flag -Wconversion detect at build time certain kinds of potential problems:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

As feature detection passes -Wconversion from CFLAGS when set, the feature detection compile tests need to not fail because of -Wconversion as the failure will be interpretted as a missing feature. Switch various types to avoid the -Wconversion issue, the exact meaning of the code is unimportant as it is typically looking for header file definitions.

What's the plan for errors in #includes that we can't modify? I noticed the Perl feature test fails with -Wconversion but can be fixed by disabling the warning:

   #pragma GCC diagnostic push
   #pragma GCC diagnostic ignored "-Wsign-conversion"
   #pragma GCC diagnostic ignored "-Wconversion"
   #include <EXTERN.h>
   #include <perl.h>
   #pragma GCC diagnostic pop
Quoted 2 lines

Not sure why it needs both those things to be disabled when I only enabled -Wconversion, but it does.

This change lgtm, I'm not sure how others feel. I don't have a plan, I was just following up on Leo's Wconversion comment to see what state things were in. The feature tests without these changes pretty much break the build (I can live without perl support :-) ) so I thought I could move things forward there and then see the state of Wconversion with the patch I was working on.

I'm not sure how others feel about fixing Wconversion in perf, the errors are quite noisy imo. The biggest issue imo will be with headers shared by tools and the kernel, where kernel people may be vocal on the merits of Wconversion.

Quoted 38 lines
Quoted 29 lines
---
  tools/build/feature/test-backtrace.c           | 2 +-
  tools/build/feature/test-bpf.c                 | 2 +-
  tools/build/feature/test-glibc.c               | 2 +-
  tools/build/feature/test-libdebuginfod.c       | 2 +-
  tools/build/feature/test-libdw.c               | 2 +-
  tools/build/feature/test-libelf-gelf_getnote.c | 2 +-
  tools/build/feature/test-libelf.c              | 2 +-
  tools/build/feature/test-lzma.c                | 2 +-
  8 files changed, 8 insertions(+), 8 deletions(-)
tools/build/feature/test-backtrace.c +1 -1
index e9ddd27c69c3..7962fbad6401 100644
@@ -5,7 +5,7 @@
  int main(void)
  {
      void *backtrace_fns[10];
-     size_t entries;
+     int entries;
 
      entries = backtrace(backtrace_fns, 10);
      backtrace_symbols_fd(backtrace_fns, entries, 1);
tools/build/feature/test-bpf.c +0 -0
index 727d22e34a6e..e7a405f83af6 100644

tools/build/feature/test-bpf.c int main(void) · line 47

Quoted 5 lines
tools/build/feature/test-bpf.c +1 -1
@@ -44,5 +44,5 @@ int main(void)
       * Test existence of __NR_bpf and BPF_PROG_LOAD.
       * This call should fail if we run the testcase.
       */
-     return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
+     return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)) == 0;

Seems a bit weird to invert some of the return values rather than doing != 0, but as you say, the actual values seem to be unimportant.

Quoted 1 line

Yeah it was arbitrary and I didn't want to add a stdlib.h dependency in a bunch of places for the sake of a definition of NULL. I'm happy for things to be done differently if people like.

Thanks,
Ian

#3 lore raw
Quoted 37 lines · Ian Rogers
Quoted 26 lines · James Clark
Quoted 4 lines · Ian Rogers

There's some expressed interest in having the compiler flag -Wconversion detect at build time certain kinds of potential problems:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

As feature detection passes -Wconversion from CFLAGS when set, the feature detection compile tests need to not fail because of -Wconversion as the failure will be interpretted as a missing feature. Switch various types to avoid the -Wconversion issue, the exact meaning of the code is unimportant as it is typically looking for header file definitions.

What's the plan for errors in #includes that we can't modify? I noticed the Perl feature test fails with -Wconversion but can be fixed by disabling the warning:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wconversion" #include <EXTERN.h>
#include <perl.h>
#pragma GCC diagnostic pop

Quoted 2 lines

Not sure why it needs both those things to be disabled when I only enabled -Wconversion, but it does.

This change lgtm, I'm not sure how others feel. I don't have a plan, I was just following up on Leo's Wconversion comment to see what state things were in. The feature tests without these changes pretty much break the build (I can live without perl support :-) ) so I thought I could move things forward there and then see the state of Wconversion with the patch I was working on.

Quoted 5 lines

I'm not sure how others feel about fixing Wconversion in perf, the errors are quite noisy imo. The biggest issue imo will be with headers shared by tools and the kernel, where kernel people may be vocal on the merits of Wconversion.

More warnings are better IMO, but I can't say I have any experience with that particular one, whether it's too painful to be useful or not.

And if it's going to require those pragmas all over the place to fully enable it maybe it's better to leave it off. Just your change without the pragmas makes sense for sporadic manual testing though, and we can leave libperl disabled.

Quoted 46 lines
Quoted 38 lines
Quoted 29 lines
---
   tools/build/feature/test-backtrace.c           | 2 +-
   tools/build/feature/test-bpf.c                 | 2 +-
   tools/build/feature/test-glibc.c               | 2 +-
   tools/build/feature/test-libdebuginfod.c       | 2 +-
   tools/build/feature/test-libdw.c               | 2 +-
   tools/build/feature/test-libelf-gelf_getnote.c | 2 +-
   tools/build/feature/test-libelf.c              | 2 +-
   tools/build/feature/test-lzma.c                | 2 +-
   8 files changed, 8 insertions(+), 8 deletions(-)
tools/build/feature/test-backtrace.c +1 -1
index e9ddd27c69c3..7962fbad6401 100644
@@ -5,7 +5,7 @@
   int main(void)
   {
       void *backtrace_fns[10];
-     size_t entries;
+     int entries;
 
       entries = backtrace(backtrace_fns, 10);
       backtrace_symbols_fd(backtrace_fns, entries, 1);
tools/build/feature/test-bpf.c +0 -0
index 727d22e34a6e..e7a405f83af6 100644

tools/build/feature/test-bpf.c int main(void) · line 47

Quoted 5 lines
tools/build/feature/test-bpf.c +1 -1
@@ -44,5 +44,5 @@ int main(void)
        * Test existence of __NR_bpf and BPF_PROG_LOAD.
        * This call should fail if we run the testcase.
        */
-     return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
+     return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)) == 0;

Seems a bit weird to invert some of the return values rather than doing != 0, but as you say, the actual values seem to be unimportant.

Quoted 1 line

Yeah it was arbitrary and I didn't want to add a stdlib.h dependency in a bunch of places for the sake of a definition of NULL. I'm happy for things to be done differently if people like.

Thanks,
Ian

author Reviewed-by James Clark on v1 1/1
#4 lore raw
In reply to Ian Rogers
Quoted 5 lines · Ian Rogers

There's some expressed interest in having the compiler flag -Wconversion detect at build time certain kinds of potential problems:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

As feature detection passes -Wconversion from CFLAGS when set, the feature detection compile tests need to not fail because of -Wconversion as the failure will be interpretted as a missing feature. Switch various types to avoid the -Wconversion issue, the exact meaning of the code is unimportant as it is typically looking for header file definitions.

Ping. Also:

Would be nice to be cleaner wrt compiler warnings.

Thanks,
Ian

Applied
#5 lore raw
In reply to Ian Rogers
Quoted 4 lines · Ian Rogers

There's some expressed interest in having the compiler flag -Wconversion detect at build time certain kinds of potential problems:
https://lore.kernel.org/lkml/[email protected]/

Quoted 8 lines

As feature detection passes -Wconversion from CFLAGS when set, the feature detection compile tests need to not fail because of -Wconversion as the failure will be interpretted as a missing feature. Switch various types to avoid the -Wconversion issue, the exact meaning of the code is unimportant as it is typically looking for header file definitions.

[...]

Applied to perf-tools-next, thanks!

Best regards,
Namhyung