PR tree-optimization/86401
[official-gcc.git] / gcc / testsuite / c-c++-common / Wstringop-truncation-4.c
blobc76f2823daf3ab4ce78baee6dc2631b53d65dd47
1 /* PR middle-end/84725 - enable attribute nonstring for all narrow character
2 types
3 Verify that -Wstringop-truncation is issued for uses of arrays and
4 pointers to qualified forms of characters of all three types.
5 { dg-do compile }
6 { dg-options "-O2 -Wall -Wstringop-truncation" } */
8 #if __cplusplus
9 extern "C"
10 #endif
11 char* strncpy (char*, const char*, __SIZE_TYPE__);
13 #define S "1234"
15 struct Arrays
17 char a[4];
18 signed char b[4];
19 unsigned char c[4];
22 void test_arrays (struct Arrays *p, const char *s)
24 strncpy (p->a, s, sizeof p->a); /* { dg-warning "\\\[-Wstringop-truncation" } */
25 strncpy ((char*)p->b, s, sizeof p->b); /* { dg-warning "\\\[-Wstringop-truncation" } */
26 strncpy ((char*)p->c, s, sizeof p->c); /* { dg-bogus "\\\[-Wstringop-truncation" } */
29 struct Pointers
31 char *p;
32 signed char *q;
33 unsigned char *r;
36 void test_pointers (struct Pointers *p)
38 strncpy (p->p, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
39 strncpy ((char*)p->q, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
40 strncpy ((char*)p->r, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
43 struct ConstArrays
45 const char a[4];
46 const signed char b[4];
47 const unsigned char c[4];
50 void test_const_arrays (struct ConstArrays *p, const char *s)
52 strncpy ((char*)p->a, s, sizeof p->a); /* { dg-warning "\\\[-Wstringop-truncation" } */
53 strncpy ((char*)p->b, s, sizeof p->b); /* { dg-warning "\\\[-Wstringop-truncation" } */
54 strncpy ((char*)p->c, s, sizeof p->c); /* { dg-bogus "\\\[-Wstringop-truncation" } */
57 struct ConstPointers
59 const char *p;
60 const signed char *q;
61 const unsigned char *r;
64 void test_const_pointers (struct ConstPointers *p)
66 strncpy ((char*)p->p, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
67 strncpy ((char*)p->q, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
68 strncpy ((char*)p->r, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
71 struct VolatileArrays
73 volatile char a[4];
74 volatile signed char b[4];
75 volatile unsigned char c[4];
78 void test_volatile_arrays (struct VolatileArrays *p, const char *s)
80 strncpy ((char*)p->a, s, sizeof p->a); /* { dg-warning "\\\[-Wstringop-truncation" } */
81 strncpy ((char*)p->b, s, sizeof p->b); /* { dg-warning "\\\[-Wstringop-truncation" } */
82 strncpy ((char*)p->c, s, sizeof p->c); /* { dg-bogus "\\\[-Wstringop-truncation" } */
85 struct VolatilePointers
87 volatile char *p;
88 volatile signed char *q;
89 volatile unsigned char *r;
92 void test_volatile_pointers (struct VolatilePointers *p)
94 strncpy ((char*)p->p, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
95 strncpy ((char*)p->q, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
96 strncpy ((char*)p->r, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
99 struct ConstVolatileArrays
101 const volatile char a[4];
102 const volatile signed char b[4];
103 const volatile unsigned char c[4];
106 void test_const_volatile_arrays (struct ConstVolatileArrays *p, const char *s)
108 strncpy ((char*)p->a, s, sizeof p->a); /* { dg-warning "\\\[-Wstringop-truncation" } */
109 strncpy ((char*)p->b, s, sizeof p->b); /* { dg-warning "\\\[-Wstringop-truncation" } */
110 strncpy ((char*)p->c, s, sizeof p->c); /* { dg-bogus "\\\[-Wstringop-truncation" } */
113 struct ConstVolatilePointers
115 const volatile char *p;
116 const volatile signed char *q;
117 const volatile unsigned char *r;
120 void test_const_volatile_pointers (struct ConstVolatilePointers *p)
122 strncpy ((char*)p->p, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
123 strncpy ((char*)p->q, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
124 strncpy ((char*)p->r, S, sizeof S - 1); /* { dg-warning "\\\[-Wstringop-truncation" } */
127 /* { dg-prune-output "-Wdiscarded-qualifiers" } */