1 // RUN: %clang_cc1 %s -triple=armv7-none-none-eabi -verify -Wunaligned-access -emit-llvm -o %t 2 // REQUIRES: arm-registered-target 3 // 4 // This test suite tests the warning triggered by the -Wunaligned-access option. 5 // The warning occurs when a struct or other type of record contains a field 6 // that is itself a record. The outer record must be a packed structure, while 7 // while the inner record must be unpacked. This is the fundamental condition 8 // for the warning to be triggered. Some of these tests may have three layers. 9 // 10 // The command line option -fsyntax-only is not used as Clang needs to be 11 // forced to layout the structs used in this test. 12 // The triple in the command line above is used for the assumptions about 13 // size and alignment of types. 14 15 // Packed-Unpacked Tests (No Pragma) 16 17 struct T1 { 18 char a; 19 int b; 20 }; 21 22 struct __attribute__((packed)) U1 { 23 char a; 24 T1 b; // expected-warning {{field b within 'U1' is less aligned than 'T1' and is usually due to 'U1' being packed, which can lead to unaligned accesses}} 25 int c; 26 }; 27 28 struct __attribute__((packed)) U2 { 29 char a; 30 T1 b __attribute__((aligned(4))); 31 int c; 32 }; 33 34 struct __attribute__((packed)) U3 { 35 char a; 36 char b; 37 short c; 38 T1 d; 39 }; 40 41 struct __attribute__((packed)) U4 { 42 T1 a; 43 int b; 44 }; 45 46 struct __attribute__((aligned(4), packed)) U5 { 47 char a; 48 T1 b; // expected-warning {{field b within 'U5' is less aligned than 'T1' and is usually due to 'U5' being packed, which can lead to unaligned accesses}} 49 int c; 50 }; 51 52 struct __attribute__((aligned(4), packed)) U6 { 53 char a; 54 char b; 55 short c; 56 T1 d; 57 }; 58 59 // Packed-Unpacked Tests with Pragma 60 61 #pragma pack(push, 1) 62 63 struct __attribute__((packed)) U7 { 64 char a; 65 T1 b; // expected-warning {{field b within 'U7' is less aligned than 'T1' and is usually due to 'U7' being packed, which can lead to unaligned accesses}} 66 int c; 67 }; 68 69 struct __attribute__((packed)) U8 { 70 char a; 71 T1 b __attribute__((aligned(4))); // expected-warning {{field b within 'U8' is less aligned than 'T1' and is usually due to 'U8' being packed, which can lead to unaligned accesses}} 72 int c; 73 }; 74 75 struct __attribute__((aligned(4))) U9 { 76 char a; 77 T1 b; // expected-warning {{field b within 'U9' is less aligned than 'T1' and is usually due to 'U9' being packed, which can lead to unaligned accesses}} 78 int c; 79 }; 80 81 struct U10 { 82 char a; 83 T1 b; // expected-warning {{field b within 'U10' is less aligned than 'T1' and is usually due to 'U10' being packed, which can lead to unaligned accesses}} 84 int c; 85 }; 86 87 #pragma pack(pop) 88 89 // Packed-Packed Tests 90 91 struct __attribute__((packed)) T2 { 92 char a; 93 int b; 94 }; 95 96 struct __attribute__((packed)) U11 { 97 char a; 98 T2 b; 99 int c; 100 }; 101 102 #pragma pack(push, 1) 103 struct U12 { 104 char a; 105 T2 b; 106 int c; 107 }; 108 #pragma pack(pop) 109 110 // Unpacked-Packed Tests 111 112 struct U13 { 113 char a; 114 T2 b; 115 int c; 116 }; 117 118 struct U14 { 119 char a; 120 T2 b __attribute__((aligned(4))); 121 int c; 122 }; 123 124 // Unpacked-Unpacked Test 125 126 struct T3 { 127 char a; 128 int b; 129 }; 130 131 struct U15 { 132 char a; 133 T3 b; 134 int c; 135 }; 136 137 // Packed-Packed-Unpacked Test (No pragma) 138 139 struct __attribute__((packed)) A1 { 140 char a; 141 T1 b; // expected-warning {{field b within 'A1' is less aligned than 'T1' and is usually due to 'A1' being packed, which can lead to unaligned accesses}} 142 }; 143 144 struct __attribute__((packed)) U16 { 145 char a; 146 A1 b; 147 int c; 148 }; 149 150 struct __attribute__((packed)) A2 { 151 char a; 152 T1 b __attribute__((aligned(4))); 153 }; 154 155 struct __attribute__((packed)) U17 { 156 char a; 157 A2 b; // expected-warning {{field b within 'U17' is less aligned than 'A2' and is usually due to 'U17' being packed, which can lead to unaligned accesses}} 158 int c; 159 }; 160 161 // Packed-Unpacked-Packed tests 162 163 struct A3 { 164 char a; 165 T2 b; 166 }; 167 168 struct __attribute__((packed)) U18 { 169 char a; 170 A3 b; 171 int c; 172 }; 173 174 struct A4 { 175 char a; 176 T2 b; 177 int c; 178 }; 179 180 #pragma pack(push, 1) 181 struct U19 { 182 char a; 183 A4 b; // expected-warning {{field b within 'U19' is less aligned than 'A4' and is usually due to 'U19' being packed, which can lead to unaligned accesses}} 184 int c; 185 }; 186 #pragma pack(pop) 187 188 // Packed-Unpacked-Unpacked tests 189 190 struct A5 { 191 char a; 192 T1 b; 193 }; 194 195 struct __attribute__((packed)) U20 { 196 char a; 197 A5 b; // expected-warning {{field b within 'U20' is less aligned than 'A5' and is usually due to 'U20' being packed, which can lead to unaligned accesses}} 198 int c; 199 }; 200 201 struct A6 { 202 char a; 203 T1 b; 204 }; 205 206 #pragma pack(push, 1) 207 struct U21 { 208 char a; 209 A6 b; // expected-warning {{field b within 'U21' is less aligned than 'A6' and is usually due to 'U21' being packed, which can lead to unaligned accesses}} 210 int c; 211 }; 212 #pragma pack(pop) 213 214 // Unpacked-Packed-Packed test 215 216 struct __attribute__((packed)) A7 { 217 char a; 218 T2 b; 219 }; 220 221 struct U22 { 222 char a; 223 A7 b; 224 int c; 225 }; 226 227 // Unpacked-Packed-Unpacked tests 228 229 struct __attribute__((packed)) A8 { 230 char a; 231 T1 b; // expected-warning {{field b within 'A8' is less aligned than 'T1' and is usually due to 'A8' being packed, which can lead to unaligned accesses}} 232 }; 233 234 struct U23 { 235 char a; 236 A8 b; 237 int c; 238 }; 239 240 struct __attribute__((packed)) A9 { 241 char a; 242 T1 b __attribute__((aligned(4))); 243 }; 244 245 struct U24 { 246 char a; 247 A9 b; 248 int c; 249 }; 250 251 struct U1 s1; 252 struct U2 s2; 253 struct U3 s3; 254 struct U4 s4; 255 struct U5 s5; 256 struct U6 s6; 257 struct U7 s7; 258 struct U8 s8; 259 struct U9 s9; 260 struct U10 s10; 261 struct U11 s11; 262 struct U12 s12; 263 struct U13 s13; 264 struct U14 s14; 265 struct U15 s15; 266 struct U16 s16; 267 struct U17 s17; 268 struct U18 s18; 269 struct U19 s19; 270 struct U20 s20; 271 struct U21 s21; 272 struct U22 s22; 273 struct U23 s23; 274 struct U24 s24;