hicpp-signed-bitwise.cpp
9.24 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// RUN: %check_clang_tidy %s hicpp-signed-bitwise %t -- -- --target=x86_64-linux
// These could cause false positives and should not be considered.
struct StreamClass {
};
StreamClass &operator<<(StreamClass &os, unsigned int i) {
return os;
}
StreamClass &operator<<(StreamClass &os, int i) {
return os;
}
StreamClass &operator>>(StreamClass &os, unsigned int i) {
return os;
}
StreamClass &operator>>(StreamClass &os, int i) {
return os;
}
struct AnotherStream {
AnotherStream &operator<<(unsigned char c) { return *this; }
AnotherStream &operator<<(signed char c) { return *this; }
AnotherStream &operator>>(unsigned char c) { return *this; }
AnotherStream &operator>>(signed char c) { return *this; }
};
void binary_bitwise() {
int SValue = 42;
int SResult;
unsigned int UValue = 42;
unsigned int UResult;
SResult = SValue & 1;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
SResult = SValue & -1;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
SResult = SValue & SValue;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
UResult = SValue & 1;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
UResult = SValue & -1;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
UResult&= 1;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
UResult = UValue & 1u; // Ok
UResult = UValue & UValue; // Ok
UResult&= 2u; // Ok
unsigned char UByte1 = 0u;
unsigned char UByte2 = 16u;
signed char SByte1 = 0;
signed char SByte2 = 16;
UByte1 = UByte1 & UByte2; // Ok
UByte1 = SByte1 & UByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
UByte1 = SByte1 & SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
SByte1 = SByte1 & SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
// More complex expressions.
UResult = UValue & (SByte1 + (SByte1 | SByte2));
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-2]]:33: warning: use of a signed integer operand with a binary bitwise operator
// The rest is to demonstrate functionality but all operators are matched equally.
// Therefore functionality is the same for all binary operations.
UByte1 = UByte1 | UByte2; // Ok
UByte1 = UByte1 | SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
UByte1|= SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
UByte1|= UByte2; // Ok
UByte1 = UByte1 ^ UByte2; // Ok
UByte1 = UByte1 ^ SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
UByte1^= SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
UByte1^= UByte2; // Ok
UByte1 = UByte1 >> UByte2; // Ok
UByte1 = UByte1 >> SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
UByte1>>= SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
UByte1>>= UByte2; // Ok
UByte1 = UByte1 << UByte2; // Ok
UByte1 = UByte1 << SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
UByte1<<= SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
UByte1<<= UByte2; // Ok
int SignedInt1 = 1 << 12;
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use of a signed integer operand with a binary bitwise operator
int SignedInt2 = 1u << 12;
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use of a signed integer operand with a binary bitwise operator
}
void f1(unsigned char c) {}
void f2(signed char c) {}
void f3(int c) {}
void unary_bitwise() {
unsigned char UByte1 = 0u;
signed char SByte1 = 0;
UByte1 = ~UByte1; // Ok
SByte1 = ~UByte1;
SByte1 = ~SByte1;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a unary bitwise operator
UByte1 = ~SByte1;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a unary bitwise operator
unsigned int UInt = 0u;
int SInt = 0;
f1(~UByte1); // Ok
f1(~SByte1);
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use of a signed integer operand with a unary bitwise operator
f1(~UInt);
f1(~SInt);
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use of a signed integer operand with a unary bitwise operator
f2(~UByte1);
f2(~SByte1);
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use of a signed integer operand with a unary bitwise operator
f2(~UInt);
f2(~SInt);
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use of a signed integer operand with a unary bitwise operator
f3(~UByte1); // Ok
f3(~SByte1);
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use of a signed integer operand with a unary bitwise operator
}
/// HICPP uses these examples to demonstrate the rule.
void standard_examples() {
int i = 3;
unsigned int k = 0u;
int r = i << -1; // Emits -Wshift-count-negative from clang
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use of a signed integer operand with a binary bitwise operator
r = i << 1;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator
r = -1 >> -1; // Emits -Wshift-count-negative from clang
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator
r = -1 >> 1;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator
r = -1 >> i;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator
r = -1 >> -i;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator
r = ~0;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a unary bitwise operator
r = ~0u; // Ok
k = ~k; // Ok
unsigned int u = (-1) & 2u;
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use of a signed integer operand with a binary bitwise operator
u = (-1) | 1u;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator
u = (-1) ^ 1u;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator
}
void streams_should_work() {
StreamClass s;
s << 1u; // Ok
s << 1; // Ok
s >> 1; // Ok
s >> 1u; // Ok
AnotherStream as;
unsigned char uc = 1u;
signed char sc = 1;
as << uc; // Ok
as << sc; // Ok
as >> uc; // Ok
as >> sc; // Ok
}
enum OldEnum {
ValueOne,
ValueTwo,
};
enum OldSigned : int {
IntOne,
IntTwo,
};
void classicEnums() {
OldEnum e1 = ValueOne, e2 = ValueTwo;
int e3; // Using the enum type, results in an error.
e3 = ValueOne | ValueTwo; // Ok
e3 = ValueOne & ValueTwo; // Ok
e3 = ValueOne ^ ValueTwo; // Ok
e3 = e1 | e2; // Ok
e3 = e1 & e2; // Ok
e3 = e1 ^ e2; // Ok
OldSigned s1 = IntOne, s2 = IntTwo;
int s3;
s3 = IntOne | IntTwo; // Signed
// CHECK-MESSAGES: [[@LINE-1]]:8: warning: use of a signed integer operand with a binary bitwise operator
s3|= IntTwo; // Signed
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
s3 = IntOne & IntTwo; // Signed
// CHECK-MESSAGES: [[@LINE-1]]:8: warning: use of a signed integer operand with a binary bitwise operator
s3&= IntTwo; // Signed
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
s3 = IntOne ^ IntTwo; // Signed
// CHECK-MESSAGES: [[@LINE-1]]:8: warning: use of a signed integer operand with a binary bitwise operator
s3^= IntTwo; // Signed
// CHECK-MESSAGES: [[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
s3 = s1 | s2; // Signed
// CHECK-MESSAGES: [[@LINE-1]]:8: warning: use of a signed integer operand with a binary bitwise operator
s3 = s1 & s2; // Signed
// CHECK-MESSAGES: [[@LINE-1]]:8: warning: use of a signed integer operand with a binary bitwise operator
s3 = s1 ^ s2; // Signed
// CHECK-MESSAGES: [[@LINE-1]]:8: warning: use of a signed integer operand with a binary bitwise operator
}
enum EnumConstruction {
one = 1,
two = 2,
test1 = 1 << 12,
// CHECK-MESSAGES: [[@LINE-1]]:11: warning: use of a signed integer operand with a binary bitwise operator
test2 = one << two,
// CHECK-MESSAGES: [[@LINE-1]]:11: warning: use of a signed integer operand with a binary bitwise operator
test3 = 1u << 12,
// CHECK-MESSAGES: [[@LINE-1]]:11: warning: use of a signed integer operand with a binary bitwise operator
};