public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Martin Sebor <msebor@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-5484] Issue -Waddress also for reference members [PR96507].
Date: Tue, 23 Nov 2021 22:37:18 +0000 (GMT)	[thread overview]
Message-ID: <20211123223718.3118E3857439@sourceware.org> (raw)

https://gcc.gnu.org/g:2dd56aed3e4e1938a9020ab2fe6a410e1a1c2eb3

commit r12-5484-g2dd56aed3e4e1938a9020ab2fe6a410e1a1c2eb3
Author: Martin Sebor <msebor@redhat.com>
Date:   Tue Nov 23 15:35:31 2021 -0700

    Issue -Waddress also for reference members [PR96507].
    
    Resolves:
    PR c++/96507 - missing -Waddress for member references
    
    gcc/cp/ChangeLog:
    
            PR c++/96507
            * typeck.c (warn_for_null_address): Handle reference members.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/96507
            * g++.dg/warn/Waddress-8.C: New test.

Diff:
---
 gcc/cp/typeck.c                        |  5 +-
 gcc/testsuite/g++.dg/warn/Waddress-8.C | 85 ++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 84dcb6f014f..872e944dc2e 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -4677,10 +4677,13 @@ warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
       return;
     }
   else if (CONVERT_EXPR_P (op)
-      && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
+	   && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
     {
       STRIP_NOPS (op);
 
+      if (TREE_CODE (op) == COMPONENT_REF)
+	op = TREE_OPERAND (op, 1);
+
       if (DECL_P (op))
 	warned = warning_at (location, OPT_Waddress,
 			     "the compiler can assume that the address of "
diff --git a/gcc/testsuite/g++.dg/warn/Waddress-8.C b/gcc/testsuite/g++.dg/warn/Waddress-8.C
new file mode 100644
index 00000000000..797102d6be4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Waddress-8.C
@@ -0,0 +1,85 @@
+/* PR c++/96507 - missing -Waddress for member references
+   { dg-do compile }
+   { dg-options "-Wall" } */
+
+typedef void F ();
+
+extern F &efr;
+extern int &eir;
+
+bool warn_ext_rfun ()
+{
+  return &efr != 0;           // { dg-warning "-Waddress" }
+}
+
+bool warn_ext_rvar ()
+{
+  return &eir != 0;           // { dg-warning "-Waddress" }
+}
+
+
+bool warn_parm_rfun (F &rf)
+{
+  return &rf != 0;            // { dg-warning "-Waddress" }
+}
+
+bool warn_parm_rvar (int &ir)
+{
+  return &ir != 0;            // { dg-warning "-Waddress" }
+}
+
+// Comparing the address of a reference argument to null also triggers
+// a -Wnonnull-compare (that seems like a bug, hence PR 103363).
+// { dg-prune-output "-Wnonnull-compare" }
+
+
+struct S
+{
+  F &fr;
+  int &ir;
+};
+
+extern S es, esa[];
+
+bool warn_ext_mem_rfun ()
+{
+  return &es.fr != 0;         // { dg-warning "-Waddress" }
+}
+
+bool warn_ext_mem_rvar ()
+{
+  return &es.ir != 0;         // { dg-warning "-Waddress" }
+}
+
+
+bool warn_ext_arr_mem_rfun (int i)
+{
+  return &esa[i].fr != 0;     // { dg-warning "-Waddress" }
+}
+
+bool warn_ext_arr_mem_rvar (int i)
+{
+  return &esa[i].ir != 0;     // { dg-warning "-Waddress" }
+}
+
+
+bool warn_parm_mem_rfun (S &s)
+{
+  return &s.fr != 0;          // { dg-warning "-Waddress" }
+}
+
+bool warn_parm_mem_rvar (S &s)
+{
+  return &s.ir != 0;          // { dg-warning "-Waddress" }
+}
+
+
+bool warn_parm_arr_mem_rfun (S sa[], int i)
+{
+  return &sa[i].fr != 0;      // { dg-warning "-Waddress" }
+}
+
+bool warn_parm_arr_mem_rvar (S sa[], int i)
+{
+  return &sa[i].ir != 0;      // { dg-warning "-Waddress" }
+}


                 reply	other threads:[~2021-11-23 22:37 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20211123223718.3118E3857439@sourceware.org \
    --to=msebor@gcc.gnu.org \
    --cc=gcc-cvs@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).