[V4] Add warning options -W[no-]compare-distinct-pointer-types

Message ID 20230817140513.26888-1-jose.marchesi@oracle.com
State Accepted
Headers
Series [V4] Add warning options -W[no-]compare-distinct-pointer-types |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Jose E. Marchesi Aug. 17, 2023, 2:05 p.m. UTC
  [Changes from V3:
- Previous thread:
  https://gcc.gnu.org/pipermail/gcc-patches/2022-August/600625.html
- The tests have been augmented to check all six relational
  operators.  In particular it covers both code paths impacted
  by the patch: the equality/inequality and the relational ops.]

GCC emits pedwarns unconditionally when comparing pointers of
different types, for example:

  int xdp_context (struct xdp_md *xdp)
    {
        void *data = (void *)(long)xdp->data;
        __u32 *metadata = (void *)(long)xdp->data_meta;
        __u32 ret;

        if (metadata + 1 > data)
          return 0;
        return 1;
   }

  /home/jemarch/foo.c: In function ‘xdp_context’:
  /home/jemarch/foo.c:15:20: warning: comparison of distinct pointer types lacks a cast
         15 |   if (metadata + 1 > data)
                 |                    ^

LLVM supports an option -W[no-]compare-distinct-pointer-types that can
be used in order to enable or disable the emission of such warnings.
It is enabled by default.

This patch adds the same options to GCC.

Documentation and testsuite updated included.
Regtested in x86_64-linu-gnu.
No regressions observed.

gcc/ChangeLog:

	PR c/106537
	* doc/invoke.texi (Option Summary): Mention
	-Wcompare-distinct-pointer-types under `Warning Options'.
	(Warning Options): Document -Wcompare-distinct-pointer-types.

gcc/c-family/ChangeLog:

	PR c/106537
	* c.opt (Wcompare-distinct-pointer-types): New option.

gcc/c/ChangeLog:

	PR c/106537
	* c-typeck.cc (build_binary_op): Warning on comparing distinct
	pointer types only when -Wcompare-distinct-pointer-types.

gcc/testsuite/ChangeLog:

	PR c/106537
	* gcc.c-torture/compile/pr106537-1.c: New test.
	* gcc.c-torture/compile/pr106537-2.c: Likewise.
	* gcc.c-torture/compile/pr106537-3.c: Likewise.
---
 gcc/c-family/c.opt                            |  4 +++
 gcc/c/c-typeck.cc                             |  6 ++--
 gcc/doc/invoke.texi                           |  6 ++++
 .../gcc.c-torture/compile/pr106537-1.c        | 34 +++++++++++++++++++
 .../gcc.c-torture/compile/pr106537-2.c        | 32 +++++++++++++++++
 .../gcc.c-torture/compile/pr106537-3.c        | 32 +++++++++++++++++
 6 files changed, 111 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr106537-1.c
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr106537-2.c
 create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr106537-3.c
  

Comments

Joseph Myers Aug. 17, 2023, 3:23 p.m. UTC | #1
On Thu, 17 Aug 2023, Jose E. Marchesi via Gcc-patches wrote:

> +@opindex Wcompare-distinct-pointer-types
> +@item -Wcompare-distinct-pointer-types

This @item should say @r{(C and Objective-C only)}, since the option isn't 
implemented for C++.  OK with that change.
  
Jose E. Marchesi Aug. 17, 2023, 3:37 p.m. UTC | #2
> On Thu, 17 Aug 2023, Jose E. Marchesi via Gcc-patches wrote:
>
>> +@opindex Wcompare-distinct-pointer-types
>> +@item -Wcompare-distinct-pointer-types
>
> This @item should say @r{(C and Objective-C only)}, since the option isn't 
> implemented for C++.  OK with that change.

Pushed with that change.
Thanks for the prompt review!
  
Marek Polacek Aug. 23, 2023, 6:12 p.m. UTC | #3
On Thu, Aug 17, 2023 at 05:37:03PM +0200, Jose E. Marchesi via Gcc-patches wrote:
> 
> > On Thu, 17 Aug 2023, Jose E. Marchesi via Gcc-patches wrote:
> >
> >> +@opindex Wcompare-distinct-pointer-types
> >> +@item -Wcompare-distinct-pointer-types
> >
> > This @item should say @r{(C and Objective-C only)}, since the option isn't 
> > implemented for C++.  OK with that change.
> 
> Pushed with that change.
> Thanks for the prompt review!

I see the following failures:

FAIL: gcc.c-torture/compile/pr106537-1.c   -Os   (test for warnings, line 28)
FAIL: gcc.c-torture/compile/pr106537-1.c   -Os   (test for warnings, line 30)
FAIL: gcc.c-torture/compile/pr106537-1.c   -O2 -flto -fno-use-linker-plugin -flto-partition=none   (test for warnings, line 28)
FAIL: gcc.c-torture/compile/pr106537-1.c   -O2 -flto -fno-use-linker-plugin -flto-partition=none   (test for warnings, line 30)
FAIL: gcc.c-torture/compile/pr106537-1.c   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects   (test for warnings, line 28)
FAIL: gcc.c-torture/compile/pr106537-1.c   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects   (test for warnings, line 30)
FAIL: gcc.c-torture/compile/pr106537-2.c   -O0   (test for warnings, line 26)
FAIL: gcc.c-torture/compile/pr106537-2.c   -O0   (test for warnings, line 28)
FAIL: gcc.c-torture/compile/pr106537-2.c   -O1   (test for warnings, line 26)
FAIL: gcc.c-torture/compile/pr106537-2.c   -O1   (test for warnings, line 28)
FAIL: gcc.c-torture/compile/pr106537-2.c   -O2   (test for warnings, line 26)
FAIL: gcc.c-torture/compile/pr106537-2.c   -O2   (test for warnings, line 28)
FAIL: gcc.c-torture/compile/pr106537-2.c   -O3 -g   (test for warnings, line 26)
FAIL: gcc.c-torture/compile/pr106537-2.c   -O3 -g   (test for warnings, line 28)
FAIL: gcc.c-torture/compile/pr106537-2.c   -Os   (test for warnings, line 26)
FAIL: gcc.c-torture/compile/pr106537-2.c   -Os   (test for warnings, line 28)
FAIL: gcc.c-torture/compile/pr106537-2.c   -O2 -flto -fno-use-linker-plugin -flto-partition=none   (test for warnings, line 26)
FAIL: gcc.c-torture/compile/pr106537-2.c   -O2 -flto -fno-use-linker-plugin -flto-partition=none   (test for warnings, line 28)
FAIL: gcc.c-torture/compile/pr106537-2.c   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects   (test for warnings, line 26)
FAIL: gcc.c-torture/compile/pr106537-2.c   -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects   (test for warnings, line 28)

The problem is that for ==/!=, when one of the types is void*,
build_binary_op goes to the branch attempting to warn about
comparing void* with a function pointer, and never gets to the 
-Wcompare-distinct-pointer-types warning.

Marek
  
Jose E. Marchesi Aug. 24, 2023, 3:18 p.m. UTC | #4
Hi Marek.

> On Thu, Aug 17, 2023 at 05:37:03PM +0200, Jose E. Marchesi via Gcc-patches wrote:
>> 
>> > On Thu, 17 Aug 2023, Jose E. Marchesi via Gcc-patches wrote:
>> >
>> >> +@opindex Wcompare-distinct-pointer-types
>> >> +@item -Wcompare-distinct-pointer-types
>> >
>> > This @item should say @r{(C and Objective-C only)}, since the option isn't 
>> > implemented for C++.  OK with that change.
>> 
>> Pushed with that change.
>> Thanks for the prompt review!
>
> I see the following failures:
>
> FAIL: gcc.c-torture/compile/pr106537-1.c   -Os   (test for warnings, line 28)
> FAIL: gcc.c-torture/compile/pr106537-1.c   -Os   (test for warnings, line 30)
> FAIL: gcc.c-torture/compile/pr106537-1.c -O2 -flto
> -fno-use-linker-plugin -flto-partition=none (test for warnings, line
> 28)
> FAIL: gcc.c-torture/compile/pr106537-1.c -O2 -flto
> -fno-use-linker-plugin -flto-partition=none (test for warnings, line
> 30)
> FAIL: gcc.c-torture/compile/pr106537-1.c -O2 -flto -fuse-linker-plugin
> -fno-fat-lto-objects (test for warnings, line 28)
> FAIL: gcc.c-torture/compile/pr106537-1.c -O2 -flto -fuse-linker-plugin
> -fno-fat-lto-objects (test for warnings, line 30)
> FAIL: gcc.c-torture/compile/pr106537-2.c   -O0   (test for warnings, line 26)
> FAIL: gcc.c-torture/compile/pr106537-2.c   -O0   (test for warnings, line 28)
> FAIL: gcc.c-torture/compile/pr106537-2.c   -O1   (test for warnings, line 26)
> FAIL: gcc.c-torture/compile/pr106537-2.c   -O1   (test for warnings, line 28)
> FAIL: gcc.c-torture/compile/pr106537-2.c   -O2   (test for warnings, line 26)
> FAIL: gcc.c-torture/compile/pr106537-2.c   -O2   (test for warnings, line 28)
> FAIL: gcc.c-torture/compile/pr106537-2.c   -O3 -g   (test for warnings, line 26)
> FAIL: gcc.c-torture/compile/pr106537-2.c   -O3 -g   (test for warnings, line 28)
> FAIL: gcc.c-torture/compile/pr106537-2.c   -Os   (test for warnings, line 26)
> FAIL: gcc.c-torture/compile/pr106537-2.c   -Os   (test for warnings, line 28)
> FAIL: gcc.c-torture/compile/pr106537-2.c -O2 -flto
> -fno-use-linker-plugin -flto-partition=none (test for warnings, line
> 26)
> FAIL: gcc.c-torture/compile/pr106537-2.c -O2 -flto
> -fno-use-linker-plugin -flto-partition=none (test for warnings, line
> 28)
> FAIL: gcc.c-torture/compile/pr106537-2.c -O2 -flto -fuse-linker-plugin
> -fno-fat-lto-objects (test for warnings, line 26)
> FAIL: gcc.c-torture/compile/pr106537-2.c -O2 -flto -fuse-linker-plugin
> -fno-fat-lto-objects (test for warnings, line 28)
>
> The problem is that for ==/!=, when one of the types is void*,
> build_binary_op goes to the branch attempting to warn about
> comparing void* with a function pointer, and never gets to the 
> -Wcompare-distinct-pointer-types warning.

Oof I wonder what happened with my regtesting.

I just pushed the patch below as obvious, which adjusts the tests to
conform to GCC's behavior of not emitting that pedwarn for
equality/inequality of void pointers with non-function pointers.

Sorry about this.  And thanks for reporting.

From 721f7e2c4e5eed645593258624dd91e6c39f3bd2 Mon Sep 17 00:00:00 2001
From: "Jose E. Marchesi" <jose.marchesi@oracle.com>
Date: Thu, 24 Aug 2023 17:10:52 +0200
Subject: [PATCH] Fix tests for PR 106537.

This patch fixes the tests for PR 106537 (support for
-W[no]-compare-distinct-pointer-types) which were expecting the
warning when checking for equality/inequality of void pointers with
non-function pointers.

gcc/testsuite/ChangeLog:

	PR c/106537
	* gcc.c-torture/compile/pr106537-1.c: Comparing void pointers to
	non-function pointers is legit.
	* gcc.c-torture/compile/pr106537-2.c: Likewise.
---
 gcc/testsuite/gcc.c-torture/compile/pr106537-1.c | 6 ++++--
 gcc/testsuite/gcc.c-torture/compile/pr106537-2.c | 6 ++++--
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/gcc/testsuite/gcc.c-torture/compile/pr106537-1.c b/gcc/testsuite/gcc.c-torture/compile/pr106537-1.c
index 3f3b06577d5..b67b6090dc3 100644
--- a/gcc/testsuite/gcc.c-torture/compile/pr106537-1.c
+++ b/gcc/testsuite/gcc.c-torture/compile/pr106537-1.c
@@ -25,9 +25,11 @@ int xdp_context (struct xdp_md *xdp)
     return 3;
   if (metadata + 1 <= data) /* { dg-warning "comparison of distinct pointer types" } */
     return 4;
-  if (metadata + 1 == data) /* { dg-warning "comparison of distinct pointer types" } */
+  /* Note that it is ok to check for equality or inequality betewen void
+     pointers and any other non-function pointers.  */
+  if ((int*) (metadata + 1) == (long*) data) /* { dg-warning "comparison of distinct pointer types" } */
     return 5;
-  if (metadata + 1 != data) /* { dg-warning "comparison of distinct pointer types" } */
+  if ((int*) metadata + 1 != (long*) data) /* { dg-warning "comparison of distinct pointer types" } */
     return 5;
 
   return 1;
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr106537-2.c b/gcc/testsuite/gcc.c-torture/compile/pr106537-2.c
index 6876adf3aab..d4223c25c94 100644
--- a/gcc/testsuite/gcc.c-torture/compile/pr106537-2.c
+++ b/gcc/testsuite/gcc.c-torture/compile/pr106537-2.c
@@ -23,9 +23,11 @@ int xdp_context (struct xdp_md *xdp)
     return 3;
   if (metadata + 1 <= data) /* { dg-warning "comparison of distinct pointer types" } */
     return 4;
-  if (metadata + 1 == data) /* { dg-warning "comparison of distinct pointer types" } */
+  /* Note that it is ok to check for equality or inequality betewen void
+     pointers and any other non-function pointers.  */
+  if ((int*) (metadata + 1) == (long*) data) /* { dg-warning "comparison of distinct pointer types" } */
     return 5;
-  if (metadata + 1 != data) /* { dg-warning "comparison of distinct pointer types" } */
+  if ((int*) metadata + 1 != (long*) data) /* { dg-warning "comparison of distinct pointer types" } */
     return 5;
 
   return 1;
  

Patch

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index c7b567ba7ab..2242524cd3e 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1935,6 +1935,10 @@  Winvalid-imported-macros
 C++ ObjC++ Var(warn_imported_macros) Warning
 Warn about macros that have conflicting header units definitions.
 
+Wcompare-distinct-pointer-types
+C ObjC Var(warn_compare_distinct_pointer_types) Warning Init(1)
+Warn if pointers of distinct types are compared without a cast.
+
 flang-info-include-translate
 C++ Var(note_include_translate_yes)
 Note #include directives translated to import declarations.
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 6f2fff51683..e6ddf37d412 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -12772,7 +12772,7 @@  build_binary_op (location_t location, enum tree_code code,
 	  else
 	    /* Avoid warning about the volatile ObjC EH puts on decls.  */
 	    if (!objc_ok)
-	      pedwarn (location, 0,
+	      pedwarn (location, OPT_Wcompare_distinct_pointer_types,
 		       "comparison of distinct pointer types lacks a cast");
 
 	  if (result_type == NULL_TREE)
@@ -12912,8 +12912,8 @@  build_binary_op (location_t location, enum tree_code code,
 	      int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
 	      result_type = build_pointer_type
 			      (build_qualified_type (void_type_node, qual));
-	      pedwarn (location, 0,
-		       "comparison of distinct pointer types lacks a cast");
+              pedwarn (location, OPT_Wcompare_distinct_pointer_types,
+                       "comparison of distinct pointer types lacks a cast");
 	    }
 	}
       else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 3380ed8bd6f..28ee6fb62bb 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -345,6 +345,7 @@  Objective-C and Objective-C++ Dialects}.
 -Wcast-align  -Wcast-align=strict  -Wcast-function-type  -Wcast-qual
 -Wchar-subscripts
 -Wclobbered  -Wcomment
+-Wcompare-distinct-pointer-types
 -Wno-complain-wrong-lang
 -Wconversion  -Wno-coverage-mismatch  -Wno-cpp
 -Wdangling-else  -Wdangling-pointer  -Wdangling-pointer=@var{n}
@@ -9106,6 +9107,11 @@  The latter front end diagnoses
 @samp{f951: Warning: command-line option '-fno-rtti' is valid for C++/D/ObjC++ but not for Fortran},
 which may be disabled with @option{-Wno-complain-wrong-lang}.
 
+@opindex Wcompare-distinct-pointer-types
+@item -Wcompare-distinct-pointer-types
+Warn if pointers of distinct types are compared without a cast.  This
+warning is enabled by default.
+
 @opindex Wconversion
 @opindex Wno-conversion
 @item -Wconversion
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr106537-1.c b/gcc/testsuite/gcc.c-torture/compile/pr106537-1.c
new file mode 100644
index 00000000000..3f3b06577d5
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/compile/pr106537-1.c
@@ -0,0 +1,34 @@ 
+/* { dg-do compile }
+   { dg-options "-O0" }
+   This testcase checks that warn_compare_distinct_pointer_types is enabled by
+   default.  */
+
+typedef int __u32;
+
+struct xdp_md
+{
+  char *data;
+  char *data_meta;
+};
+
+int xdp_context (struct xdp_md *xdp)
+{
+  void *data = (void *)(long)xdp->data;
+  __u32 *metadata = (void *)(long)xdp->data_meta;
+  __u32 ret;
+
+  if (metadata + 1 > data) /* { dg-warning "comparison of distinct pointer types" } */
+    return 1;
+  if (metadata + 1 >= data) /* { dg-warning "comparison of distinct pointer types" } */
+    return 2;
+  if (metadata + 1 < data) /* { dg-warning "comparison of distinct pointer types" } */
+    return 3;
+  if (metadata + 1 <= data) /* { dg-warning "comparison of distinct pointer types" } */
+    return 4;
+  if (metadata + 1 == data) /* { dg-warning "comparison of distinct pointer types" } */
+    return 5;
+  if (metadata + 1 != data) /* { dg-warning "comparison of distinct pointer types" } */
+    return 5;
+
+  return 1;
+}
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr106537-2.c b/gcc/testsuite/gcc.c-torture/compile/pr106537-2.c
new file mode 100644
index 00000000000..6876adf3aab
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/compile/pr106537-2.c
@@ -0,0 +1,32 @@ 
+/* { dg-do compile } */
+/* { dg-options "-Wcompare-distinct-pointer-types" } */
+
+typedef int __u32;
+
+struct xdp_md
+{
+  char *data;
+  char *data_meta;
+};
+
+int xdp_context (struct xdp_md *xdp)
+{
+  void *data = (void *)(long)xdp->data;
+  __u32 *metadata = (void *)(long)xdp->data_meta;
+  __u32 ret;
+
+  if (metadata + 1 > data) /* { dg-warning "comparison of distinct pointer types" } */
+    return 1;
+  if (metadata + 1 >= data) /* { dg-warning "comparison of distinct pointer types" } */
+    return 2;
+  if (metadata + 1 < data) /* { dg-warning "comparison of distinct pointer types" } */
+    return 3;
+  if (metadata + 1 <= data) /* { dg-warning "comparison of distinct pointer types" } */
+    return 4;
+  if (metadata + 1 == data) /* { dg-warning "comparison of distinct pointer types" } */
+    return 5;
+  if (metadata + 1 != data) /* { dg-warning "comparison of distinct pointer types" } */
+    return 5;
+
+  return 1;
+}
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr106537-3.c b/gcc/testsuite/gcc.c-torture/compile/pr106537-3.c
new file mode 100644
index 00000000000..73c9b251824
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/compile/pr106537-3.c
@@ -0,0 +1,32 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O0 -Wno-compare-distinct-pointer-types" } */
+
+typedef int __u32;
+
+struct xdp_md
+{
+  char *data;
+  char *data_meta;
+};
+
+int xdp_context (struct xdp_md *xdp)
+{
+  void *data = (void *)(long)xdp->data;
+  __u32 *metadata = (void *)(long)xdp->data_meta;
+  __u32 ret;
+
+  if (metadata + 1 > data) /* There shouldn't be a warning here.  */
+    return 1;
+  if (metadata + 1 >= data) /* There shouldn't be a warning here. */
+    return 2;
+  if (metadata + 1 < data) /* There shouldn't be a warning here.  */
+    return 3;
+  if (metadata + 1 <= data) /* There shouldn't be a warning here.  */
+    return 4;
+  if (metadata + 1 == data) /* There shouldn't be a warning here.  */
+    return 5;
+  if (metadata + 1 != data) /* There shouldn't be a warning here.  */
+    return 5;
+
+  return 1;
+}