annotate gcc/testsuite/c-c++-common/Wsizeof-pointer-memaccess1.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Test -Wsizeof-pointer-memaccess warnings. */
kono
parents:
diff changeset
2 /* { dg-do compile } */
kono
parents:
diff changeset
3 /* { dg-options "-Wall -Wno-sizeof-array-argument" } */
kono
parents:
diff changeset
4 /* { dg-options "-Wall -Wno-sizeof-array-argument -Wno-c++-compat" { target c } } */
kono
parents:
diff changeset
5 /* { dg-require-effective-target alloca } */
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 typedef __SIZE_TYPE__ size_t;
kono
parents:
diff changeset
8 #ifdef __cplusplus
kono
parents:
diff changeset
9 extern "C" {
kono
parents:
diff changeset
10 #endif
kono
parents:
diff changeset
11 extern int snprintf (char *, size_t, const char *, ...);
kono
parents:
diff changeset
12 extern int vsnprintf (char *, size_t, const char *, __builtin_va_list);
kono
parents:
diff changeset
13 extern void *memchr (const void *, int, size_t);
kono
parents:
diff changeset
14 #ifdef __cplusplus
kono
parents:
diff changeset
15 }
kono
parents:
diff changeset
16 #endif
kono
parents:
diff changeset
17
kono
parents:
diff changeset
18 struct A { short a, b; int c, d; long e, f; };
kono
parents:
diff changeset
19 typedef struct A TA;
kono
parents:
diff changeset
20 typedef struct A *PA;
kono
parents:
diff changeset
21 typedef TA *PTA;
kono
parents:
diff changeset
22 struct B {};
kono
parents:
diff changeset
23 typedef struct B TB;
kono
parents:
diff changeset
24 typedef struct B *PB;
kono
parents:
diff changeset
25 typedef TB *PTB;
kono
parents:
diff changeset
26 typedef int X[3][3][3];
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 void foo (void **);
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 void
kono
parents:
diff changeset
31 f1 (void *x)
kono
parents:
diff changeset
32 {
kono
parents:
diff changeset
33 struct A a, *pa1 = &a;
kono
parents:
diff changeset
34 TA *pa2 = &a;
kono
parents:
diff changeset
35 PA pa3 = &a;
kono
parents:
diff changeset
36 PTA pa4 = &a;
kono
parents:
diff changeset
37 void *arr[100];
kono
parents:
diff changeset
38 int i = 0;
kono
parents:
diff changeset
39 arr[i++] = memchr (&a, 0, sizeof (&a)); /* { dg-warning "call is the same expression as the source; did you mean to remove the addressof" } */
kono
parents:
diff changeset
40 arr[i++] = memchr (pa1, 0, sizeof (pa1)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
kono
parents:
diff changeset
41 arr[i++] = memchr (pa2, 0, sizeof pa2); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
kono
parents:
diff changeset
42 arr[i++] = memchr (pa3, 0, sizeof (pa3)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
kono
parents:
diff changeset
43 arr[i++] = memchr (pa4, 0, sizeof pa4); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
kono
parents:
diff changeset
44 arr[i++] = memchr (pa1, 0, sizeof (struct A *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
kono
parents:
diff changeset
45 arr[i++] = memchr (pa2, 0, sizeof (PTA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
kono
parents:
diff changeset
46 arr[i++] = memchr (pa3, 0, sizeof (PA)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
kono
parents:
diff changeset
47 arr[i++] = memchr (pa4, 0, sizeof (__typeof (pa4))); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 /* These are correct, no warning. */
kono
parents:
diff changeset
50 arr[i++] = memchr (&a, 0, sizeof a);
kono
parents:
diff changeset
51 arr[i++] = memchr (&a, 0, sizeof (a));
kono
parents:
diff changeset
52 arr[i++] = memchr (&a, 0, sizeof (struct A));
kono
parents:
diff changeset
53 arr[i++] = memchr (&a, 0, sizeof (const struct A));
kono
parents:
diff changeset
54 arr[i++] = memchr (&a, 0, sizeof (volatile struct A));
kono
parents:
diff changeset
55 arr[i++] = memchr (&a, 0, sizeof (volatile const struct A));
kono
parents:
diff changeset
56 arr[i++] = memchr (&a, 0, sizeof (TA));
kono
parents:
diff changeset
57 arr[i++] = memchr (&a, 0, sizeof (__typeof (*&a)));
kono
parents:
diff changeset
58 arr[i++] = memchr (pa1, 0, sizeof (*pa1));
kono
parents:
diff changeset
59 arr[i++] = memchr (pa2, 0, sizeof (*pa3));
kono
parents:
diff changeset
60 arr[i++] = memchr (pa3, 0, sizeof (__typeof (*pa3)));
kono
parents:
diff changeset
61 /* These are probably broken, but obfuscated, no warning. */
kono
parents:
diff changeset
62 arr[i++] = memchr ((void *) &a, 0, sizeof (&a));
kono
parents:
diff changeset
63 arr[i++] = memchr ((char *) &a, 0, sizeof (&a));
kono
parents:
diff changeset
64 arr[i++] = memchr (&a, 0, sizeof (&a) + 0);
kono
parents:
diff changeset
65 arr[i++] = memchr (&a, 0, 0 + sizeof (&a));
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 foo (arr);
kono
parents:
diff changeset
68 }
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 void
kono
parents:
diff changeset
71 f2 (void *x)
kono
parents:
diff changeset
72 {
kono
parents:
diff changeset
73 struct B b, *pb1 = &b;
kono
parents:
diff changeset
74 TB *pb2 = &b;
kono
parents:
diff changeset
75 PB pb3 = &b;
kono
parents:
diff changeset
76 PTB pb4 = &b;
kono
parents:
diff changeset
77 void *arr[100];
kono
parents:
diff changeset
78 int i = 0;
kono
parents:
diff changeset
79 arr[i++] = memchr (&b, 0, sizeof (&b)); /* { dg-warning "call is the same expression as the source; did you mean to remove the addressof" } */
kono
parents:
diff changeset
80 arr[i++] = memchr (pb1, 0, sizeof (pb1)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
kono
parents:
diff changeset
81 arr[i++] = memchr (pb2, 0, sizeof pb2); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
kono
parents:
diff changeset
82 arr[i++] = memchr (pb3, 0, sizeof (pb3)); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
kono
parents:
diff changeset
83 arr[i++] = memchr (pb4, 0, sizeof pb4); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
kono
parents:
diff changeset
84 arr[i++] = memchr (pb1, 0, sizeof (struct B *)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
kono
parents:
diff changeset
85 arr[i++] = memchr (pb2, 0, sizeof (PTB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
kono
parents:
diff changeset
86 arr[i++] = memchr (pb3, 0, sizeof (PB)); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
kono
parents:
diff changeset
87 arr[i++] = memchr (pb4, 0, sizeof (__typeof (pb4))); /* { dg-warning "call is the same pointer type \[^\n\r\]* as the source; expected \[^\n\r\]* or an explicit length" } */
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 /* These are correct, no warning. */
kono
parents:
diff changeset
90 arr[i++] = memchr (&b, 0, sizeof b);
kono
parents:
diff changeset
91 arr[i++] = memchr (&b, 0, sizeof (b));
kono
parents:
diff changeset
92 arr[i++] = memchr (&b, 0, sizeof (struct B));
kono
parents:
diff changeset
93 arr[i++] = memchr (&b, 0, sizeof (const struct B));
kono
parents:
diff changeset
94 arr[i++] = memchr (&b, 0, sizeof (volatile struct B));
kono
parents:
diff changeset
95 arr[i++] = memchr (&b, 0, sizeof (volatile const struct B));
kono
parents:
diff changeset
96 arr[i++] = memchr (&b, 0, sizeof (TB));
kono
parents:
diff changeset
97 arr[i++] = memchr (&b, 0, sizeof (__typeof (*&b)));
kono
parents:
diff changeset
98 arr[i++] = memchr (pb1, 0, sizeof (*pb1));
kono
parents:
diff changeset
99 arr[i++] = memchr (pb2, 0, sizeof (*pb3));
kono
parents:
diff changeset
100 arr[i++] = memchr (pb3, 0, sizeof (__typeof (*pb3)));
kono
parents:
diff changeset
101 /* These are probably broken, but obfuscated, no warning. */
kono
parents:
diff changeset
102 arr[i++] = memchr ((void *) &b, 0, sizeof (&b));
kono
parents:
diff changeset
103 arr[i++] = memchr ((char *) &b, 0, sizeof (&b));
kono
parents:
diff changeset
104 arr[i++] = memchr (&b, 0, sizeof (&b) + 0);
kono
parents:
diff changeset
105 arr[i++] = memchr (&b, 0, 0 + sizeof (&b));
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 foo (arr);
kono
parents:
diff changeset
108 }
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 void
kono
parents:
diff changeset
111 f3 (void *x, char *y, int z, X w)
kono
parents:
diff changeset
112 {
kono
parents:
diff changeset
113 unsigned char *y1 = (unsigned char *) __builtin_alloca (z + 16);
kono
parents:
diff changeset
114 char buf1[7];
kono
parents:
diff changeset
115 signed char buf2[z + 32];
kono
parents:
diff changeset
116 long buf3[17];
kono
parents:
diff changeset
117 int *buf4[9];
kono
parents:
diff changeset
118 signed char *y2 = buf2;
kono
parents:
diff changeset
119 char c;
kono
parents:
diff changeset
120 void *arr[100];
kono
parents:
diff changeset
121 int i = 0;
kono
parents:
diff changeset
122 arr[i++] = memchr (y, 0, sizeof (y)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
kono
parents:
diff changeset
123 arr[i++] = memchr (y1, 0, sizeof (y1)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
kono
parents:
diff changeset
124 arr[i++] = memchr (y2, 0, sizeof (y2)); /* { dg-warning "call is the same expression as the source; did you mean to provide an explicit length" } */
kono
parents:
diff changeset
125 arr[i++] = memchr (&c, 0, sizeof (&c)); /* { dg-warning "call is the same expression as the source; did you mean to remove the addressof" } */
kono
parents:
diff changeset
126 arr[i++] = memchr (w, 0, sizeof w); /* { dg-warning "call is the same expression as the source; did you mean to dereference it" } */
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 /* These are correct, no warning. */
kono
parents:
diff changeset
129 arr[i++] = memchr (y, 0, sizeof (*y));
kono
parents:
diff changeset
130 arr[i++] = memchr (y1, 0, sizeof (*y2));
kono
parents:
diff changeset
131 arr[i++] = memchr (buf1, 0, sizeof buf1);
kono
parents:
diff changeset
132 arr[i++] = memchr (buf3, 0, sizeof (buf3));
kono
parents:
diff changeset
133 arr[i++] = memchr (&buf3[0], 0, sizeof (buf3));
kono
parents:
diff changeset
134 arr[i++] = memchr (&buf4[0], 0, sizeof (buf4));
kono
parents:
diff changeset
135 arr[i++] = memchr (w, 0, sizeof (X));
kono
parents:
diff changeset
136 /* These are probably broken, but obfuscated, no warning. */
kono
parents:
diff changeset
137 arr[i++] = memchr ((void *) y, 0, sizeof (y));
kono
parents:
diff changeset
138 arr[i++] = memchr ((char *) y1, 0, sizeof (y2));
kono
parents:
diff changeset
139 arr[i++] = memchr (y, 0, sizeof (y) + 0);
kono
parents:
diff changeset
140 arr[i++] = memchr (y1, 0, 0 + sizeof (y2));
kono
parents:
diff changeset
141 arr[i++] = memchr ((void *) &c, 0, sizeof (&c));
kono
parents:
diff changeset
142 arr[i++] = memchr ((signed char *) &c, 0, sizeof (&c));
kono
parents:
diff changeset
143 arr[i++] = memchr (&c, 0, sizeof (&c) + 0);
kono
parents:
diff changeset
144 arr[i++] = memchr (&c, 0, 0 + sizeof (&c));
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 foo (arr);
kono
parents:
diff changeset
147 }
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 void
kono
parents:
diff changeset
150 f4 (char x[64], char *y, __builtin_va_list ap)
kono
parents:
diff changeset
151 {
kono
parents:
diff changeset
152 char buf[128], *p = buf;
kono
parents:
diff changeset
153 snprintf (x, sizeof (x), "%s", y); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
kono
parents:
diff changeset
154 vsnprintf (x, sizeof (x), "%s", ap); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
kono
parents:
diff changeset
155 snprintf (p, sizeof (p), "%s", y); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
kono
parents:
diff changeset
156 vsnprintf (p, sizeof (p), "%s", ap); /* { dg-warning "call is the same expression as the destination; did you mean to provide an explicit length" } */
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 /* These are correct, no warning. */
kono
parents:
diff changeset
159 snprintf (buf, sizeof (buf), "%s", y);
kono
parents:
diff changeset
160 vsnprintf (buf, sizeof (buf), "%s", ap);
kono
parents:
diff changeset
161 snprintf (p, sizeof (buf), "%s", y);
kono
parents:
diff changeset
162 vsnprintf (p, sizeof (buf), "%s", ap);
kono
parents:
diff changeset
163 }