From c7813484bc9fe2c578d7538d0a9e286bf2e64599 Mon Sep 17 00:00:00 2001 From: Jeff Law Date: Tue, 25 Sep 2018 16:17:45 -0600 Subject: [PATCH] re PR c/87387 (trunk/gcc/builtins.c:585:7: warning: -Wself-assign problem) PR c/87387 * builtins.c (unterminated_array): Simplify. * expr.c (string_constant): Handle SSA_NAME. Add more exceptions where pointer arithmetic is safe. * gcc.dg/warn-stpcpy-no-nul.c: Drop unnecessary xfails. * gcc.dg/warn-stplen-no-nul.c: Likewise. From-SVN: r264585 --- gcc/ChangeLog | 7 +++++ gcc/builtins.c | 23 ++------------ gcc/expr.c | 37 ++++++++++++++++++++++- gcc/testsuite/ChangeLog | 5 +++ gcc/testsuite/gcc.dg/warn-stpcpy-no-nul.c | 4 +-- gcc/testsuite/gcc.dg/warn-strlen-no-nul.c | 10 +++--- 6 files changed, 57 insertions(+), 29 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 12e7789991a..43857153e9f 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2018-09-25 Bernd Edlinger + + PR c/87387 + * builtins.c (unterminated_array): Simplify. + * expr.c (string_constant): Handle SSA_NAME. Add more exceptions + where pointer arithmetic is safe. + 2018-09-25 Segher Boessenkool PR target/86987 diff --git a/gcc/builtins.c b/gcc/builtins.c index 1d4de099726..5f00208ff09 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -570,28 +570,9 @@ warn_string_no_nul (location_t loc, const char *fn, tree arg, tree decl) tree unterminated_array (tree exp) { - if (TREE_CODE (exp) == SSA_NAME) - { - gimple *stmt = SSA_NAME_DEF_STMT (exp); - if (!is_gimple_assign (stmt)) - return NULL_TREE; - - tree rhs1 = gimple_assign_rhs1 (stmt); - tree_code code = gimple_assign_rhs_code (stmt); - if (code == ADDR_EXPR - && TREE_CODE (TREE_OPERAND (rhs1, 0)) == ARRAY_REF) - rhs1 = rhs1; - else if (code != POINTER_PLUS_EXPR) - return NULL_TREE; - - exp = rhs1; - } - tree nonstr = NULL; - if (c_strlen (exp, 1, &nonstr, 1) == NULL && nonstr) - return nonstr; - - return NULL_TREE; + c_strlen (exp, 1, &nonstr); + return nonstr; } /* Compute the length of a null-terminated character string or wide diff --git a/gcc/expr.c b/gcc/expr.c index b8782b9b133..583c7f008ff 100644 --- a/gcc/expr.c +++ b/gcc/expr.c @@ -11372,7 +11372,10 @@ string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl) /* Avoid pointers to arrays (see bug 86622). */ if (POINTER_TYPE_P (TREE_TYPE (arg)) && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE - && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF) + && !(decl && !*decl) + && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl)) + && mem_size && tree_fits_uhwi_p (*mem_size) + && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl)))) return NULL_TREE; tree type = TREE_TYPE (arg1); @@ -11381,6 +11384,38 @@ string_constant (tree arg, tree *ptr_offset, tree *mem_size, tree *decl) } return NULL_TREE; } + else if (TREE_CODE (arg) == SSA_NAME) + { + gimple *stmt = SSA_NAME_DEF_STMT (arg); + if (!is_gimple_assign (stmt)) + return NULL_TREE; + + tree rhs1 = gimple_assign_rhs1 (stmt); + tree_code code = gimple_assign_rhs_code (stmt); + if (code == ADDR_EXPR) + return string_constant (rhs1, ptr_offset, mem_size, decl); + else if (code != POINTER_PLUS_EXPR) + return NULL_TREE; + + tree offset; + if (tree str = string_constant (rhs1, &offset, mem_size, decl)) + { + /* Avoid pointers to arrays (see bug 86622). */ + if (POINTER_TYPE_P (TREE_TYPE (rhs1)) + && TREE_CODE (TREE_TYPE (TREE_TYPE (rhs1))) == ARRAY_TYPE + && !(decl && !*decl) + && !(decl && tree_fits_uhwi_p (DECL_SIZE_UNIT (*decl)) + && mem_size && tree_fits_uhwi_p (*mem_size) + && tree_int_cst_equal (*mem_size, DECL_SIZE_UNIT (*decl)))) + return NULL_TREE; + + tree rhs2 = gimple_assign_rhs2 (stmt); + tree type = TREE_TYPE (rhs2); + *ptr_offset = fold_build2 (PLUS_EXPR, type, offset, rhs2); + return str; + } + return NULL_TREE; + } else if (DECL_P (arg)) array = arg; else diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 05dbb0d40ea..7b26b894b15 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-09-25 Jeff Law + + * gcc.dg/warn-stpcpy-no-nul.c: Drop unnecessary xfails. + * gcc.dg/warn-stplen-no-nul.c: Likewise. + 2018-09-25 Alexandre Oliva * gnat.dg/dinst.adb: Adjust for locviews. diff --git a/gcc/testsuite/gcc.dg/warn-stpcpy-no-nul.c b/gcc/testsuite/gcc.dg/warn-stpcpy-no-nul.c index 78c4a7f9286..e718010ec2a 100644 --- a/gcc/testsuite/gcc.dg/warn-stpcpy-no-nul.c +++ b/gcc/testsuite/gcc.dg/warn-stpcpy-no-nul.c @@ -71,13 +71,13 @@ void test_two_dim_array (char *d) T (&b[3][1] + 1); /* { dg-warning "nul" } */ T (&b[3][v0]); /* { dg-warning "nul" } */ T (&b[3][1] + v0); /* { dg-warning "nul" } */ - T (&b[3][v0] + v1); /* { dg-warning "nul" "bug ???" { xfail *-*-* } } */ + T (&b[3][v0] + v1); /* { dg-warning "nul" } */ T (&b[i3][i1]); /* { dg-warning "nul" } */ T (&b[i3][i1] + i1); /* { dg-warning "nul" } */ T (&b[i3][v0]); /* { dg-warning "nul" } */ T (&b[i3][i1] + v0); /* { dg-warning "nul" } */ - T (&b[i3][v0] + v1); /* { dg-warning "nul" "bug ???" { xfail *-*-* } } */ + T (&b[i3][v0] + v1); /* { dg-warning "nul" } */ T (v0 ? "" : b[0]); T (v0 ? "" : b[1]); diff --git a/gcc/testsuite/gcc.dg/warn-strlen-no-nul.c b/gcc/testsuite/gcc.dg/warn-strlen-no-nul.c index 997dfc3540f..b716aa44770 100644 --- a/gcc/testsuite/gcc.dg/warn-strlen-no-nul.c +++ b/gcc/testsuite/gcc.dg/warn-strlen-no-nul.c @@ -71,9 +71,9 @@ T (&b[3][v0] + v1); /* { dg-warning "nul" } */ T (&b[i3][i1]); /* { dg-warning "nul" } */ T (&b[i3][i1] + 1); /* { dg-warning "nul" } */ T (&b[i3][i1] + i1); /* { dg-warning "nul" } */ -T (&b[i3][v0]); /* { dg-warning "nul" "pr86919" { xfail *-*-* } } */ -T (&b[i3][i1] + v0); /* { dg-warning "nul" "pr86919" { xfail *-*-* } } */ -T (&b[i3][v0] + v1); /* { dg-warning "nul" "pr86919" { xfail *-*-* } } */ +T (&b[i3][v0]); /* { dg-warning "nul" } */ +T (&b[i3][i1] + v0); /* { dg-warning "nul" } */ +T (&b[i3][v0] + v1); /* { dg-warning "nul" } */ T (v0 ? "" : b[0]); T (v0 ? "" : b[1]); @@ -152,10 +152,10 @@ T (&s.b[1] + v0); /* { dg-warning "nul" } */ T (&s.b[i0]); /* { dg-warning "nul" } */ T (&s.b[i0] + i1); /* { dg-warning "nul" } */ -T (&s.b[i0] + v0); /* { dg-warning "nul" "pr86919" { xfail *-*-* } } */ +T (&s.b[i0] + v0); /* { dg-warning "nul" } */ T (&s.b[i1]); /* { dg-warning "nul" } */ T (&s.b[i1] + i1); /* { dg-warning "nul" } */ -T (&s.b[i1] + v0); /* { dg-warning "nul" "pr86919" { xfail *-*-* } } */ +T (&s.b[i1] + v0); /* { dg-warning "nul" } */ struct B { struct A a[2]; }; -- 2.30.2