r177190 - Enhance -Wtautological-constant-out-of-range-compare to include the name of the enum constant.

Jordan Rose jordan_rose at apple.com
Fri Mar 15 14:54:07 PDT 2013


On Mar 15, 2013, at 14:50 , Ted Kremenek <kremenek at apple.com> wrote:

> Author: kremenek
> Date: Fri Mar 15 16:50:10 2013
> New Revision: 177190
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=177190&view=rev
> Log:
> Enhance -Wtautological-constant-out-of-range-compare to include the name of the enum constant.
> 
> This is QoI.  Fixes <rdar://problem/13076064>.
> 
> Modified:
>    cfe/trunk/lib/Sema/SemaChecking.cpp
>    cfe/trunk/test/Sema/compare.c
>    cfe/trunk/test/SemaCXX/compare.cpp
>    cfe/trunk/test/SemaCXX/warn-enum-compare.cpp
> 
> Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=177190&r1=177189&r2=177190&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaChecking.cpp Fri Mar 15 16:50:10 2013
> @@ -4495,9 +4495,22 @@ static void DiagnoseOutOfRangeComparison
>     else // op == BO_GT || op == BO_GE
>       IsTrue = PositiveConstant;
>   }
> -  SmallString<16> PrettySourceValue(Value.toString(10));
> +
> +  // If this is a comparison to an enum constant, include that
> +  // constant in the diagnostic.
> +  const EnumConstantDecl *ED = 0;
> +  if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Constant))
> +    ED = dyn_cast<EnumConstantDecl>(DR->getDecl());
> +
> +  SmallString<64> PrettySourceValue;
> +  llvm::raw_svector_ostream OS(PrettySourceValue);
> +  if (ED)
> +    OS << '\'' << ED->getName() << "' (" << Value << ")";
> +  else
> +    OS << Value;

This could just be 'OS << *ED << " (" << Value << ")", right?

Also, it's possible you could bake this into the diagnostic format, but I guess that's probably not worth it.


>   S.Diag(E->getOperatorLoc(), diag::warn_out_of_range_compare)
> -      << PrettySourceValue << OtherT << IsTrue
> +      << OS.str() << OtherT << IsTrue
>       << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
> }
> 
> 
> Modified: cfe/trunk/test/Sema/compare.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/compare.c?rev=177190&r1=177189&r2=177190&view=diff
> ==============================================================================
> --- cfe/trunk/test/Sema/compare.c (original)
> +++ cfe/trunk/test/Sema/compare.c Fri Mar 15 16:50:10 2013
> @@ -93,8 +93,8 @@ int ints(long a, unsigned long b) {
>          // (C,b)
>          (C == (unsigned long) b) +
>          (C == (unsigned int) b) +
> -         (C == (unsigned short) b) + // expected-warning {{comparison of constant 65536 with expression of type 'unsigned short' is always false}}
> -         (C == (unsigned char) b) + // expected-warning {{comparison of constant 65536 with expression of type 'unsigned char' is always false}}
> +         (C == (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
> +         (C == (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
>          ((long) C == b) +
>          ((int) C == b) +
>          ((short) C == b) +
> @@ -105,8 +105,8 @@ int ints(long a, unsigned long b) {
>          ((signed char) C == (unsigned char) b) +
>          (C < (unsigned long) b) +
>          (C < (unsigned int) b) +
> -         (C < (unsigned short) b) + // expected-warning {{comparison of constant 65536 with expression of type 'unsigned short' is always false}}
> -         (C < (unsigned char) b) + // expected-warning {{comparison of constant 65536 with expression of type 'unsigned char' is always false}}
> +         (C < (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
> +         (C < (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
>          ((long) C < b) +
>          ((int) C < b) +
>          ((short) C < b) +
> @@ -123,8 +123,8 @@ int ints(long a, unsigned long b) {
>          (a == (unsigned char) C) +
>          ((long) a == C) +
>          ((int) a == C) +
> -         ((short) a == C) + // expected-warning {{comparison of constant 65536 with expression of type 'short' is always false}}
> -         ((signed char) a == C) + // expected-warning {{comparison of constant 65536 with expression of type 'signed char' is always false}}
> +         ((short) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always false}}
> +         ((signed char) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always false}}
>          ((long) a == (unsigned long) C) +
>          ((int) a == (unsigned int) C) +
>          ((short) a == (unsigned short) C) +
> @@ -135,8 +135,8 @@ int ints(long a, unsigned long b) {
>          (a < (unsigned char) C) +
>          ((long) a < C) +
>          ((int) a < C) +
> -         ((short) a < C) + // expected-warning {{comparison of constant 65536 with expression of type 'short' is always true}}
> -         ((signed char) a < C) + // expected-warning {{comparison of constant 65536 with expression of type 'signed char' is always true}}
> +         ((short) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always true}}
> +         ((signed char) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always true}}
>          ((long) a < (unsigned long) C) +  // expected-warning {{comparison of integers of different signs}}
>          ((int) a < (unsigned int) C) +  // expected-warning {{comparison of integers of different signs}}
>          ((short) a < (unsigned short) C) +
> 
> Modified: cfe/trunk/test/SemaCXX/compare.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/compare.cpp?rev=177190&r1=177189&r2=177190&view=diff
> ==============================================================================
> --- cfe/trunk/test/SemaCXX/compare.cpp (original)
> +++ cfe/trunk/test/SemaCXX/compare.cpp Fri Mar 15 16:50:10 2013
> @@ -89,8 +89,8 @@ int test0(long a, unsigned long b) {
>          // (C,b)
>          (C == (unsigned long) b) +
>          (C == (unsigned int) b) +
> -         (C == (unsigned short) b) + // expected-warning {{comparison of constant 65536 with expression of type 'unsigned short' is always false}}
> -         (C == (unsigned char) b) +  // expected-warning {{comparison of constant 65536 with expression of type 'unsigned char' is always false}}
> +         (C == (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
> +         (C == (unsigned char) b) +  // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
>          ((long) C == b) +
>          ((int) C == b) +
>          ((short) C == b) +
> @@ -101,8 +101,8 @@ int test0(long a, unsigned long b) {
>          ((signed char) C == (unsigned char) b) +
>          (C < (unsigned long) b) +
>          (C < (unsigned int) b) +
> -         (C < (unsigned short) b) + // expected-warning {{comparison of constant 65536 with expression of type 'unsigned short' is always false}}
> -         (C < (unsigned char) b) + // expected-warning {{comparison of constant 65536 with expression of type 'unsigned char' is always false}}
> +         (C < (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}}
> +         (C < (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}}
>          ((long) C < b) +
>          ((int) C < b) +
>          ((short) C < b) +
> @@ -119,8 +119,8 @@ int test0(long a, unsigned long b) {
>          (a == (unsigned char) C) +
>          ((long) a == C) +
>          ((int) a == C) +
> -         ((short) a == C) + // expected-warning {{comparison of constant 65536 with expression of type 'short' is always false}}
> -         ((signed char) a == C) + // expected-warning {{comparison of constant 65536 with expression of type 'signed char' is always false}}
> +         ((short) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always false}}
> +         ((signed char) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always false}}
>          ((long) a == (unsigned long) C) +
>          ((int) a == (unsigned int) C) +
>          ((short) a == (unsigned short) C) +
> @@ -131,8 +131,8 @@ int test0(long a, unsigned long b) {
>          (a < (unsigned char) C) +
>          ((long) a < C) +
>          ((int) a < C) +
> -         ((short) a < C) + // expected-warning {{comparison of constant 65536 with expression of type 'short' is always true}}
> -         ((signed char) a < C) + // expected-warning {{comparison of constant 65536 with expression of type 'signed char' is always true}}
> +         ((short) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always true}}
> +         ((signed char) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always true}}
>          ((long) a < (unsigned long) C) +  // expected-warning {{comparison of integers of different signs}}
>          ((int) a < (unsigned int) C) +  // expected-warning {{comparison of integers of different signs}}
>          ((short) a < (unsigned short) C) +
> 
> Modified: cfe/trunk/test/SemaCXX/warn-enum-compare.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-enum-compare.cpp?rev=177190&r1=177189&r2=177190&view=diff
> ==============================================================================
> --- cfe/trunk/test/SemaCXX/warn-enum-compare.cpp (original)
> +++ cfe/trunk/test/SemaCXX/warn-enum-compare.cpp Fri Mar 15 16:50:10 2013
> @@ -39,8 +39,8 @@ void test () {
>   while (b == c);
>   while (B1 == name1::B2);
>   while (B2 == name2::B1);
> -  while (x == AnonAA); // expected-warning {{comparison of constant 42 with expression of type 'Foo' is always false}}
> -  while (AnonBB == y); // expected-warning {{comparison of constant 45 with expression of type 'Bar' is always false}}
> +  while (x == AnonAA); // expected-warning {{comparison of constant 'AnonAA' (42) with expression of type 'Foo' is always false}}
> +  while (AnonBB == y); // expected-warning {{comparison of constant 'AnonBB' (45) with expression of type 'Bar' is always false}}
>   while (AnonAA == AnonAB);
>   while (AnonAB == AnonBA);
>   while (AnonBB == AnonAA);
> 
> 
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130315/9ba06328/attachment.html>


More information about the cfe-commits mailing list