241 lines · cpp
1// RUN: %check_clang_tidy %s hicpp-signed-bitwise %t -- -- --target=x86_64-linux2 3// These could cause false positives and should not be considered.4struct StreamClass {5};6StreamClass &operator<<(StreamClass &os, unsigned int i) {7 return os;8}9StreamClass &operator<<(StreamClass &os, int i) {10 return os;11}12StreamClass &operator>>(StreamClass &os, unsigned int i) {13 return os;14}15StreamClass &operator>>(StreamClass &os, int i) {16 return os;17}18struct AnotherStream {19 AnotherStream &operator<<(unsigned char c) { return *this; }20 AnotherStream &operator<<(signed char c) { return *this; }21 22 AnotherStream &operator>>(unsigned char c) { return *this; }23 AnotherStream &operator>>(signed char c) { return *this; }24};25 26void binary_bitwise() {27 int SValue = 42;28 int SResult;29 30 unsigned int UValue = 42;31 unsigned int UResult;32 33 SResult = SValue & 1;34 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator35 SResult = SValue & -1;36 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator37 SResult = SValue & SValue;38 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator39 40 UResult = SValue & 1;41 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator42 UResult = SValue & -1;43 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator44 UResult&= 1;45 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator46 47 UResult = UValue & 1u; // Ok48 UResult = UValue & UValue; // Ok49 UResult&= 2u; // Ok50 51 unsigned char UByte1 = 0u;52 unsigned char UByte2 = 16u;53 signed char SByte1 = 0;54 signed char SByte2 = 16;55 56 UByte1 = UByte1 & UByte2; // Ok57 UByte1 = SByte1 & UByte2;58 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator59 UByte1 = SByte1 & SByte2;60 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator61 SByte1 = SByte1 & SByte2;62 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator63 64 // More complex expressions.65 UResult = UValue & (SByte1 + (SByte1 | SByte2));66 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use of a signed integer operand with a binary bitwise operator67 // CHECK-MESSAGES: :[[@LINE-2]]:33: warning: use of a signed integer operand with a binary bitwise operator68 69 // The rest is to demonstrate functionality but all operators are matched equally.70 // Therefore functionality is the same for all binary operations.71 UByte1 = UByte1 | UByte2; // Ok72 UByte1 = UByte1 | SByte2;73 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use of a signed integer operand with a binary bitwise operator74 UByte1|= SByte2;75 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator76 UByte1|= UByte2; // Ok77 78 UByte1 = UByte1 ^ UByte2; // Ok79 UByte1 = UByte1 ^ SByte2;80 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use of a signed integer operand with a binary bitwise operator81 UByte1^= SByte2;82 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator83 UByte1^= UByte2; // Ok84 85 UByte1 = UByte1 >> UByte2; // Ok86 UByte1 = UByte1 >> SByte2;87 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use of a signed integer operand with a binary bitwise operator88 UByte1>>= SByte2;89 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator90 UByte1>>= UByte2; // Ok91 92 UByte1 = UByte1 << UByte2; // Ok93 UByte1 = UByte1 << SByte2;94 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use of a signed integer operand with a binary bitwise operator95 UByte1<<= SByte2;96 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator97 UByte1<<= UByte2; // Ok98 99 int SignedInt1 = 1 << 12;100 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use of a signed integer operand with a binary bitwise operator101 int SignedInt2 = 1u << 12;102 // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: use of a signed integer operand with a binary bitwise operator103}104 105void f1(unsigned char c) {}106void f2(signed char c) {}107void f3(int c) {}108 109void unary_bitwise() {110 unsigned char UByte1 = 0u;111 signed char SByte1 = 0;112 113 UByte1 = ~UByte1; // Ok114 SByte1 = ~UByte1;115 SByte1 = ~SByte1;116 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a unary bitwise operator117 UByte1 = ~SByte1;118 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a unary bitwise operator119 120 unsigned int UInt = 0u;121 int SInt = 0;122 123 f1(~UByte1); // Ok124 f1(~SByte1);125 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a unary bitwise operator126 f1(~UInt);127 f1(~SInt);128 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a unary bitwise operator129 f2(~UByte1);130 f2(~SByte1);131 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a unary bitwise operator132 f2(~UInt);133 f2(~SInt);134 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a unary bitwise operator135 f3(~UByte1); // Ok136 f3(~SByte1);137 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a unary bitwise operator138}139 140/// HICPP uses these examples to demonstrate the rule.141void standard_examples() {142 int i = 3;143 unsigned int k = 0u;144 145 int r = i << -1; // Emits -Wshift-count-negative from clang146 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use of a signed integer operand with a binary bitwise operator147 r = i << 1;148 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator149 150 r = -1 >> -1; // Emits -Wshift-count-negative from clang151 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator152 r = -1 >> 1;153 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator154 155 r = -1 >> i;156 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator157 r = -1 >> -i;158 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator159 160 r = ~0;161 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use of a signed integer operand with a unary bitwise operator162 r = ~0u; // Ok163 k = ~k; // Ok164 165 unsigned int u = (-1) & 2u;166 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use of a signed integer operand with a binary bitwise operator167 u = (-1) | 1u;168 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator169 u = (-1) ^ 1u;170 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator171}172 173void streams_should_work() {174 StreamClass s;175 s << 1u; // Ok176 s << 1; // Ok177 s >> 1; // Ok178 s >> 1u; // Ok179 180 AnotherStream as;181 unsigned char uc = 1u;182 signed char sc = 1;183 as << uc; // Ok184 as << sc; // Ok185 as >> uc; // Ok186 as >> sc; // Ok187}188 189enum OldEnum {190 ValueOne,191 ValueTwo,192};193 194enum OldSigned : int {195 IntOne,196 IntTwo,197};198 199void classicEnums() {200 OldEnum e1 = ValueOne, e2 = ValueTwo;201 int e3; // Using the enum type, results in an error.202 e3 = ValueOne | ValueTwo; // Ok203 e3 = ValueOne & ValueTwo; // Ok204 e3 = ValueOne ^ ValueTwo; // Ok205 e3 = e1 | e2; // Ok206 e3 = e1 & e2; // Ok207 e3 = e1 ^ e2; // Ok208 209 OldSigned s1 = IntOne, s2 = IntTwo;210 int s3;211 s3 = IntOne | IntTwo; // Signed212 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: use of a signed integer operand with a binary bitwise operator213 s3|= IntTwo; // Signed214 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator215 s3 = IntOne & IntTwo; // Signed216 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: use of a signed integer operand with a binary bitwise operator217 s3&= IntTwo; // Signed218 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator219 s3 = IntOne ^ IntTwo; // Signed220 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: use of a signed integer operand with a binary bitwise operator221 s3^= IntTwo; // Signed222 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator223 s3 = s1 | s2; // Signed224 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: use of a signed integer operand with a binary bitwise operator225 s3 = s1 & s2; // Signed226 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: use of a signed integer operand with a binary bitwise operator227 s3 = s1 ^ s2; // Signed228 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: use of a signed integer operand with a binary bitwise operator229}230 231enum EnumConstruction {232 one = 1,233 two = 2,234 test1 = 1 << 12,235 // CHECK-MESSAGES: [[@LINE-1]]:11: warning: use of a signed integer operand with a binary bitwise operator236 test2 = one << two,237 // CHECK-MESSAGES: [[@LINE-1]]:11: warning: use of a signed integer operand with a binary bitwise operator238 test3 = 1u << 12,239 // CHECK-MESSAGES: [[@LINE-1]]:17: warning: use of a signed integer operand with a binary bitwise operator240};241