Skip to content

Commit f819dbf

Browse files
committed
Classify (small unsigned bitfield) < 0 comparisons under
-Wtautological-unsigned-zero-compare not under -Wtautological-value-range-compare.
1 parent cff6dda commit f819dbf

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10944,6 +10944,12 @@ static bool CheckTautologicalComparison(Sema &S, BinaryOperator *E,
1094410944
if (InRange && IsEnumConstOrFromMacro(S, Constant))
1094510945
return false;
1094610946

10947+
// A comparison of an unsigned bit-field against 0 is really a type problem,
10948+
// even though at the type level the bit-field might promote to 'signed int'.
10949+
if (Other->refersToBitField() && InRange && Value == 0 &&
10950+
Other->getType()->isUnsignedIntegerOrEnumerationType())
10951+
TautologicalTypeCompare = true;
10952+
1094710953
// If this is a comparison to an enum constant, include that
1094810954
// constant in the diagnostic.
1094910955
const EnumConstantDecl *ED = nullptr;

clang/test/Sema/compare.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,20 @@ int test5(unsigned int x) {
285285
&& (0 <= x); // expected-warning {{comparison of 0 <= unsigned expression is always true}}
286286
}
287287

288+
struct bitfield {
289+
int a : 3;
290+
unsigned b : 3;
291+
long c : 40;
292+
unsigned long d : 40;
293+
};
294+
295+
void test5a(struct bitfield a) {
296+
if (a.a < 0) {}
297+
if (a.b < 0) {} // expected-warning {{comparison of unsigned expression < 0 is always false}}
298+
if (a.c < 0) {}
299+
if (a.d < 0) {} // expected-warning {{comparison of unsigned expression < 0 is always false}}
300+
}
301+
288302
int test6(unsigned i, unsigned power) {
289303
unsigned x = (i < (1 << power) ? i : 0);
290304
return x != 3 ? 1 << power : i;

0 commit comments

Comments
 (0)