2017-03-04 02:02:02 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -triple x86_64-unknown-linux -std=c++14 -analyzer-checker=optin.performance -analyzer-config optin.performance.Padding:AllowedPad=2 -verify %s
|
2015-12-15 05:38:59 +08:00
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'struct IntSandwich' (6 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
i, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct IntSandwich {
|
|
|
|
char c1;
|
|
|
|
int i;
|
|
|
|
char c2;
|
|
|
|
};
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'struct TurDuckHen' (6 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
i, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct TurDuckHen {
|
|
|
|
char c1;
|
|
|
|
struct IntSandwich i;
|
|
|
|
char c2;
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(2)
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+11{{\
|
|
|
|
Excessive padding in 'struct SmallIntSandwich' (4 padding bytes, where 0 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
i1, \
|
|
|
|
i2, \
|
|
|
|
i3, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
c3, \
|
|
|
|
c4, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct SmallIntSandwich {
|
|
|
|
char c1;
|
|
|
|
int i1;
|
|
|
|
char c2;
|
|
|
|
int i2;
|
|
|
|
char c3;
|
|
|
|
int i3;
|
|
|
|
char c4;
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
union SomeUnion { // no-warning
|
|
|
|
char c;
|
|
|
|
short s;
|
|
|
|
int i;
|
|
|
|
};
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'struct HoldsAUnion' (6 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
u, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct HoldsAUnion {
|
|
|
|
char c1;
|
|
|
|
union SomeUnion u;
|
|
|
|
char c2;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SmallCharArray { // no-warning
|
|
|
|
char c[5];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MediumIntArray { // no-warning
|
|
|
|
int i[5];
|
|
|
|
};
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'struct StructSandwich' (6 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
m, \
|
|
|
|
s, \
|
|
|
|
s2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct StructSandwich {
|
|
|
|
struct SmallCharArray s;
|
|
|
|
struct MediumIntArray m;
|
|
|
|
struct SmallCharArray s2;
|
|
|
|
};
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'TypedefSandwich' (6 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
i, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
typedef struct {
|
|
|
|
char c1;
|
|
|
|
int i;
|
|
|
|
char c2;
|
|
|
|
} TypedefSandwich;
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'struct StructAttrAlign' (10 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
i, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct StructAttrAlign {
|
|
|
|
char c1;
|
|
|
|
int i;
|
|
|
|
char c2;
|
|
|
|
} __attribute__((aligned(8)));
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+8{{\
|
|
|
|
Excessive padding in 'struct OverlyAlignedChar' (8185 padding bytes, where 4089 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
c, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
x, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct OverlyAlignedChar {
|
|
|
|
char c1;
|
|
|
|
int x;
|
|
|
|
char c2;
|
|
|
|
char c __attribute__((aligned(4096)));
|
|
|
|
};
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'struct HoldsOverlyAlignedChar' (8190 padding bytes, where 4094 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
o, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct HoldsOverlyAlignedChar {
|
|
|
|
char c1;
|
|
|
|
struct OverlyAlignedChar o;
|
|
|
|
char c2;
|
|
|
|
};
|
|
|
|
|
|
|
|
void internalStructFunc() {
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'struct X' (6 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
t, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct X {
|
|
|
|
char c1;
|
|
|
|
int t;
|
|
|
|
char c2;
|
|
|
|
};
|
|
|
|
struct X obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
void typedefStructFunc() {
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'S' (6 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
t, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
typedef struct {
|
|
|
|
char c1;
|
|
|
|
int t;
|
|
|
|
char c2;
|
|
|
|
} S;
|
|
|
|
S obj;
|
|
|
|
}
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'struct DefaultAttrAlign' (22 padding bytes, where 6 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
i, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct DefaultAttrAlign {
|
|
|
|
char c1;
|
|
|
|
long long i;
|
|
|
|
char c2;
|
|
|
|
} __attribute__((aligned));
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'struct SmallArrayShortSandwich' (2 padding bytes, where 0 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
s, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct SmallArrayShortSandwich {
|
|
|
|
char c1;
|
|
|
|
short s;
|
|
|
|
char c2;
|
|
|
|
} ShortArray[20];
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'struct SmallArrayInFunc' (2 padding bytes, where 0 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
s, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct SmallArrayInFunc {
|
|
|
|
char c1;
|
|
|
|
short s;
|
|
|
|
char c2;
|
|
|
|
};
|
|
|
|
|
|
|
|
void arrayHolder() {
|
|
|
|
struct SmallArrayInFunc Arr[15];
|
|
|
|
}
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'class VirtualIntSandwich' (10 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
i, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
class VirtualIntSandwich {
|
|
|
|
virtual void foo() {}
|
|
|
|
char c1;
|
|
|
|
int i;
|
|
|
|
char c2;
|
|
|
|
};
|
|
|
|
|
|
|
|
// constructed so as not to have tail padding
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+8{{\
|
|
|
|
Excessive padding in 'class InnerPaddedB' (6 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
i1, \
|
|
|
|
i2, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
class InnerPaddedB {
|
|
|
|
char c1;
|
|
|
|
int i1;
|
|
|
|
char c2;
|
|
|
|
int i2;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Empty {}; // no-warning
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'class LotsOfSpace' (6 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
i, \
|
|
|
|
e1, \
|
|
|
|
e2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
class LotsOfSpace {
|
|
|
|
Empty e1;
|
|
|
|
int i;
|
|
|
|
Empty e2;
|
|
|
|
};
|
|
|
|
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'TypedefSandwich2' (6 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
t, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
typedef struct {
|
|
|
|
char c1;
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'TypedefSandwich2::NestedTypedef' (6 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
i, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
typedef struct {
|
|
|
|
char c1;
|
|
|
|
int i;
|
|
|
|
char c2;
|
|
|
|
} NestedTypedef;
|
|
|
|
NestedTypedef t;
|
|
|
|
char c2;
|
|
|
|
} TypedefSandwich2;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct Foo {
|
2016-08-16 04:16:13 +08:00
|
|
|
// expected-warning@+7{{\
|
|
|
|
Excessive padding in 'struct Foo<int>::Nested' (6 padding bytes, where 2 is optimal). \
|
|
|
|
Optimal fields order: \
|
|
|
|
t, \
|
|
|
|
c1, \
|
|
|
|
c2, \
|
|
|
|
}}
|
2015-12-15 05:38:59 +08:00
|
|
|
struct Nested {
|
|
|
|
char c1;
|
|
|
|
T t;
|
|
|
|
char c2;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Holder { // no-warning
|
|
|
|
Foo<int>::Nested t1;
|
|
|
|
Foo<char>::Nested t2;
|
|
|
|
};
|