xref: /llvm-project/clang/test/CodeGenCXX/warn-all-padded-packed-packed-non-pod.cpp (revision 43feb3eeb1ba4de920d6eaa12b7a5c21f6dd6229)
1 // RUN: %clang_cc1 -triple=x86_64-none-none -Wpadded -Wpacked -Wno-padded-bitfield -verify=expected,top %s -emit-llvm-only
2 // RUN: %clang_cc1 -triple=x86_64-none-none -Wpadded -Wpacked -Wno-padded-bitfield -verify=expected,abi15 -fclang-abi-compat=15 %s -emit-llvm-only
3 // -Wpacked-non-pod itself should not emit the "packed attribute is unnecessary" warnings.
4 // RUN: %clang_cc1 -triple=x86_64-none-none -Wpacked-non-pod -verify=top %s -emit-llvm-only
5 // -Wall should not emit the "packed attribute is unnecessary" warnings without -Wpacked.
6 // RUN: %clang_cc1 -triple=x86_64-none-none -Wall -verify=top %s -emit-llvm-only
7 
8 
9 struct S1 {
10   char c;
11   short s; // expected-warning {{padding struct 'S1' with 1 byte to align 's'}}
12   long l; // expected-warning {{padding struct 'S1' with 4 bytes to align 'l'}}
13 };
14 
15 struct S2 { // expected-warning {{padding size of 'S2' with 3 bytes to alignment boundary}}
16   int i;
17   char c;
18 };
19 
20 struct S3 {
21   char c;
22   int i;
23 } __attribute__((packed));
24 
25 struct S4 {
26   int i;
27   char c;
28 } __attribute__((packed));
29 
30 struct S5 {
31   char c;
32   union {
33     char c;
34     int i;
35   } u; // expected-warning {{padding struct 'S5' with 3 bytes to align 'u'}}
36 };
37 
38 struct S6 { // expected-warning {{padding size of 'S6' with 30 bits to alignment boundary}}
39   int i : 2;
40 };
41 
42 struct S7 { // expected-warning {{padding size of 'S7' with 7 bytes to alignment boundary}}
43   char c;
44   virtual void m();
45 };
46 
47 struct B {
48   char c;
49 };
50 
51 struct S8 : B {
52   int i; // expected-warning {{padding struct 'S8' with 3 bytes to align 'i'}}
53 };
54 
55 struct S9 {
56   int x;
57   int y;
58 } __attribute__((packed));
59 
60 struct S10 {
61   int x;
62   char a,b,c,d;
63 } __attribute__((packed));
64 
65 
66 struct S11 { // expected-warning {{packed attribute is unnecessary for 'S11'}}
67   bool x;
68   char a,b,c,d;
69 } __attribute__((packed));
70 
71 struct S12 {
72   bool b : 1;
73   char c; // expected-warning {{padding struct 'S12' with 7 bits to align 'c'}}
74 };
75 
76 struct S13 { // expected-warning {{padding size of 'S13' with 6 bits to alignment boundary}}
77   char c;
78   bool b : 10;
79 };
80 
81 struct S14 { // expected-warning {{packed attribute is unnecessary for 'S14'}}
82   char a,b,c,d;
83 } __attribute__((packed));
84 
85 struct S15 { // expected-warning {{packed attribute is unnecessary for 'S15'}}
86   struct S14 s;
87   char a;
88 } __attribute__((packed));
89 
90 struct S16 { // expected-warning {{padding size of 'S16' with 2 bytes to alignment boundary}} expected-warning {{packed attribute is unnecessary for 'S16'}}
91   char a,b;
92 } __attribute__((packed, aligned(4)));
93 
94 struct S17 {
95   struct S16 s;
96   char a,b;
97 } __attribute__((packed, aligned(2)));
98 
99 struct S18 { // expected-warning {{padding size of 'S18' with 2 bytes to alignment boundary}} expected-warning {{packed attribute is unnecessary for 'S18'}}
100   struct S16 s;
101   char a,b;
102 } __attribute__((packed, aligned(4)));
103 
104 struct S19 { // expected-warning {{packed attribute is unnecessary for 'S19'}}
105   bool b;
106   char a;
107 } __attribute__((packed, aligned(1)));
108 
109 struct S20 {
110   int i;
111   char a;
112 } __attribute__((packed, aligned(1)));
113 
114 struct S21 { // expected-warning {{padding size of 'S21' with 4 bits to alignment boundary}}
115   unsigned char a : 6;
116   unsigned char b : 6;
117 } __attribute__((packed, aligned(1)));
118 
119 struct S22 { // expected-warning {{packed attribute is unnecessary for 'S22'}}
120   unsigned char a : 4;
121   unsigned char b : 4;
122 } __attribute__((packed));
123 
124 struct S23 { // expected-warning {{padding size of 'S23' with 4 bits to alignment boundary}} expected-warning {{packed attribute is unnecessary for 'S23'}}
125   unsigned char a : 2;
126   unsigned char b : 2;
127 } __attribute__((packed));
128 
129 struct S24 {
130   unsigned char a : 6;
131   unsigned char b : 6;
132   unsigned char c : 6;
133   unsigned char d : 6;
134   unsigned char e : 6;
135   unsigned char f : 6;
136   unsigned char g : 6;
137   unsigned char h : 6;
138 } __attribute__((packed));
139 
140 struct S25 { // expected-warning {{padding size of 'S25' with 7 bits to alignment boundary}} expected-warning {{packed attribute is unnecessary for 'S25'}}
141   unsigned char a;
142   unsigned char b : 1;
143 } __attribute__((packed));
144 
145 struct S26 { // expected-warning {{packed attribute is unnecessary for 'S26'}}
146   unsigned char a : 1;
147   unsigned char b; //expected-warning {{padding struct 'S26' with 7 bits to align 'b'}}
148 } __attribute__((packed));
149 
150 struct S27 { // expected-warning {{padding size of 'S27' with 7 bits to alignment boundary}}
151   unsigned char a : 1;
152   unsigned char b : 8;
153 } __attribute__((packed));
154 
155 struct S28_non_pod {
156  protected:
157   int i;
158 };
159 struct S28 {
160   char c1;
161   short s1;
162   char c2;
163   S28_non_pod p1; // top-warning {{not packing field 'p1' as it is non-POD for the purposes of layout}}
164 } __attribute__((packed));
165 
166 struct S29_non_pod_align_1 {
167  protected:
168   char c;
169 };
170 struct S29 {
171   S29_non_pod_align_1 p1;
172   int i;
173 } __attribute__((packed)); // no warning
174 static_assert(alignof(S29) == 1, "");
175 
176 struct S30 {
177 protected:
178  short s;
179 } __attribute__((packed)); // no warning
180 struct S30_use { // abi15-warning {{packed attribute is unnecessary for 'S30_use'}}
181   char c;
182   S30 u;
183 } __attribute__((packed));
184 static_assert(sizeof(S30_use) == 3, "");
185 
186 // The warnings are emitted when the layout of the structs is computed, so we have to use them.
f(S1,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S12,S13,S14,S15,S16,S17,S18,S19,S20,S21,S22,S23,S24,S25,S26,S27,S28,S29)187 void f(S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13,
188        S14, S15, S16, S17, S18, S19, S20, S21, S22, S23, S24, S25,
189        S26, S27, S28, S29){}
190