altera-struct-pack-align.cpp
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// RUN: %check_clang_tidy %s altera-struct-pack-align %t -- -header-filter=.*
// Struct needs both alignment and packing
struct error {
char a;
double b;
char c;
};
// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'error' is inefficient due to padding; only needs 10 bytes but is using 24 bytes [altera-struct-pack-align]
// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((packed))" to reduce the amount of padding applied to struct 'error'
// CHECK-MESSAGES: :[[@LINE-7]]:8: warning: accessing fields in struct 'error' is inefficient due to poor alignment; currently aligned to 8 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]
// CHECK-MESSAGES: :[[@LINE-8]]:8: note: use "__attribute__((aligned(16)))" to align struct 'error' to 16 bytes
// CHECK-FIXES: __attribute__((packed))
// CHECK-FIXES: __attribute__((aligned(16)));
// Struct is explicitly packed, but needs alignment
struct error_packed {
char a;
double b;
char c;
} __attribute__((packed));
// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'error_packed' is inefficient due to poor alignment; currently aligned to 1 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]
// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((aligned(16)))" to align struct 'error_packed' to 16 bytes
// CHECK-FIXES: __attribute__((aligned(16)))
// Struct is properly packed, but needs alignment
struct align_only {
char a;
char b;
char c;
char d;
int e;
double f;
};
// CHECK-MESSAGES: :[[@LINE-8]]:8: warning: accessing fields in struct 'align_only' is inefficient due to poor alignment; currently aligned to 8 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]
// CHECK-MESSAGES: :[[@LINE-9]]:8: note: use "__attribute__((aligned(16)))" to align struct 'align_only' to 16 bytes
// CHECK-FIXES: __attribute__((aligned(16)));
// Struct is perfectly packed but wrongly aligned
struct bad_align {
char a;
double b;
char c;
} __attribute__((packed)) __attribute__((aligned(8)));
// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'bad_align' is inefficient due to poor alignment; currently aligned to 8 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]
// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((aligned(16)))" to align struct 'bad_align' to 16 bytes
// CHECK-FIXES: __attribute__((aligned(16)));
struct bad_align2 {
char a;
double b;
char c;
} __attribute__((packed)) __attribute__((aligned(32)));
// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'bad_align2' is inefficient due to poor alignment; currently aligned to 32 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]
// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((aligned(16)))" to align struct 'bad_align2' to 16 bytes
// CHECK-FIXES: __attribute__((aligned(16)));
struct bad_align3 {
char a;
double b;
char c;
} __attribute__((packed)) __attribute__((aligned(4)));
// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'bad_align3' is inefficient due to poor alignment; currently aligned to 4 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]
// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((aligned(16)))" to align struct 'bad_align3' to 16 bytes
// CHECK-FIXES: __attribute__((aligned(16)));
// Struct is both perfectly packed and aligned
struct success {
char a;
double b;
char c;
} __attribute__((packed)) __attribute__((aligned(16)));
//Should take 10 bytes and be aligned to 16 bytes
// Struct is properly packed, and explicitly aligned
struct success2 {
int a;
int b;
int c;
} __attribute__((aligned(16)));
// If struct is properly aligned, packing not needed
struct success3 {
char a;
double b;
char c;
} __attribute__((aligned(16)));
// If struct is templated, warnings should not be triggered
template <typename A, typename B>
struct success4 {
A a;
B b;
int c;
};
// Warnings should not trigger on struct instantiations
void no_trigger_on_instantiation() {
struct bad_align3 instantiated { 'a', 0.001, 'b' };
}