1 /* Verify that strncmp equalities aren't eliminated when the trailing array
2    type referenced by a member pointer is smaller than the string in cases
3    when the pointer pointed to by the enclosing object references an object
4    sufficiently large to store a string of equal length.
5   { dg-do compile }
6   { dg-options "-O2 -Wall -Wextra -Wno-stringop-overread -fdump-tree-optimized" } */
7 
8 void init (void*);
9 
10 struct A1 { char i, a[1]; };
11 
f1_arr(void)12 void f1_arr (void)
13 {
14   char a[9];
15   init (a);
16 
17   struct A1 *p = (struct A1*)a;
18 
19   if (__builtin_strncmp (p->a, "01234567", 8) == 0)
20     {
21       extern void array_test (void);
22       array_test ();
23     }
24 }
25 
f1_ptr(void)26 void f1_ptr (void)
27 {
28   void *p;
29   init (&p);
30 
31   struct A1 *q = (struct A1*)p;
32 
33   if (__builtin_strncmp (q->a, "0123456789", 10) == 0)
34     {
35       extern void pointer_test (void);
36       pointer_test ();
37     }
38 }
39 
f1_struct(void)40 void f1_struct (void)
41 {
42   struct { char a[9]; } b;
43   init (&b);
44 
45   struct A1 *p = (struct A1*)&b;
46 
47   if (__builtin_strncmp (p->a, "01234567", 8) == 0)
48     {
49       extern void struct_test (void);
50       struct_test ();
51     }
52 }
53 
f1_memptr(void)54 void f1_memptr (void)
55 {
56   struct { void *p; } b;
57   init (&b);
58 
59   struct A1 *p = (struct A1*)b.p;
60 
61   if (__builtin_strncmp (p->a, "0123456789", 10) == 0)
62     {
63       extern void memptr_test (void);
64       memptr_test ();
65     }
66 }
67 
68 
69 struct A2 { char i, a[2]; };
70 
f2_arr(void)71 void f2_arr (void)
72 {
73   char a[8];
74   init (a);
75 
76   struct A2 *p = (struct A2*)a;
77 
78   if (__builtin_strncmp (p->a, "0123456", 7) == 0)
79     {
80       extern void array_test (void);
81       array_test ();
82     }
83 }
84 
f2_ptr(void)85 void f2_ptr (void)
86 {
87   void *p;
88   init (&p);
89 
90   struct A2 *q = (struct A2*)p;
91 
92   if (__builtin_strncmp (q->a, "0123456789", 10) == 0)
93     {
94       extern void pointer_test (void);
95       pointer_test ();
96     }
97 }
98 
f2_struct(void)99 void f2_struct (void)
100 {
101   struct { char a[8]; } b;
102   init (&b);
103 
104   struct A2 *p = (struct A2*)&b;
105 
106   if (__builtin_strncmp (p->a, "0123456", 7) == 0)
107     {
108       extern void struct_test (void);
109       struct_test ();
110     }
111 }
112 
f2_memptr(void)113 void f2_memptr (void)
114 {
115   struct { void *p; } b;
116   init (&b);
117 
118   struct A2 *p = (struct A2*)b.p;
119 
120   if (__builtin_strncmp (p->a, "0123456789", 10) == 0)
121     {
122       extern void memptr_test (void);
123       memptr_test ();
124     }
125 }
126 
127 /* { dg-final { scan-tree-dump-times "array_test" 2 "optimized" } }
128    { dg-final { scan-tree-dump-times "pointer_test" 2 "optimized" } }
129    { dg-final { scan-tree-dump-times "struct_test" 2 "optimized" } }
130    { dg-final { scan-tree-dump-times "memptr_test" 2 "optimized" } } */
131