public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] c: -Wincompatible-pointer-types should cover mismatches in ?:
Date: Fri, 20 Oct 2023 10:54:53 +0200	[thread overview]
Message-ID: <87pm19znaa.fsf@oldenburg.str.redhat.com> (raw)

gcc/c/

	PR c/109826
	PR other/44209
	* c-typeck.cc (build_conditional_expr): Use
	OPT_Wincompatible_pointer_types for pointer mismatches.
	Emit location information for the operand.

gcc/testsuite/

	* gcc.dg/Wincompatible-pointer-types-4.c: New.
	* gcc.dg/Wincompatible-pointer-types-5.c: New.
	* gcc.dg/Wincompatible-pointer-types-6.c: New.

---
 gcc/c/c-typeck.cc                                    | 16 ++++++++--------
 gcc/testsuite/gcc.dg/Wincompatible-pointer-types-2.c | 10 ++++++++++
 gcc/testsuite/gcc.dg/Wincompatible-pointer-types-3.c | 10 ++++++++++
 gcc/testsuite/gcc.dg/Wincompatible-pointer-types-4.c | 10 ++++++++++
 4 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 6e044b4afbc..0bfae129491 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -5564,14 +5564,14 @@ build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
       else
 	{
 	  int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
-	  if (bltin1 && bltin2)
-	    warning_at (colon_loc, OPT_Wincompatible_pointer_types,
-			"pointer type mismatch between %qT and %qT "
-			"of %qD and %qD in conditional expression",
-			type1, type2, bltin1, bltin2);
-	  else
-	    pedwarn (colon_loc, 0,
-		     "pointer type mismatch in conditional expression");
+	  if (emit_diagnostic (bltin1 && bltin2 ? DK_WARNING : DK_PEDWARN,
+			       colon_loc, OPT_Wincompatible_pointer_types,
+			       "pointer type mismatch "
+			       "in conditional expression"))
+	    {
+	      inform (op1_loc, "first expression has type %qT", type1);
+	      inform (op2_loc, "second expression has type %qT", type2);
+	    }
 	  result_type = build_pointer_type
 			  (build_qualified_type (void_type_node, qual));
 	}
diff --git a/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-2.c b/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-2.c
new file mode 100644
index 00000000000..19276bbcfba
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-2.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+void *
+f1 (int flag, int *a, long *b)
+{
+  return flag ? a : b; /* { dg-warning "pointer type mismatch in conditional expression \\\[-Wincompatible-pointer-types\\\]" } */
+  /* { dg-note "first expression has type 'int \\*'" "" { target *-*-* } .-1 } */
+  /* { dg-note "second expression has type 'long int \\*'" "" { target *-*-* } .-2 } */
+}
diff --git a/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-3.c b/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-3.c
new file mode 100644
index 00000000000..3a6f35e79d9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-3.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c90 -pedantic-errors" } */
+
+void *
+f1 (int flag, int *a, long *b)
+{
+  return flag ? a : b; /* { dg-error "pointer type mismatch in conditional expression \\\[-Wincompatible-pointer-types\\\]" } */
+  /* { dg-note "first expression has type 'int \\*'" "" { target *-*-* } .-1 } */
+  /* { dg-note "second expression has type 'long int \\*'" "" { target *-*-* } .-2 } */
+}
diff --git a/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-4.c b/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-4.c
new file mode 100644
index 00000000000..c62a7e02bb9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wincompatible-pointer-types-4.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c90 -pedantic-errors" } */
+
+void *
+f1 (int flag)
+{
+  return flag ? __builtin_memcpy : __builtin_memcmp; /* { dg-warning "pointer type mismatch in conditional expression \\\[-Wincompatible-pointer-types\\\]" } */
+  /* { dg-note "first expression has type 'void \\* \\(\\*\\)\\(void \\*," "" { target *-*-* } .-1 } */
+  /* { dg-note "second expression has type 'int \\(\\*\\)\\(const void \\*," "" { target *-*-* } .-2 } */
+}

base-commit: 8f4bbdc28df6e87a7ad5ec5ca191a7a836a4f016


             reply	other threads:[~2023-10-20  8:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-20  8:54 Florian Weimer [this message]
2023-10-20 19:08 ` Joseph Myers

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=87pm19znaa.fsf@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).