c++: Fix invalid -Wduplicated-cond warning [PR94265]
authorPatrick Palka <ppalka@redhat.com>
Wed, 25 Mar 2020 16:32:43 +0000 (12:32 -0400)
committerPatrick Palka <ppalka@redhat.com>
Wed, 25 Mar 2020 16:33:12 +0000 (12:33 -0400)
This fixes a false-positive warning from -Wduplicate-cond in the presence of an
if-statement with a non-empty init-statement.  Precisely determining whether a
non-empty init-statement has side effects seems tricky and error-prone, so this
patch takes the route of unconditionally invalidating the condition chain when
it encounters such an if-statement.

gcc/cp/ChangeLog:

PR c++/94265
* parser.c (cp_parser_selection_statement) <case RID_IF>: Invalidate the
current condition chain when the if-statement has a non-empty
init-statement.

gcc/testsuite/ChangeLog:

PR c++/94265
* g++.dg/warn/Wduplicated-cond1.C: New test.

gcc/cp/ChangeLog
gcc/cp/parser.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wduplicated-cond1.C [new file with mode: 0644]

index d9f87eca64932569087ea1e4f93def8a4be1e5f6..34ccb9fa4c0445106bde23c123cd44b13cc009b5 100644 (file)
@@ -1,3 +1,10 @@
+2020-03-25  Patrick Palka  <ppalka@redhat.com>
+
+       PR c++/94265
+       * parser.c (cp_parser_selection_statement) <case RID_IF>: Invalidate the
+       current condition chain when the if-statement has a non-empty
+       init-statement.
+
 2020-03-25  Iain Sandoe  <iain@sandoe.co.uk>
 
        PR c++/94319
index cbd5510a8fb3e4fe67b565a3751bdfd2d5846d02..0536365369162fa1da25ede940ed22dfb31092ce 100644 (file)
@@ -11934,6 +11934,13 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p,
              pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
                       "init-statement in selection statements only available "
                       "with %<-std=c++17%> or %<-std=gnu++17%>");
+           if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
+             {
+               /* A non-empty init-statement can have arbitrary side
+                  effects.  */
+               delete chain;
+               chain = NULL;
+             }
            cp_parser_init_statement (parser, &decl);
          }
 
index 7f2d590871378050ca97a3e33f198657a472e5dc..ddfdaa6da2f767f819c0810e8b6660671e77cdb3 100644 (file)
@@ -1,3 +1,8 @@
+2020-03-25  Patrick Palka  <ppalka@redhat.com>
+
+       PR c++/94265
+       * g++.dg/warn/Wduplicated-cond1.C: New test.
+
 2020-03-25  Martin Sebor  <msebor@redhat.com>
 
        PR tree-optimization/94131
diff --git a/gcc/testsuite/g++.dg/warn/Wduplicated-cond1.C b/gcc/testsuite/g++.dg/warn/Wduplicated-cond1.C
new file mode 100644 (file)
index 0000000..e85920a
--- /dev/null
@@ -0,0 +1,16 @@
+// PR c++/94265
+// { dg-do compile { target c++17 } }
+// { dg-additional-options "-Wduplicated-cond" }
+
+void
+foo ()
+{
+  if (int a = 0; a)
+  { }
+  else if (a = 5; a) // { dg-message "previously used here" }
+  { }
+  else if (; a) // { dg-warning "duplicated .if. condition" }
+  { }
+  else if (int b = ++a; a)
+  { }
+}