]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR c/30552 (gcc crashes when compiling examples with GNU statement expressions...
authorDavid Pagan <dave.pagan@oracle.com>
Wed, 2 May 2018 17:22:26 +0000 (17:22 +0000)
committerJeff Law <law@gcc.gnu.org>
Wed, 2 May 2018 17:22:26 +0000 (11:22 -0600)
PR c/30552
* c-decl.c (old_style_parameter_scope): New function.
* c-parser.c (c_parser_postfix_expression): Check for statement
expressions in old-style function parameter list declarations.
* c-parser.h (old_style_parameter_scope): New extern declaration.

PR c/30552
* gcc.dg/noncompile/pr30552-1.c: New test.
* gcc.dg/noncompile/pr30552-2.c: New test.
* gcc.dg/noncompile/pr30552-3.c: New test.
* gcc.dg/noncompile/pr30552-4.c: New test.

From-SVN: r259849

gcc/c/ChangeLog
gcc/c/c-decl.c
gcc/c/c-parser.c
gcc/c/c-parser.h
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/noncompile/pr30552-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/noncompile/pr30552-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/noncompile/pr30552-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/noncompile/pr30552-4.c [new file with mode: 0644]

index 7a3cf3f93ebe6b2b2b956111162624c1cb8c66be..d88d70d99fc7a2aac5864df1c9a737478d885de3 100644 (file)
@@ -1,3 +1,11 @@
+2018-05-02  David Pagan  <dave.pagan@oracle.com>
+
+       PR c/30552
+       * c-decl.c (old_style_parameter_scope): New function.
+       * c-parser.c (c_parser_postfix_expression): Check for statement
+       expressions in old-style function parameter list declarations.
+       * c-parser.h (old_style_parameter_scope): New extern declaration.
+
 2018-04-25  Jakub Jelinek  <jakub@redhat.com>
 
        PR sanitizer/84307
index 7255588ac1e0f87a581b52ce1129ea916109877c..3c4b18edf562b18a108e48df20d3bec0713377ee 100644 (file)
@@ -952,6 +952,17 @@ global_bindings_p (void)
   return current_scope == file_scope;
 }
 
+/* Return true if we're declaring parameters in an old-style function
+   declaration.  */
+
+bool
+old_style_parameter_scope (void)
+{
+  /* If processing parameters and there is no function statement list, we
+   * have an old-style function declaration.  */
+  return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
+}
+
 void
 keep_next_level (void)
 {
index 47720861d3f22ea1ec414c0f134004b355798df3..6b41a615dbd480559792aa62adcc1e7acb91d57b 100644 (file)
@@ -7930,7 +7930,10 @@ c_parser_postfix_expression (c_parser *parser)
          c_parser_consume_token (parser);
          brace_loc = c_parser_peek_token (parser)->location;
          c_parser_consume_token (parser);
-         if (!building_stmt_list_p ())
+         /* If we've not yet started the current function's statement list,
+            or we're in the parameter scope of an old-style function
+            declaration, statement expressions are not allowed.  */
+         if (!building_stmt_list_p () || old_style_parameter_scope ())
            {
              error_at (loc, "braced-group within expression allowed "
                        "only inside a function");
index da77f68eb1c5645f2db9143958273476ab8cb718..c9d38ace96c918044228d8070461e159afabd674 100644 (file)
@@ -155,6 +155,9 @@ extern c_token * c_parser_tokens_buf (c_parser *parser, unsigned n);
 extern bool c_parser_error (c_parser *parser);
 extern void c_parser_set_error (c_parser *parser, bool);
 
+/* A bit of a hack to have this here.  It would be better in a c-decl.h.  */
+extern bool old_style_parameter_scope (void);
+
 /* Return true if the next token from PARSER has the indicated
    TYPE.  */
 
index ecd2de3bc27421833a99ec2a3ab9bbf376e02f0e..63750af34950db9d161c960791149f2c62f1b90f 100644 (file)
@@ -1,3 +1,11 @@
+2018-05-02  David Pagan  <dave.pagan@oracle.com>
+
+       PR c/30552
+       * gcc.dg/noncompile/pr30552-1.c: New test.
+       * gcc.dg/noncompile/pr30552-2.c: New test.
+       * gcc.dg/noncompile/pr30552-3.c: New test.
+       * gcc.dg/noncompile/pr30552-4.c: New test.
+
 2018-05-02  Richard Biener  <rguenther@suse.de>
 
        PR tree-optimization/85597
diff --git a/gcc/testsuite/gcc.dg/noncompile/pr30552-1.c b/gcc/testsuite/gcc.dg/noncompile/pr30552-1.c
new file mode 100644 (file)
index 0000000..a19d9e0
--- /dev/null
@@ -0,0 +1,17 @@
+/* PR c/30552 */
+
+/* Statement expression as formal array argument size in nested old-style 
+   function declaration should generate user error, not internal compiler 
+   error.  */
+
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+int main()
+{
+  void fun(int a) /* { dg-error "old-style parameter declarations in prototyped function definition" } */
+    int a[({void h(){}2;})]; /* { dg-error "braced-group within expression allowed only inside a function" } */
+  {
+  }
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/noncompile/pr30552-2.c b/gcc/testsuite/gcc.dg/noncompile/pr30552-2.c
new file mode 100644 (file)
index 0000000..d884883
--- /dev/null
@@ -0,0 +1,17 @@
+/* PR c/30552 */
+
+/* Another example of a statement expression as formal array argument size in
+ * nested old-style function declaration should generate user error, not 
+ * internal compiler error.  */
+
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+int main()
+{
+  void fun(a)
+    int a[({int b=2; b;})]; /* { dg-error "braced-group within expression allowed only inside a function" } */
+  {
+  }
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/noncompile/pr30552-3.c b/gcc/testsuite/gcc.dg/noncompile/pr30552-3.c
new file mode 100644 (file)
index 0000000..7b48e76
--- /dev/null
@@ -0,0 +1,15 @@
+/* PR c/30552 */
+
+/* Related example where statement expression used as old-style formal array 
+ * argument size in an invalid nested function declaration should generate 
+ * user error, not internal compiler error. */
+
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+int main()
+{
+  int g()
+    int a[( {int b} )]; /* { dg-error "braced-group within expression allowed only inside a function|declaration for parameter" } */
+  return 0; /* { dg-error "expected declaration specifiers before" } */
+} /* { dg-error "expected declaration specifiers before|end of input|expected declaration or statement at end of input" } */
diff --git a/gcc/testsuite/gcc.dg/noncompile/pr30552-4.c b/gcc/testsuite/gcc.dg/noncompile/pr30552-4.c
new file mode 100644 (file)
index 0000000..d1f9c6b
--- /dev/null
@@ -0,0 +1,15 @@
+/* PR c/30552 */
+
+/* Statement expression as formal array argument size in nested function
+ * prototype scope is valid.  */
+
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+int main()
+{
+  void fun(int a[({void h(){}10;})])
+  {
+  }
+  return 0;
+}