The problem here is c_parser_skip_until_found stops at a closing nesting
delimiter without consuming it. So if we don't consume it in
c_parser_gimple_compound_statement, we would go into an infinite loop. The C
parser similar code in c_parser_statement_after_labels to handle this specific
case too.

        PR c/117741

gcc/c/ChangeLog:

        * gimple-parser.cc (c_parser_gimple_compound_statement): Handle
        CPP_CLOSE_PAREN/CPP_CLOSE_SQUARE with an error and skipping the token.

gcc/testsuite/ChangeLog:

        * gcc.dg/gimplefe-54.c: New test.

Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com>
---
 gcc/c/gimple-parser.cc             | 10 ++++++++++
 gcc/testsuite/gcc.dg/gimplefe-54.c | 10 ++++++++++
 2 files changed, 20 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/gimplefe-54.c

diff --git a/gcc/c/gimple-parser.cc b/gcc/c/gimple-parser.cc
index 81f3921c876..4763cf23313 100644
--- a/gcc/c/gimple-parser.cc
+++ b/gcc/c/gimple-parser.cc
@@ -664,6 +664,16 @@ c_parser_gimple_compound_statement (gimple_parser &parser, 
gimple_seq *seq)
            break;
          }
 
+       case CPP_CLOSE_PAREN:
+       case CPP_CLOSE_SQUARE:
+         /* Avoid infinite loop in error recovery:
+            c_parser_skip_until_found stops at a closing nesting
+            delimiter without consuming it, but here we need to consume
+            it to proceed further.  */
+         c_parser_error (parser, "expected statement");
+         c_parser_consume_token (parser);
+       break;
+
        default:
 expr_stmt:
          c_parser_gimple_statement (parser, seq);
diff --git a/gcc/testsuite/gcc.dg/gimplefe-54.c 
b/gcc/testsuite/gcc.dg/gimplefe-54.c
new file mode 100644
index 00000000000..71a49ac39c2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/gimplefe-54.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-fgimple" } */
+
+/* PR c/117741 */
+/* Make sure after a parsing error we
+   don't go into an infinite loop. */
+
+int i;
+void __GIMPLE foo() {
+  i = ) /* { dg-error "" } */
-- 
2.43.0

Reply via email to