aboutsummaryrefslogtreecommitdiff
path: root/test/Sema/address-packed.c
blob: 2799e19c48f1353476bf83a80384e93bbbea1397 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// RUN: %clang_cc1 -fsyntax-only -verify %s

#include <stdint.h>

extern void f1(int *);
extern void f2(char *);

struct Ok {
  char c;
  int x;
};

struct __attribute__((packed)) Arguable {
  char c0;
  int x;
  char c1;
};

union __attribute__((packed)) UnionArguable {
  char c;
  int x;
};

typedef struct Arguable ArguableT;

struct Arguable *get_arguable();

void to_void(void *);
void to_intptr(intptr_t);

void g0(void) {
  {
    struct Ok ok;
    f1(&ok.x); // no-warning
    f2(&ok.c); // no-warning
  }
  {
    struct Arguable arguable;
    f2(&arguable.c0); // no-warning
    f1(&arguable.x);  // expected-warning {{packed member 'x' of class or structure 'Arguable'}}
    f2(&arguable.c1); // no-warning

    f1((int *)(void *)&arguable.x); // no-warning
    to_void(&arguable.x);           // no-warning
    void *p = &arguable.x;          // no-warning
    to_void(p);
    to_intptr((intptr_t)p);         // no-warning
  }
  {
    union UnionArguable arguable;
    f2(&arguable.c); // no-warning
    f1(&arguable.x); // expected-warning {{packed member 'x' of class or structure 'UnionArguable'}}

    f1((int *)(void *)&arguable.x);   // no-warning
    to_void(&arguable.x);             // no-warning
    to_intptr((intptr_t)&arguable.x); // no-warning
  }
  {
    ArguableT arguable;
    f2(&arguable.c0); // no-warning
    f1(&arguable.x);  // expected-warning {{packed member 'x' of class or structure 'Arguable'}}
    f2(&arguable.c1); // no-warning

    f1((int *)(void *)&arguable.x);   // no-warning
    to_void(&arguable.x);             // no-warning
    to_intptr((intptr_t)&arguable.x); // no-warning
  }
  {
    struct Arguable *arguable = get_arguable();
    f2(&arguable->c0); // no-warning
    f1(&arguable->x);  // expected-warning {{packed member 'x' of class or structure 'Arguable'}}
    f2(&arguable->c1); // no-warning

    f1((int *)(void *)&arguable->x);    // no-warning
    to_void(&arguable->c1);             // no-warning
    to_intptr((intptr_t)&arguable->c1); // no-warning
  }
  {
    ArguableT *arguable = get_arguable();
    f2(&(arguable->c0)); // no-warning
    f1(&(arguable->x));  // expected-warning {{packed member 'x' of class or structure 'Arguable'}}
    f2(&(arguable->c1)); // no-warning

    f1((int *)(void *)&(arguable->x));      // no-warning
    to_void(&(arguable->c1));               // no-warning
    to_intptr((intptr_t)&(arguable->c1));   // no-warning
  }
}

struct S1 {
  char c;
  int i __attribute__((packed));
};

int *g1(struct S1 *s1) {
  return &s1->i; // expected-warning {{packed member 'i' of class or structure 'S1'}}
}

struct S2_i {
  int i;
};
struct __attribute__((packed)) S2 {
  char c;
  struct S2_i inner;
};

int *g2(struct S2 *s2) {
  return &s2->inner.i; // expected-warning {{packed member 'inner' of class or structure 'S2'}}
}

struct S2_a {
  char c;
  struct S2_i inner __attribute__((packed));
};

int *g2_a(struct S2_a *s2_a) {
  return &s2_a->inner.i; // expected-warning {{packed member 'inner' of class or structure 'S2_a'}}
}

struct __attribute__((packed)) S3 {
  char c;
  struct {
    int i;
  } inner;
};

int *g3(struct S3 *s3) {
  return &s3->inner.i; // expected-warning {{packed member 'inner' of class or structure 'S3'}}
}

struct S4 {
  char c;
  struct __attribute__((packed)) {
    int i;
  } inner;
};

int *g4(struct S4 *s4) {
  return &s4->inner.i; // expected-warning {{packed member 'i' of class or structure 'S4::(anonymous)'}}
}

struct S5 {
  char c;
  struct {
    char c1;
    int i __attribute__((packed));
  } inner;
};

int *g5(struct S5 *s5) {
  return &s5->inner.i; // expected-warning {{packed member 'i' of class or structure 'S5::(anonymous)'}}
}

struct __attribute__((packed, aligned(2))) AlignedTo2 {
  int x;
};

char *g6(struct AlignedTo2 *s) {
  return (char *)&s->x; // no-warning
}

struct __attribute__((packed, aligned(2))) AlignedTo2Bis {
  int x;
};

struct AlignedTo2Bis* g7(struct AlignedTo2 *s)
{
    return (struct AlignedTo2Bis*)&s->x; // no-warning
}

typedef struct {
  char c;
  int x;
} __attribute__((packed)) TypedefStructArguable;

typedef union {
  char c;
  int x;
} __attribute((packed)) TypedefUnionArguable;

typedef TypedefStructArguable TypedefStructArguableTheSecond;

int *typedef1(TypedefStructArguable *s) {
    return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefStructArguable'}}
}

int *typedef2(TypedefStructArguableTheSecond *s) {
    return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefStructArguable'}}
}

int *typedef3(TypedefUnionArguable *s) {
    return &s->x; // expected-warning {{packed member 'x' of class or structure 'TypedefUnionArguable'}}
}

struct S6 {
  union {
    char c;
    int x;
  } __attribute__((packed));
};

int *anonymousInnerUnion(struct S6 *s) {
  return &s->x; // expected-warning {{packed member 'x' of class or structure 'S6::(anonymous)'}}
}

struct S6a {
    int a;
    int _;
    int c;
    char __;
    int d;
} __attribute__((packed, aligned(16))) s6;

void g8()
{ 
    f1(&s6.a); // no-warning
    f1(&s6.c); // no-warning
    f1(&s6.d); // expected-warning {{packed member 'd' of class or structure 'S6a'}}
}

struct __attribute__((packed, aligned(1))) MisalignedContainee { double d; };
struct __attribute__((aligned(8))) AlignedContainer { struct MisalignedContainee b; };

struct AlignedContainer *p;
double* g9() {
  return &p->b.d; // no-warning
}

union OneUnion
{
    uint32_t a;
    uint32_t b:1;
};

struct __attribute__((packed)) S7 {
    uint8_t length;
    uint8_t stuff;
    uint8_t padding[2];
    union OneUnion one_union;
};

union AnotherUnion {
    long data;
    struct S7 s;
} *au;

union OneUnion* get_OneUnion(void)
{
    return &au->s.one_union; // no-warning
}

struct __attribute__((packed)) S8 {
    uint8_t data1;
    uint8_t data2;
	uint16_t wider_data;
};

#define LE_READ_2(p)					\
	((uint16_t)					\
	 ((((const uint8_t *)(p))[0]      ) |		\
	  (((const uint8_t *)(p))[1] <<  8)))

uint32_t get_wider_data(struct S8 *s)
{
    return LE_READ_2(&s->wider_data); // no-warning
}

struct S9 {
  uint32_t x;
  uint8_t y[2];
  uint16_t z;
} __attribute__((__packed__));

typedef struct S9 __attribute__((__aligned__(16))) aligned_S9;

void g10() {
  struct S9 x;
  struct S9 __attribute__((__aligned__(8))) y;
  aligned_S9 z;

  uint32_t *p32;
  p32 = &x.x; // expected-warning {{packed member 'x' of class or structure 'S9'}}
  p32 = &y.x; // no-warning
  p32 = &z.x; // no-warning
}

typedef struct {
  uint32_t msgh_bits;
  uint32_t msgh_size;
  int32_t msgh_voucher_port;
  int32_t msgh_id;
} S10Header;

typedef struct {
  uint32_t t;
  uint64_t m;
  uint32_t p;
  union {
    struct {
      uint32_t a;
      double z;
    } __attribute__((aligned(8), packed)) a;
    struct {
      uint32_t b;
      double z;
      uint32_t a;
    } __attribute__((aligned(8), packed)) b;
  };
} __attribute__((aligned(8), packed)) S10Data;

typedef struct {
  S10Header hdr;
  uint32_t size;
  uint8_t count;
  S10Data data[] __attribute__((aligned(8)));
} __attribute__((aligned(8), packed)) S10;

void g11(S10Header *hdr);
void g12(S10 *s) {
  g11(&s->hdr); // no-warning
}

struct S11 {
  uint32_t x;
} __attribute__((__packed__));

void g13(void) {
  struct S11 __attribute__((__aligned__(4))) a[4];
  uint32_t *p32;
  p32 = &a[0].x; // no-warning
}