Fix ICE in substring-handling building 502.gcc_r (PR 87562)
[official-gcc.git] / gcc / testsuite / c-c++-common / attr-nonstring-7.c
bloba32cbfedfa58982efeb558e744823db7e378f855
1 /* PR 85643 - attribute nonstring fails to squash -Wstringop-truncation
2 warning
3 { dg-do compile }
4 { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
6 #define strncpy __builtin_strncpy
8 struct A {
9 char a[16 + 1];
12 struct B {
13 char a[16] __attribute__ ((__nonstring__));
16 struct B*
17 test_memarray (const struct A *s)
19 static struct B b;
20 strncpy (b.a, s->a, sizeof b.a);
21 return &b;
24 const char*
25 test_array (const char *s)
27 static char a[80] __attribute__ ((__nonstring__));
28 strncpy (a, s, sizeof a);
29 return a;
32 const char*
33 test_array_idx (const char *s)
35 static char a[80] __attribute__ ((__nonstring__));
36 char *p __attribute__ ((__nonstring__)) = &a[20];
37 strncpy (p, s, 60); /* { dg-bogus "-Wstringop-truncation" } */
38 return a;
41 const char*
42 test_array_off (const char *s)
44 static char a[80] __attribute__ ((__nonstring__));
45 char *p __attribute__ ((__nonstring__)) = a + 20;
46 strncpy (p, s, 60); /* { dg-bogus "-Wstringop-truncation" } */
47 return a;
50 struct B*
51 test_memarray_cstidx_idx (const char *s)
53 static struct B b[2];
54 char *p __attribute__ ((__nonstring__)) = &b[1].a[4];
56 /* The destination below is represented as &MEM[(void *)&a + 20B] and
57 which (in general) doesn't make it possible to determine what member
58 it refers to. */
59 strncpy (p, s, sizeof b[1].a - 4); /* { dg-bogus "-Wstringop-truncation" "" { xfail *-*-*} } */
60 return b;
63 struct B*
64 test_memarray_cstidx_off (const char *s)
66 static struct B b[2];
67 char *p __attribute__ ((__nonstring__)) = b[1].a + 4;
69 /* Same as above. */
70 strncpy (p, s, sizeof b[1].a - 4); /* { dg-bogus "-Wstringop-truncation" "" { xfail *-*-*} } */
71 return b;
74 struct B*
75 test_memarray_varidx_idx (const char *s, int i)
77 static struct B b[3];
78 char *p __attribute__ ((__nonstring__)) = &b[i].a[4];
79 strncpy (p, s, sizeof b[i].a - 4);
80 return b;
83 struct B*
84 test_memarray_varidx_off (const char *s, int i)
86 static struct B b[3];
87 char *p __attribute__ ((__nonstring__)) = b[i].a + 4;
88 strncpy (p, s, sizeof b[i].a - 4);
89 return b;