diff options
author | Martin Sebor <msebor@redhat.com> | 2018-05-15 21:52:16 +0000 |
---|---|---|
committer | Martin Sebor <msebor@gcc.gnu.org> | 2018-05-15 15:52:16 -0600 |
commit | 275605696b6754f178af04ef6187b1faed6092e9 (patch) | |
tree | 10e11bad25a9ba1a4554e39dcfae31ec8bfc2f17 /gcc | |
parent | ab168044871f9984ba878720b6391b20cc77ebb2 (diff) | |
download | gcc-275605696b6754f178af04ef6187b1faed6092e9.zip gcc-275605696b6754f178af04ef6187b1faed6092e9.tar.gz gcc-275605696b6754f178af04ef6187b1faed6092e9.tar.bz2 |
PR middle-end/85643 - attribute nonstring fails to squash -Wstringop-truncation warning
gcc/ChangeLog:
PR middle-end/85643
* calls.c (get_attr_nonstring_decl): Handle MEM_REF.
gcc/testsuite/ChangeLog:
PR middle-end/85643
* c-c++-common/attr-nonstring-7.c: New test.
From-SVN: r260271
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/calls.c | 6 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/c-c++-common/attr-nonstring-7.c | 90 |
4 files changed, 105 insertions, 1 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index ecb5241..53fcbb1 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2018-05-15 Martin Sebor <msebor@redhat.com> + + PR middle-end/85643 + * calls.c (get_attr_nonstring_decl): Handle MEM_REF. + 2018-05-15 Richard Biener <rguenther@suse.de> * tree-ssa-dse.c (dse_classify_store): Remove use_stmt parameter, diff --git a/gcc/calls.c b/gcc/calls.c index 9eb0467..f0e9d3b 100644 --- a/gcc/calls.c +++ b/gcc/calls.c @@ -1586,8 +1586,12 @@ get_attr_nonstring_decl (tree expr, tree *ref) if (ref) *ref = decl; - if (TREE_CODE (decl) == COMPONENT_REF) + if (TREE_CODE (decl) == ARRAY_REF) + decl = TREE_OPERAND (decl, 0); + else if (TREE_CODE (decl) == COMPONENT_REF) decl = TREE_OPERAND (decl, 1); + else if (TREE_CODE (decl) == MEM_REF) + return get_attr_nonstring_decl (TREE_OPERAND (decl, 0), ref); if (DECL_P (decl) && lookup_attribute ("nonstring", DECL_ATTRIBUTES (decl))) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index fe012a4..b6e0e78 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-05-15 Martin Sebor <msebor@redhat.com> + + PR middle-end/85643 + * c-c++-common/attr-nonstring-7.c: New test. + 2018-05-15 Richard Biener <rguenther@suse.de> * gcc.dg/tree-ssa/ssa-dse-31.c: New testcase. diff --git a/gcc/testsuite/c-c++-common/attr-nonstring-7.c b/gcc/testsuite/c-c++-common/attr-nonstring-7.c new file mode 100644 index 0000000..a32cbfe --- /dev/null +++ b/gcc/testsuite/c-c++-common/attr-nonstring-7.c @@ -0,0 +1,90 @@ +/* PR 85643 - attribute nonstring fails to squash -Wstringop-truncation + warning + { dg-do compile } + { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */ + +#define strncpy __builtin_strncpy + +struct A { + char a[16 + 1]; +}; + +struct B { + char a[16] __attribute__ ((__nonstring__)); +}; + +struct B* +test_memarray (const struct A *s) +{ + static struct B b; + strncpy (b.a, s->a, sizeof b.a); + return &b; +} + +const char* +test_array (const char *s) +{ + static char a[80] __attribute__ ((__nonstring__)); + strncpy (a, s, sizeof a); + return a; +} + +const char* +test_array_idx (const char *s) +{ + static char a[80] __attribute__ ((__nonstring__)); + char *p __attribute__ ((__nonstring__)) = &a[20]; + strncpy (p, s, 60); /* { dg-bogus "-Wstringop-truncation" } */ + return a; +} + +const char* +test_array_off (const char *s) +{ + static char a[80] __attribute__ ((__nonstring__)); + char *p __attribute__ ((__nonstring__)) = a + 20; + strncpy (p, s, 60); /* { dg-bogus "-Wstringop-truncation" } */ + return a; +} + +struct B* +test_memarray_cstidx_idx (const char *s) +{ + static struct B b[2]; + char *p __attribute__ ((__nonstring__)) = &b[1].a[4]; + + /* The destination below is represented as &MEM[(void *)&a + 20B] and + which (in general) doesn't make it possible to determine what member + it refers to. */ + strncpy (p, s, sizeof b[1].a - 4); /* { dg-bogus "-Wstringop-truncation" "" { xfail *-*-*} } */ + return b; +} + +struct B* +test_memarray_cstidx_off (const char *s) +{ + static struct B b[2]; + char *p __attribute__ ((__nonstring__)) = b[1].a + 4; + + /* Same as above. */ + strncpy (p, s, sizeof b[1].a - 4); /* { dg-bogus "-Wstringop-truncation" "" { xfail *-*-*} } */ + return b; +} + +struct B* +test_memarray_varidx_idx (const char *s, int i) +{ + static struct B b[3]; + char *p __attribute__ ((__nonstring__)) = &b[i].a[4]; + strncpy (p, s, sizeof b[i].a - 4); + return b; +} + +struct B* +test_memarray_varidx_off (const char *s, int i) +{ + static struct B b[3]; + char *p __attribute__ ((__nonstring__)) = b[i].a + 4; + strncpy (p, s, sizeof b[i].a - 4); + return b; +} |