Implement -Wduplicated-branches (PR c/64279)

Marek Polacek polacek@redhat.com
Mon Oct 24 13:59:00 GMT 2016


On Thu, Oct 20, 2016 at 02:21:42PM -0600, Martin Sebor wrote:
> > --- gcc/testsuite/c-c++-common/Wduplicated-branches-1.c
> > +++ gcc/testsuite/c-c++-common/Wduplicated-branches-1.c
> > @@ -0,0 +1,187 @@
> > +/* PR c/64279 */
> > +/* { dg-do compile } */
> > +/* { dg-options "-Wduplicated-branches -O2" } */
> > +
> > +extern void foo (int);
> > +extern int g;
> > +extern int a[10];
> > +
> > +int
> > +f (int i, int *p)
> > +{
> > +  const int j = 0;
> > +  if (j == 0)
> > +    {
> > +      if (i > 10) /* { dg-warning "this condition has identical branches" } */
> > +	/* Optimizers can figure out that this is 1.  */
> > +	*p = j * 2 + 1;
> > +      else
> > +	*p = 1;
> > +    }
> 
> I wonder if this test case (perhaps with a slight modification)
> illustrates the concern Jeff raised.  Suppose j is an argument
> to the function whose value of zero is determined by constant
> propagation.  Such code is not uncommon but will presumably be
> diagnosed, which in all likelihood will be considered a false
> positive.  I don't have a sense of how pervasive such cases
> might be.  Do you have any data from projects other than GCC?
> (Since there are no fixes in the patch I assume it didn't find
> any bugs in GCC itself.)

The case above is just a case where with -O GCC can figure out what the value
of a const-qualified variable is, see decl_constant_value_for_optimization.
Since the warning is implemented even before gimplifying, optimizations like
CP don't come into play yet.

I don't have data from anything else than GCC itself.  It hasn't found any
bugs in GCC (yet), but the codebase was recently scanned by the PVS tool
and the bugs were fixed.

	Marek



More information about the Gcc-patches mailing list