On Thu, Feb 18, 2016 at 10:45:46PM +0100, Jakub Jelinek wrote: > Hi! > > And here is a fix for the dynamic_cast issue I've mentioned recently. > Alternatively, ifnonnull could build instead NE_EXPR with swapped last > two arguments on the COND_EXPR, and then I believe the cp_fold change would > not be needed. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? > Or do you prefer the NE_EXPR variant?
Here is the variant with NE_EXPR, it works on *nonnull* tests, haven't tested it more yet, but will overnight. Personally I think this is better, because folding wants to canonicalize the COND_EXPR with EQ_EXPR and no code in the then branch to NE_EXPR with no code in the else branch, so it means we waste some more GC memory. 2016-02-18 Jakub Jelinek <ja...@redhat.com> PR c++/69850 * rtti.c (ifnonnull): Set TREE_NO_WARNING on the condition, use NE_EXPR instead of EQ_EXPR and swap last two arguments on COND_EXPR. * g++.dg/warn/Wnonnull-compare-4.C: New test. --- gcc/cp/rtti.c.jj 2016-02-11 10:50:58.000000000 +0100 +++ gcc/cp/rtti.c 2016-02-18 22:47:04.023079812 +0100 @@ -507,12 +507,13 @@ get_typeid (tree type, tsubst_flags_t co static tree ifnonnull (tree test, tree result, tsubst_flags_t complain) { - return build3 (COND_EXPR, TREE_TYPE (result), - build2 (EQ_EXPR, boolean_type_node, test, - cp_convert (TREE_TYPE (test), nullptr_node, - complain)), - cp_convert (TREE_TYPE (result), nullptr_node, complain), - result); + tree cond = build2 (NE_EXPR, boolean_type_node, test, + cp_convert (TREE_TYPE (test), nullptr_node, complain)); + /* This is a compiler generated comparison, don't emit + e.g. -Wnonnull-compare warning for it. */ + TREE_NO_WARNING (cond) = 1; + return build3 (COND_EXPR, TREE_TYPE (result), cond, result, + cp_convert (TREE_TYPE (result), nullptr_node, complain)); } /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working --- gcc/testsuite/g++.dg/warn/Wnonnull-compare-4.C.jj 2016-02-18 18:03:04.777398778 +0100 +++ gcc/testsuite/g++.dg/warn/Wnonnull-compare-4.C 2016-02-18 18:02:46.414652133 +0100 @@ -0,0 +1,14 @@ +// PR c++/69850 +// { dg-do compile } +// { dg-options "-Wnonnull-compare" } + +struct A { virtual ~A (); int foo (); }; +struct B { virtual ~B () { } }; +struct C : B, A { }; + +int +A::foo () +{ + C *c = dynamic_cast<C *> (this); // { dg-bogus "nonnull argument" } + return !c; +} Jakub