This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

New testcase (compare2.c) for bogus -Wsign-compare warnings


Bootstraps have recently exposed another limitation of the
sign-compare warnings.  Gcc normally excuses "(unsigned) > 64", but it
does not excuse "(unsigned) > (expr ? 64 : 32)".

IMHO, gcc should be able to check that the branches of a ?: stmt are
INTEGER_CST and int_fits_type_p() and let those cases pass also.
Ditto for statement expressions.  Ditto for subexpressions containing
these constructs.

Admittedly, I'm not volunteering to supply a patch to gcc to allow
this.  But I'm hoping a testcase might inspire someone. :-)

Okay to install as gcc.dg/compare2.c ?

		--Kaveh


/* Test for a bogus warning on comparison between signed and unsigned.
   This was inspired by code in gcc. */

/* { dg-do compile } */
/* { dg-options "-Wsign-compare" } */

int tf = 1;

void f(int x, unsigned int y)
{
  /* ?: branches are constants.  */
  x > (tf?64:128); /* { dg-bogus "signed and unsigned" "case 1" } */
  y > (tf?64:128); /* { dg-bogus "signed and unsigned" "case 2" } */

  /* ?: branches are constants.  */
  x > (tf?64:(tf?128:256)); /* { dg-bogus "signed and unsigned" "case 3" } */
  y > (tf?64:(tf?128:256)); /* { dg-bogus "signed and unsigned" "case 4" } */

  /* ?: branches are signed constants.  */
  x > (tf?64:-1); /* { dg-bogus "signed and unsigned" "case 5" } */
  y > (tf?64:-1); /* { dg-warning "signed and unsigned" "case 6" } */

  /* ?: branches are signed constants.  */
  x > (tf?64:(tf?128:-1)); /* { dg-bogus "signed and unsigned" "case 7" } */
  y > (tf?64:(tf?128:-1)); /* { dg-warning "signed and unsigned" "case 8" } */

  /* Statement expression.  */
  x > ({tf; 64;}); /* { dg-bogus "signed and unsigned" "case 9" } */
  y > ({tf; 64;}); /* { dg-bogus "signed and unsigned" "case 10" } */

  /* Statement expression with ?: .  */
  x > ({tf; tf?64:(tf?128:256);}); /* { dg-bogus "signed and unsigned" "case 11" } */
  y > ({tf; tf?64:(tf?128:256);}); /* { dg-bogus "signed and unsigned" "case 12" } */

  /* Statement expression with signed ?:.  */
  x > ({tf; tf?64:-1;}); /* { dg-bogus "signed and unsigned" "case 13" } */
  y > ({tf; tf?64:-1;}); /* { dg-warning "signed and unsigned" "case 14" } */

  /* Statement expression with signed ?:.  */
  x > ({tf; tf?64:(tf?128:-1);}); /* { dg-bogus "signed and unsigned" "case 15" } */
  y > ({tf; tf?64:(tf?128:-1);}); /* { dg-warning "signed and unsigned" "case 16" } */
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]