1// RUN: %check_clang_tidy -std=c++17-or-later %s performance-enum-size %t -- \
2// RUN: -config="{CheckOptions: {performance-enum-size.EnumIgnoreList: '::IgnoredEnum;IgnoredSecondEnum'}}"
3
4namespace std
5{
6using uint8_t = unsigned char;
7using int8_t = signed char;
8using uint16_t = unsigned short;
9using int16_t = signed short;
10using uint32_t = unsigned int;
11using int32_t = signed int;
12}
13
14enum class Value
15// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: enum 'Value' uses a larger base type ('int', size: 4 bytes) than necessary for its value set, consider using 'std::uint8_t' (1 byte) as the base type to reduce its size [performance-enum-size]
16{
17 supported
18};
19
20
21enum class EnumClass : std::int16_t
22// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: enum 'EnumClass' uses a larger base type ('std::int16_t' (aka 'short'), size: 2 bytes) than necessary for its value set, consider using 'std::uint8_t' (1 byte) as the base type to reduce its size [performance-enum-size]
23{
24 supported
25};
26
27enum EnumWithType : std::uint16_t
28// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'EnumWithType' uses a larger base type ('std::uint16_t' (aka 'unsigned short'), size: 2 bytes) than necessary for its value set, consider using 'std::uint8_t' (1 byte) as the base type to reduce its size [performance-enum-size]
29{
30 supported,
31 supported2
32};
33
34enum EnumWithNegative
35// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'EnumWithNegative' uses a larger base type ('int', size: 4 bytes) than necessary for its value set, consider using 'std::int8_t' (1 byte) as the base type to reduce its size [performance-enum-size]
36{
37 s1 = -128,
38 s2 = -100,
39 s3 = 100,
40 s4 = 127
41};
42
43enum EnumThatCanBeReducedTo2Bytes
44// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'EnumThatCanBeReducedTo2Bytes' uses a larger base type ('int', size: 4 bytes) than necessary for its value set, consider using 'std::int16_t' (2 bytes) as the base type to reduce its size [performance-enum-size]
45{
46 a1 = -128,
47 a2 = 0x6EEE
48};
49
50enum EnumOnlyNegative
51// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'EnumOnlyNegative' uses a larger base type ('int', size: 4 bytes) than necessary for its value set, consider using 'std::int8_t' (1 byte) as the base type to reduce its size [performance-enum-size]
52{
53 b1 = -125,
54 b2 = -50,
55 b3 = -10
56};
57
58enum CorrectU8 : std::uint8_t
59{
60 c01 = 10,
61 c02 = 11
62};
63
64enum CorrectU16 : std::uint16_t
65{
66 c11 = 10,
67 c12 = 0xFFFF
68};
69
70constexpr int getValue()
71{
72 return 256;
73}
74
75
76enum CalculatedDueToUnknown1 : unsigned int
77// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'CalculatedDueToUnknown1' uses a larger base type ('unsigned int', size: 4 bytes) than necessary for its value set, consider using 'std::uint16_t' (2 bytes) as the base type to reduce its size [performance-enum-size]
78{
79 c21 = 10,
80 c22 = getValue()
81};
82
83enum CalculatedDueToUnknown2 : unsigned int
84// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: enum 'CalculatedDueToUnknown2' uses a larger base type ('unsigned int', size: 4 bytes) than necessary for its value set, consider using 'std::uint16_t' (2 bytes) as the base type to reduce its size [performance-enum-size]
85{
86 c31 = 10,
87 c32 = c31 + 246
88};
89
90enum class IgnoredEnum : std::uint32_t
91{
92 unused1 = 1,
93 unused2 = 2
94};
95
96namespace internal
97{
98
99enum class IgnoredSecondEnum
100{
101 unused1 = 1,
102 unused2 = 2
103};
104
105enum class EnumClassWithoutValues : int {};
106enum EnumWithoutValues {};
107
108}
109

source code of clang-tools-extra/test/clang-tidy/checkers/performance/enum-size.cpp