On 10/24/2016 07:59 AM, Marek Polacek wrote:
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.
Ah, okay. That should limit the number of these false positives.
(I saw -O2 in dg-options and assumed it was important. It sounds
like the test case should pass even with -O1).
But even without constant propagation there will be similar cases
(though probably less pervasive). For instance, if j were defined
to something like this:
const int j = 4 == sizeof (size_t);
Martin