dynamic_cast can legally return nullptr, so I don't think it's helpful for -Waddress to warn for
if (dynamic_cast<A*>(&ref)) // ... More generally, it's likely not useful to warn for the artificial POINTER_PLUS_EXPRs created by build_base_path. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/105569 gcc/cp/ChangeLog: * class.cc (build_base_path): Suppress -Waddress warning. gcc/testsuite/ChangeLog: * g++.dg/warn/Waddress-9.C: New test. --- gcc/cp/class.cc | 2 ++ gcc/testsuite/g++.dg/warn/Waddress-9.C | 34 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 gcc/testsuite/g++.dg/warn/Waddress-9.C diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc index 3c195b35396..06167380423 100644 --- a/gcc/cp/class.cc +++ b/gcc/cp/class.cc @@ -518,6 +518,8 @@ build_base_path (enum tree_code code, expr = build1 (NOP_EXPR, ptr_target_type, expr); + suppress_warning (expr, OPT_Waddress); + indout: if (!want_pointer) { 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..2ec41949ccf --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Waddress-9.C @@ -0,0 +1,34 @@ +// 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; + + // -Waddress warns here + bool b = nullptr == dynamic_cast<A*>(&ref); // { dg-bogus "-Waddress" } + bool b4 = nullptr == static_cast<A*>(&ref); // { dg-bogus "-Waddress" } + if (dynamic_cast<A*>(&ref)) // { dg-bogus "-Waddress" } + { + } + if (static_cast<A*>(&ref)) // { dg-bogus "-Waddress" } + { + } + + // -Waddress doesn't warn anymore + auto ptr = dynamic_cast<A*>(&ref); + bool b2 = ptr == nullptr; + + C* cobject = new C(); + C &cref = *cobject; + + // -Waddress also doesn't warn anymore + bool b3 = nullptr == dynamic_cast<A*>(&cref); +} base-commit: 682e587f1021241758f7dfe0b22651008622a312 -- 2.36.1