When warning about comparing an unsigned int to being >= 0, don't issue a warning if the zero value was an

enum or was expanded from a macro.

Fixes: <rdar://problem/8414119>
llvm-svn: 114695
This commit is contained in:
Ted Kremenek 2010-09-23 21:43:44 +00:00
parent 24fb1d03d3
commit 6274be47fa
2 changed files with 28 additions and 1 deletions

View File

@ -2449,7 +2449,17 @@ bool IsSameFloatAfterCast(const APValue &value,
void AnalyzeImplicitConversions(Sema &S, Expr *E);
bool IsZero(Sema &S, Expr *E) {
static bool IsZero(Sema &S, Expr *E) {
// Suppress cases where we are comparing against an enum constant.
if (const DeclRefExpr *DR =
dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
if (isa<EnumConstantDecl>(DR->getDecl()))
return false;
// Suppress cases where the '0' value is expanded from a macro.
if (E->getLocStart().isMacroID())
return false;
llvm::APSInt Value;
return E->isIntegerConstantExpr(Value, S.Context) && Value == 0;
}

View File

@ -288,3 +288,20 @@ int test6(unsigned i, unsigned power) {
unsigned x = (i < (1 << power) ? i : 0);
return x != 3 ? 1 << power : i;
}
// <rdar://problem/8414119> enum >= (enum)0 comparison should not generate any warnings
enum rdar8414119_Vals { X, Y, Z };
#define ZERO 0
#define CHECK(x) (x >= X)
void rdar8414119_foo(enum rdar8414119_Vals v) {
if (CHECK(v)) // no-warning
return;
if (v >= X) // no-warning
return;
}
int rdar8414119_bar(unsigned x) {
return x >= ZERO; // no-warning
}
#undef ZERO
#undef CHECK