274 lines · cpp
1// RUN: %clang_cc1 %s -triple=armv7-none-none-eabi -verify -Wunaligned-access -emit-llvm -o %t2// REQUIRES: arm-registered-target3//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 field6// that is itself a record. The outer record must be a packed structure, while7// while the inner record must be unpacked. This is the fundamental condition8// 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 be11// forced to layout the structs used in this test.12// The triple in the command line above is used for the assumptions about13// size and alignment of types.14 15// Packed-Unpacked Tests (No Pragma)16 17struct T1 {18 char a;19 int b;20};21 22struct __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 28struct __attribute__((packed)) U2 {29 char a;30 T1 b __attribute__((aligned(4)));31 int c;32};33 34struct __attribute__((packed)) U3 {35 char a;36 char b;37 short c;38 T1 d;39};40 41struct __attribute__((packed)) U4 {42 T1 a;43 int b;44};45 46struct __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 52struct __attribute__((aligned(4), packed)) U6 {53 char a;54 char b;55 short c;56 T1 d;57};58 59// Packed-Unpacked Tests with Pragma60 61#pragma pack(push, 1)62 63struct __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 69struct __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 75struct __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 81struct 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 Tests90 91struct __attribute__((packed)) T2 {92 char a;93 int b;94};95 96struct __attribute__((packed)) U11 {97 char a;98 T2 b;99 int c;100};101 102#pragma pack(push, 1)103struct U12 {104 char a;105 T2 b;106 int c;107};108#pragma pack(pop)109 110// Unpacked-Packed Tests111 112struct U13 {113 char a;114 T2 b;115 int c;116};117 118struct U14 {119 char a;120 T2 b __attribute__((aligned(4)));121 int c;122};123 124// Unpacked-Unpacked Test125 126struct T3 {127 char a;128 int b;129};130 131struct U15 {132 char a;133 T3 b;134 int c;135};136 137// Packed-Packed-Unpacked Test (No pragma)138 139struct __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 144struct __attribute__((packed)) U16 {145 char a;146 A1 b;147 int c;148};149 150struct __attribute__((packed)) A2 {151 char a;152 T1 b __attribute__((aligned(4)));153};154 155struct __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 tests162 163struct A3 {164 char a;165 T2 b;166};167 168struct __attribute__((packed)) U18 {169 char a;170 A3 b;171 int c;172};173 174struct A4 {175 char a;176 T2 b;177 int c;178};179 180#pragma pack(push, 1)181struct 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 tests189 190struct A5 {191 char a;192 T1 b;193};194 195struct __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 201struct A6 {202 char a;203 T1 b;204};205 206#pragma pack(push, 1)207struct 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 test215 216struct __attribute__((packed)) A7 {217 char a;218 T2 b;219};220 221struct U22 {222 char a;223 A7 b;224 int c;225};226 227// Unpacked-Packed-Unpacked tests228 229struct __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 234struct U23 {235 char a;236 A8 b;237 int c;238};239 240struct __attribute__((packed)) A9 {241 char a;242 T1 b __attribute__((aligned(4)));243};244 245struct U24 {246 char a;247 A9 b;248 int c;249};250 251struct U1 s1;252struct U2 s2;253struct U3 s3;254struct U4 s4;255struct U5 s5;256struct U6 s6;257struct U7 s7;258struct U8 s8;259struct U9 s9;260struct U10 s10;261struct U11 s11;262struct U12 s12;263struct U13 s13;264struct U14 s14;265struct U15 s15;266struct U16 s16;267struct U17 s17;268struct U18 s18;269struct U19 s19;270struct U20 s20;271struct U21 s21;272struct U22 s22;273struct U23 s23;274struct U24 s24;