public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Marek Polacek <mpolacek@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-784] c++: improve -Waddress warnings with *_cast [PR105569]
Date: Thu, 26 May 2022 21:11:44 +0000 (GMT)	[thread overview]
Message-ID: <20220526211144.B479C38344E5@sourceware.org> (raw)

https://gcc.gnu.org/g:6f56efa94e845db0d5c934ca202295019bf334c1

commit r13-784-g6f56efa94e845db0d5c934ca202295019bf334c1
Author: Marek Polacek <polacek@redhat.com>
Date:   Wed May 11 14:38:49 2022 -0400

    c++: improve -Waddress warnings with *_cast [PR105569]
    
    This patch improves the diagnostic for -Waddress when it warns for
    
      if (dynamic_cast<A*>(&ref))
        // ...
    
    where 'ref' is a reference, which cannot be null.  In particular, it
    changes
    warning: comparing the result of pointer addition '(((A*)ref) + ((sizetype)(*(long int*)((& ref)->B::_vptr.B + -24))))' and NULL
    to
    warning: the compiler can assume that the address of 'ref' will never be NULL
    
            PR c++/105569
    
    gcc/cp/ChangeLog:
    
            * typeck.cc (warn_for_null_address): Improve the warning when
            the POINTER_PLUS_EXPR's base is of reference type.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/warn/Waddress-9.C: New test.

Diff:
---
 gcc/cp/typeck.cc                       | 12 ++++++++++--
 gcc/testsuite/g++.dg/warn/Waddress-9.C | 31 +++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index 385cdf4d733..190d710cd27 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -4757,8 +4757,16 @@ warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
       tree off = TREE_OPERAND (cop, 1);
       if (!integer_zerop (off)
 	  && !warning_suppressed_p (cop, OPT_Waddress))
-	warning_at (location, OPT_Waddress, "comparing the result of pointer "
-		    "addition %qE and NULL", cop);
+	{
+	  tree base = TREE_OPERAND (cop, 0);
+	  STRIP_NOPS (base);
+	  if (TYPE_REF_P (TREE_TYPE (base)))
+	    warning_at (location, OPT_Waddress, "the compiler can assume that "
+			"the address of %qE will never be NULL", base);
+	  else
+	    warning_at (location, OPT_Waddress, "comparing the result of "
+			"pointer addition %qE and NULL", cop);
+	}
       return;
     }
   else if (CONVERT_EXPR_P (op)
diff --git a/gcc/testsuite/g++.dg/warn/Waddress-9.C b/gcc/testsuite/g++.dg/warn/Waddress-9.C
new file mode 100644
index 00000000000..d3e469734b8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Waddress-9.C
@@ -0,0 +1,31 @@
+// PR c++/105569
+// { dg-do compile { target c++11 } }
+// { dg-options -Waddress }
+
+class A {};
+
+class B : public virtual A {};
+
+class C : public A {};
+
+int main() {
+    B* object = new B();
+    B &ref = *object;
+
+    bool b = nullptr == dynamic_cast<A*>(&ref); // { dg-warning "the address of 'ref' will never be NULL" }
+    bool b4 = nullptr == static_cast<A*>(&ref); // { dg-warning "the address of 'ref' will never be NULL" }
+    if (dynamic_cast<A*>(&ref)) // { dg-warning "the address of 'ref' will never be NULL" }
+      {
+      }
+    if (static_cast<A*>(&ref)) // { dg-warning "the address of 'ref' will never be NULL" }
+      {
+      }
+
+    auto ptr = dynamic_cast<A*>(&ref);
+    bool b2 = ptr == nullptr;
+
+    C* cobject = new C();
+    C &cref = *cobject;
+
+    bool b3 = nullptr == dynamic_cast<A*>(&cref);
+}


                 reply	other threads:[~2022-05-26 21:11 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=20220526211144.B479C38344E5@sourceware.org \
    --to=mpolacek@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).