On Tue, Mar 15, 2016 at 11:56:18AM +0100, Jakub Jelinek wrote:
> From compile time perspective, I wonder if it wouldn't be better to do
> the cheap tests early, like:
>       if (warn_address
>           && (complain & tf_warning)
>           && c_inhibit_evaluation_warnings == 0
>           && !TREE_NO_WARNING (op0))
>         {
>           tree cop0 = fold_non_dependent_expr (op0);
> 
>           if (TREE_CODE (cop0) == ADDR_EXPR
>               && decl_with_nonnull_addr_p (TREE_OPERAND (cop0, 0))
>               && !TREE_NO_WARNING (cop0))
>             warning (OPT_waddress, "the address of %qD will never be NULL",
>                      TREE_OPERAND (cop0, 0));
>         }
> thus perform fold_non_dependent_expr only if it is needed.

Ok, makes sense.

> Furthermore, I wonder if it isn't preferrable to %qD the non-folded
> expression (if it is ADDR_EXPR, that is), so perhaps:
> TREE_OPERAND (TREE_CODE (op0) == ADDR_EXPR ? op0 : cop0, 0)
> ?

I tried this before but it gave the same output as with what I have now,
so I left this unchanged in this version...

Thanks.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2016-03-15  Marek Polacek  <pola...@redhat.com>

        PR c++/70194
        * typeck.c (cp_build_binary_op): Call fold_non_dependent_expr before
        warning about an address not being null.  Check cheap stuff first.

        * g++.dg/warn/constexpr-70194.C: New test.

diff --git gcc/cp/typeck.c gcc/cp/typeck.c
index 20f0afc..5069e88 100644
--- gcc/cp/typeck.c
+++ gcc/cp/typeck.c
@@ -4520,14 +4520,18 @@ cp_build_binary_op (location_t location,
          else
            result_type = type0;
 
-         if (TREE_CODE (op0) == ADDR_EXPR
-             && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
+         if (warn_address
+             && (complain & tf_warning)
+             && c_inhibit_evaluation_warnings == 0
+             && !TREE_NO_WARNING (op0))
            {
-             if ((complain & tf_warning)
-                 && c_inhibit_evaluation_warnings == 0
-                 && !TREE_NO_WARNING (op0))
+             tree cop0 = fold_non_dependent_expr (op0);
+
+             if (TREE_CODE (cop0) == ADDR_EXPR
+                 && decl_with_nonnull_addr_p (TREE_OPERAND (cop0, 0))
+                 && !TREE_NO_WARNING (cop0))
                warning (OPT_Waddress, "the address of %qD will never be NULL",
-                        TREE_OPERAND (op0, 0));
+                        TREE_OPERAND (cop0, 0));
            }
 
          if (CONVERT_EXPR_P (op0)
@@ -4559,14 +4563,18 @@ cp_build_binary_op (location_t location,
          else
            result_type = type1;
 
-         if (TREE_CODE (op1) == ADDR_EXPR 
-             && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
+         if (warn_address
+             && (complain & tf_warning)
+             && c_inhibit_evaluation_warnings == 0
+             && !TREE_NO_WARNING (op1))
            {
-             if ((complain & tf_warning)
-                 && c_inhibit_evaluation_warnings == 0
-                 && !TREE_NO_WARNING (op1))
+             tree cop1 = fold_non_dependent_expr (op1);
+
+             if (TREE_CODE (cop1) == ADDR_EXPR
+                 && decl_with_nonnull_addr_p (TREE_OPERAND (cop1, 0))
+                 && !TREE_NO_WARNING (cop1))
                warning (OPT_Waddress, "the address of %qD will never be NULL",
-                        TREE_OPERAND (op1, 0));
+                        TREE_OPERAND (cop1, 0));
            }
 
          if (CONVERT_EXPR_P (op1)
diff --git gcc/testsuite/g++.dg/warn/constexpr-70194.C 
gcc/testsuite/g++.dg/warn/constexpr-70194.C
index e69de29..cdc56c0 100644
--- gcc/testsuite/g++.dg/warn/constexpr-70194.C
+++ gcc/testsuite/g++.dg/warn/constexpr-70194.C
@@ -0,0 +1,12 @@
+// PR c++/70194
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wall" }
+
+int i;
+
+const bool b0 = &i == 0; // { dg-warning "the address of .i. will never be 
NULL" }
+constexpr int *p = &i;
+const bool b1 = p == 0; // { dg-warning "the address of .i. will never be 
NULL" }
+const bool b2 = 0 == p; // { dg-warning "the address of .i. will never be 
NULL" }
+const bool b3 = p != 0; // { dg-warning "the address of .i. will never be 
NULL" }
+const bool b4 = 0 != p; // { dg-warning "the address of .i. will never be 
NULL" }

        Marek

Reply via email to