Message ID | 20160315104120.GC10006@redhat.com |
---|---|
State | New |
Headers | show |
On Tue, Mar 15, 2016 at 11:41:20AM +0100, Marek Polacek wrote: > This is to fix missing "address of %qD will never be NULL" warning that went > away since the delayed folding merge. The problem was that cp_build_binary_op > was getting unfolded ops so in the constexpr case it saw "(int *) p" instead of > "&i" (in this particular testcase). Fixed by calling fold_non_dependent_expr > as is done elsewhere. > (It doesn't seem like the "if (CONVERT_EXPR_P (op?)" blocks need to use cop? > too.) > > I did not try to address the other issues Martin has raised in the PR yet. > > Bootstrapped/regtested on x86_64-linux, ok for trunk? > > 2016-03-15 Marek Polacek <polacek@redhat.com> > > PR c++/70194 > * typeck.c (cp_build_binary_op): Call fold_non_dependent_expr before > warning about an address not being null. > > * g++.dg/warn/constexpr-70194.C: New test. > > diff --git gcc/cp/typeck.c gcc/cp/typeck.c > index 20f0afc..a789c7a 100644 > --- gcc/cp/typeck.c > +++ gcc/cp/typeck.c > @@ -4520,14 +4520,16 @@ 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))) > + tree cop0 = fold_non_dependent_expr (op0); > + > + if (TREE_CODE (cop0) == ADDR_EXPR > + && decl_with_nonnull_addr_p (TREE_OPERAND (cop0, 0))) 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. 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) ? Jakub
diff --git gcc/cp/typeck.c gcc/cp/typeck.c index 20f0afc..a789c7a 100644 --- gcc/cp/typeck.c +++ gcc/cp/typeck.c @@ -4520,14 +4520,16 @@ 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))) + tree cop0 = fold_non_dependent_expr (op0); + + if (TREE_CODE (cop0) == ADDR_EXPR + && decl_with_nonnull_addr_p (TREE_OPERAND (cop0, 0))) { if ((complain & tf_warning) && c_inhibit_evaluation_warnings == 0 - && !TREE_NO_WARNING (op0)) + && !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 +4561,16 @@ 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))) + tree cop1 = fold_non_dependent_expr (op1); + + if (TREE_CODE (cop1) == ADDR_EXPR + && decl_with_nonnull_addr_p (TREE_OPERAND (cop1, 0))) { if ((complain & tf_warning) && c_inhibit_evaluation_warnings == 0 - && !TREE_NO_WARNING (op1)) + && !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" }