109 lines · cpp
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 std5{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 Value15// 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 supported18};19 20 21enum class EnumClass : std::int16_t22// 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 supported25};26 27enum EnumWithType : std::uint16_t28// 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 supported232};33 34enum EnumWithNegative35// 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 = 12741};42 43enum EnumThatCanBeReducedTo2Bytes44// 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 = 0x6EEE48};49 50enum EnumOnlyNegative51// 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 = -1056};57 58enum CorrectU8 : std::uint8_t59{60 c01 = 10,61 c02 = 1162};63 64enum CorrectU16 : std::uint16_t65{66 c11 = 10,67 c12 = 0xFFFF68};69 70constexpr int getValue()71{72 return 256;73}74 75 76enum CalculatedDueToUnknown1 : unsigned int77// 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 int84// 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 + 24688};89 90enum class IgnoredEnum : std::uint32_t91{92 unused1 = 1,93 unused2 = 294};95 96namespace internal97{98 99enum class IgnoredSecondEnum100{101 unused1 = 1,102 unused2 = 2103};104 105enum class EnumClassWithoutValues : int {};106enum EnumWithoutValues {};107 108}109