From: Ted Kremenek Date: Thu, 23 Sep 2010 21:43:44 +0000 (+0000) Subject: When warning about comparing an unsigned int to being >= 0, don't issue a warning... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e3b159c0d4a93db8586861ff011b5a10de4ae6ef;p=clang 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: git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@114695 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 790e7671ed..c4e86875ed 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -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(E->IgnoreParenImpCasts())) + if (isa(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; } diff --git a/test/Sema/compare.c b/test/Sema/compare.c index b2c3563395..2a6917e607 100644 --- a/test/Sema/compare.c +++ b/test/Sema/compare.c @@ -288,3 +288,20 @@ int test6(unsigned i, unsigned power) { unsigned x = (i < (1 << power) ? i : 0); return x != 3 ? 1 << power : i; } + +// 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 +