312 lines · cpp
1// RUN: %clang_analyze_cc1 -triple x86_64-unknown-linux -std=c++14 -analyzer-checker=optin.performance -analyzer-config optin.performance.Padding:AllowedPad=2 -verify %s2 3// expected-warning@+7{{\4Excessive padding in 'struct IntSandwich' (6 padding bytes, where 2 is optimal). \5Optimal fields order: \6i, \7c1, \8c2, \9}}10struct IntSandwich {11 char c1;12 int i;13 char c2;14};15 16// expected-warning@+7{{\17Excessive padding in 'struct TurDuckHen' (6 padding bytes, where 2 is optimal). \18Optimal fields order: \19i, \20c1, \21c2, \22}}23struct TurDuckHen {24 char c1;25 struct IntSandwich i;26 char c2;27};28 29#pragma pack(push)30#pragma pack(2)31// expected-warning@+11{{\32Excessive padding in 'struct SmallIntSandwich' (4 padding bytes, where 0 is optimal). \33Optimal fields order: \34i1, \35i2, \36i3, \37c1, \38c2, \39c3, \40c4, \41}}42struct SmallIntSandwich {43 char c1;44 int i1;45 char c2;46 int i2;47 char c3;48 int i3;49 char c4;50};51#pragma pack(pop)52 53union SomeUnion { // no-warning54 char c;55 short s;56 int i;57};58 59// expected-warning@+7{{\60Excessive padding in 'struct HoldsAUnion' (6 padding bytes, where 2 is optimal). \61Optimal fields order: \62u, \63c1, \64c2, \65}}66struct HoldsAUnion {67 char c1;68 union SomeUnion u;69 char c2;70};71 72struct SmallCharArray { // no-warning73 char c[5];74};75 76struct MediumIntArray { // no-warning77 int i[5];78};79 80// expected-warning@+7{{\81Excessive padding in 'struct StructSandwich' (6 padding bytes, where 2 is optimal). \82Optimal fields order: \83m, \84s, \85s2, \86}}87struct StructSandwich {88 struct SmallCharArray s;89 struct MediumIntArray m;90 struct SmallCharArray s2;91};92 93// expected-warning@+7{{\94Excessive padding in 'TypedefSandwich' (6 padding bytes, where 2 is optimal). \95Optimal fields order: \96i, \97c1, \98c2, \99}}100typedef struct {101 char c1;102 int i;103 char c2;104} TypedefSandwich;105 106// expected-warning@+7{{\107Excessive padding in 'struct StructAttrAlign' (10 padding bytes, where 2 is optimal). \108Optimal fields order: \109i, \110c1, \111c2, \112}}113struct StructAttrAlign {114 char c1;115 int i;116 char c2;117} __attribute__((aligned(8)));118 119// expected-warning@+8{{\120Excessive padding in 'struct OverlyAlignedChar' (8185 padding bytes, where 4089 is optimal). \121Optimal fields order: \122c, \123c1, \124c2, \125x, \126}}127struct OverlyAlignedChar {128 char c1;129 int x;130 char c2;131 char c __attribute__((aligned(4096)));132};133 134// expected-warning@+7{{\135Excessive padding in 'struct HoldsOverlyAlignedChar' (8190 padding bytes, where 4094 is optimal). \136Optimal fields order: \137o, \138c1, \139c2, \140}}141struct HoldsOverlyAlignedChar {142 char c1;143 struct OverlyAlignedChar o;144 char c2;145};146 147void internalStructFunc() {148 // expected-warning@+7{{\149Excessive padding in 'struct X' (6 padding bytes, where 2 is optimal). \150Optimal fields order: \151t, \152c1, \153c2, \154}}155 struct X {156 char c1;157 int t;158 char c2;159 };160 struct X obj;161}162 163void typedefStructFunc() {164 // expected-warning@+7{{\165Excessive padding in 'S' (6 padding bytes, where 2 is optimal). \166Optimal fields order: \167t, \168c1, \169c2, \170}}171 typedef struct {172 char c1;173 int t;174 char c2;175 } S;176 S obj;177}178 179// expected-warning@+7{{\180Excessive padding in 'struct DefaultAttrAlign' (22 padding bytes, where 6 is optimal). \181Optimal fields order: \182i, \183c1, \184c2, \185}}186struct DefaultAttrAlign {187 char c1;188 long long i;189 char c2;190} __attribute__((aligned));191 192// expected-warning@+7{{\193Excessive padding in 'struct SmallArrayShortSandwich' (2 padding bytes, where 0 is optimal). \194Optimal fields order: \195s, \196c1, \197c2, \198}}199struct SmallArrayShortSandwich {200 char c1;201 short s;202 char c2;203} ShortArray[20];204 205// expected-warning@+7{{\206Excessive padding in 'struct SmallArrayInFunc' (2 padding bytes, where 0 is optimal). \207Optimal fields order: \208s, \209c1, \210c2, \211}}212struct SmallArrayInFunc {213 char c1;214 short s;215 char c2;216};217 218void arrayHolder() {219 struct SmallArrayInFunc Arr[15];220}221 222// expected-warning@+7{{\223Excessive padding in 'class VirtualIntSandwich' (10 padding bytes, where 2 is optimal). \224Optimal fields order: \225i, \226c1, \227c2, \228}}229class VirtualIntSandwich {230 virtual void foo() {}231 char c1;232 int i;233 char c2;234};235 236// constructed so as not to have tail padding237// expected-warning@+8{{\238Excessive padding in 'class InnerPaddedB' (6 padding bytes, where 2 is optimal). \239Optimal fields order: \240i1, \241i2, \242c1, \243c2, \244}}245class InnerPaddedB {246 char c1;247 int i1;248 char c2;249 int i2;250};251 252class Empty {}; // no-warning253 254// expected-warning@+7{{\255Excessive padding in 'class LotsOfSpace' (6 padding bytes, where 2 is optimal). \256Optimal fields order: \257i, \258e1, \259e2, \260}}261class LotsOfSpace {262 Empty e1;263 int i;264 Empty e2;265};266 267// expected-warning@+7{{\268Excessive padding in 'struct TypedefSandwich2' (6 padding bytes, where 2 is optimal). \269Optimal fields order: \270t, \271c1, \272c2, \273}}274typedef struct TypedefSandwich2 {275 char c1;276 // expected-warning@+7{{\277Excessive padding in 'TypedefSandwich2::NestedTypedef' (6 padding bytes, where 2 is optimal). \278Optimal fields order: \279i, \280c1, \281c2, \282}}283 typedef struct {284 char c1;285 int i;286 char c2;287 } NestedTypedef;288 NestedTypedef t;289 char c2;290} TypedefSandwich2;291 292template <typename T>293struct Foo {294 // expected-warning@+7{{\295Excessive padding in 'struct Foo<int>::Nested' (6 padding bytes, where 2 is optimal). \296Optimal fields order: \297t, \298c1, \299c2, \300}}301 struct Nested {302 char c1;303 T t;304 char c2;305 };306};307 308struct Holder { // no-warning309 Foo<int>::Nested t1;310 Foo<char>::Nested t2;311};312