Clang Project

clang_source_code/test/CodeGenCXX/warn-padded-packed.cpp
1// RUN: %clang_cc1 -triple=x86_64-none-none -Wpadded -Wpacked -verify %s -emit-llvm-only
2
3struct S1 {
4  char c;
5  short s; // expected-warning {{padding struct 'S1' with 1 byte to align 's'}}
6  long l; // expected-warning {{padding struct 'S1' with 4 bytes to align 'l'}}
7};
8
9struct S2 { // expected-warning {{padding size of 'S2' with 3 bytes to alignment boundary}}
10  int i;
11  char c;
12};
13
14struct S3 {
15  char c;
16  int i;
17} __attribute__((packed));
18
19struct S4 {
20  int i;
21  char c;
22} __attribute__((packed));
23
24struct S5 {
25  char c;
26  union {
27    char c;
28    int i;
29  } u; // expected-warning {{padding struct 'S5' with 3 bytes to align 'u'}}
30};
31
32struct S6 { // expected-warning {{padding size of 'S6' with 30 bits to alignment boundary}}
33  int i : 2;
34};
35
36struct S7 { // expected-warning {{padding size of 'S7' with 7 bytes to alignment boundary}}
37  char c;
38  virtual void m();
39};
40
41struct B {
42  char c;
43};
44
45struct S8 : B {
46  int i; // expected-warning {{padding struct 'S8' with 3 bytes to align 'i'}}
47};
48
49struct S9 {
50  int x;
51  int y;
52} __attribute__((packed));
53
54struct S10 {
55  int x;
56  char a,b,c,d;
57} __attribute__((packed));
58
59
60struct S11 { // expected-warning {{packed attribute is unnecessary for 'S11'}}
61  bool x;
62  char a,b,c,d;
63} __attribute__((packed));
64
65struct S12 {
66  bool b : 1;
67  char c; // expected-warning {{padding struct 'S12' with 7 bits to align 'c'}}
68};
69
70struct S13 { // expected-warning {{padding size of 'S13' with 6 bits to alignment boundary}}
71  char c;
72  bool b : 10;
73};
74
75struct S14 { // expected-warning {{packed attribute is unnecessary for 'S14'}}
76  char a,b,c,d;
77} __attribute__((packed));
78
79struct S15 { // expected-warning {{packed attribute is unnecessary for 'S15'}}
80  struct S14 s;
81  char a;
82} __attribute__((packed));
83
84struct S16 { // expected-warning {{padding size of 'S16' with 2 bytes to alignment boundary}} expected-warning {{packed attribute is unnecessary for 'S16'}}
85  char a,b;
86} __attribute__((packed, aligned(4)));
87
88struct S17 {
89  struct S16 s;
90  char a,b;
91} __attribute__((packed, aligned(2)));
92
93struct S18 { // expected-warning {{padding size of 'S18' with 2 bytes to alignment boundary}} expected-warning {{packed attribute is unnecessary for 'S18'}}
94  struct S16 s;
95  char a,b;
96} __attribute__((packed, aligned(4)));
97
98struct S19 { // expected-warning {{packed attribute is unnecessary for 'S19'}}
99  bool b;
100  char a;
101} __attribute__((packed, aligned(1)));
102
103struct S20 {
104  int i;
105  char a;
106} __attribute__((packed, aligned(1)));
107
108struct S21 { // expected-warning {{padding size of 'S21' with 4 bits to alignment boundary}}
109  unsigned char a : 6;
110  unsigned char b : 6;
111} __attribute__((packed, aligned(1)));
112
113struct S22 { // expected-warning {{packed attribute is unnecessary for 'S22'}}
114  unsigned char a : 4;
115  unsigned char b : 4;
116} __attribute__((packed));
117
118struct S23 { // expected-warning {{padding size of 'S23' with 4 bits to alignment boundary}} expected-warning {{packed attribute is unnecessary for 'S23'}}
119  unsigned char a : 2;
120  unsigned char b : 2;
121} __attribute__((packed));
122
123struct S24 {
124  unsigned char a : 6;
125  unsigned char b : 6;
126  unsigned char c : 6;
127  unsigned char d : 6;
128  unsigned char e : 6;
129  unsigned char f : 6;
130  unsigned char g : 6;
131  unsigned char h : 6;
132} __attribute__((packed));
133
134struct S25 { // expected-warning {{padding size of 'S25' with 7 bits to alignment boundary}} expected-warning {{packed attribute is unnecessary for 'S25'}}
135  unsigned char a;
136  unsigned char b : 1;
137} __attribute__((packed));
138
139struct S26 { // expected-warning {{packed attribute is unnecessary for 'S26'}}
140  unsigned char a : 1;
141  unsigned char b; //expected-warning {{padding struct 'S26' with 7 bits to align 'b'}}
142} __attribute__((packed));
143
144struct S27 { // expected-warning {{padding size of 'S27' with 7 bits to alignment boundary}}
145  unsigned char a : 1;
146  unsigned char b : 8;
147} __attribute__((packed));
148
149
150// The warnings are emitted when the layout of the structs is computed, so we have to use them.
151void f(S1*, S2*, S3*, S4*, S5*, S6*, S7*, S8*, S9*, S10*, S11*, S12*, S13*,
152       S14*, S15*, S16*, S17*, S18*, S19*, S20*, S21*, S22*, S23*, S24*, S25*,
153       S26*, S27*){}
154