Hi,

On 17/05/2018 16:58, Jason Merrill wrote:
On Thu, May 17, 2018 at 10:27 AM, Paolo Carlini
<paolo.carl...@oracle.com> wrote:
PS: maybe better using function_declarator_p?
I think so, yes.  The relevant rule seems to be "The declarator shall
not specify a function or an array.", so let's check for arrays, too.
Agreed. I had the amended patch ready when I noticed (again) that it wasn't addressing another related class of issues which involves declarators not followed by initializers. Thus I tried to fix those too, and the below which moves the check up appears to work fine, passes testing, etc. Are there any risks that an erroneous function / array as declarator is in fact a well formed expression?!? I haven't been able so far to construct examples...


Thanks!
Paolo.

////////////////////////
Index: cp/parser.c
===================================================================
--- cp/parser.c (revision 260331)
+++ cp/parser.c (working copy)
@@ -11527,6 +11527,33 @@ cp_parser_selection_statement (cp_parser* parser,
     }
 }
 
+/* Helper function for cp_parser_condition.  Enforces [stmt.stmt]:
+   The declarator shall not specify a function or an array.  Returns
+   TRUE is the declator is valid, FALSE otherwise.  */
+
+static bool
+cp_parser_check_condition_declarator (cp_parser* parser,
+                                     cp_declarator *declarator,
+                                     location_t loc)
+{
+  if (function_declarator_p (declarator)
+      || declarator->kind == cdk_array)
+    {
+      if (declarator->kind == cdk_array)
+       error_at (loc, "an array type is not allowed here");
+      else
+       error_at (loc, "a function type is not allowed here");
+      if (parser->fully_implicit_function_template_p)
+       abort_fully_implicit_template (parser);
+      cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
+                                            /*or_comma=*/false,
+                                            /*consume_paren=*/false);
+      return false;
+    }
+  else
+    return true;
+}
+
 /* Parse a condition.
 
    condition:
@@ -11571,6 +11598,7 @@ cp_parser_condition (cp_parser* parser)
       tree attributes;
       cp_declarator *declarator;
       tree initializer = NULL_TREE;
+      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
 
       /* Parse the declarator.  */
       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
@@ -11592,10 +11620,15 @@ cp_parser_condition (cp_parser* parser)
       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
          && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
        cp_parser_simulate_error (parser);
-       
+
       /* If we did see an `=' or '{', then we are looking at a declaration
         for sure.  */
-      if (cp_parser_parse_definitely (parser))
+      bool decl_p = cp_parser_parse_definitely (parser);
+
+      if (!cp_parser_check_condition_declarator (parser, declarator, loc))
+       return error_mark_node;
+
+      if (decl_p)
        {
          tree pushed_scope;
          bool non_constant_p;
Index: testsuite/g++.dg/cpp0x/cond1.C
===================================================================
--- testsuite/g++.dg/cpp0x/cond1.C      (nonexistent)
+++ testsuite/g++.dg/cpp0x/cond1.C      (working copy)
@@ -0,0 +1,23 @@
+// PR c++/84588
+// { dg-do compile { target c++11 } }
+
+void foo()
+{
+  if (int bar() {})  // { dg-error "function type is not allowed" }
+    ;
+
+  for (;int bar() {};)  // { dg-error "function type is not allowed" }
+    ;
+
+  while (int bar() {})  // { dg-error "function type is not allowed" }
+    ;
+
+  if (int a[] {})  // { dg-error "array type is not allowed" }
+    ;
+
+  for (;int a[] {};)  // { dg-error "array type is not allowed" }
+    ;
+
+  while (int a[] {})  // { dg-error "array type is not allowed" }
+    ;
+}
Index: testsuite/g++.dg/cpp1y/pr84588-1.C
===================================================================
--- testsuite/g++.dg/cpp1y/pr84588-1.C  (nonexistent)
+++ testsuite/g++.dg/cpp1y/pr84588-1.C  (working copy)
@@ -0,0 +1,25 @@
+// { dg-do compile { target c++14 } }
+
+struct a {
+  void b() {}
+  void c(void (*) () = [] {
+      if (a a(int auto) {})  // { dg-error "two or more data types|function 
type" }
+      ;
+  }) {}
+};
+
+struct d {
+  void e() {}
+  void f(void (*) () = [] {
+      for (;d d(int auto) {};)  // { dg-error "two or more data types|function 
type" }
+      ;
+  }) {}
+};
+
+struct g {
+  void h() {}
+  void i(void (*) () = [] {
+      while (g g(int auto) {})  // { dg-error "two or more data types|function 
type" }
+      ;
+  }) {}
+};
Index: testsuite/g++.dg/cpp1y/pr84588-2.C
===================================================================
--- testsuite/g++.dg/cpp1y/pr84588-2.C  (nonexistent)
+++ testsuite/g++.dg/cpp1y/pr84588-2.C  (working copy)
@@ -0,0 +1,25 @@
+// { dg-do compile { target c++14 } }
+
+struct a {
+  void b() {}
+  void c(void (*) () = [] {
+      if (a a(int auto))  // { dg-error "two or more data types|function type" 
}
+      ;
+  }) {}
+};
+
+struct d {
+  void e() {}
+  void f(void (*) () = [] {
+      for (;d d(int auto);)  // { dg-error "two or more data types|function 
type" }
+      ;
+  }) {}
+};
+
+struct g {
+  void h() {}
+  void i(void (*) () = [] {
+      while (g g(int auto))  // { dg-error "two or more data types|function 
type" }
+      ;
+  }) {}
+};
Index: testsuite/g++.dg/parse/cond6.C
===================================================================
--- testsuite/g++.dg/parse/cond6.C      (nonexistent)
+++ testsuite/g++.dg/parse/cond6.C      (working copy)
@@ -0,0 +1,22 @@
+// PR c++/84588
+
+void foo()
+{
+  if (int bar())  // { dg-error "function type is not allowed" }
+    ;
+
+  for (;int bar();)  // { dg-error "function type is not allowed" }
+    ;
+
+  while (int bar())  // { dg-error "function type is not allowed" }
+    ;
+
+  if (int a[])  // { dg-error "array type is not allowed" }
+    ;
+
+  for (;int a[];)  // { dg-error "array type is not allowed" }
+    ;
+
+  while (int a[])  // { dg-error "array type is not allowed" }
+    ;
+}
Index: testsuite/g++.old-deja/g++.jason/cond.C
===================================================================
--- testsuite/g++.old-deja/g++.jason/cond.C     (revision 260331)
+++ testsuite/g++.old-deja/g++.jason/cond.C     (working copy)
@@ -47,11 +47,10 @@ int main()
   if (struct B * foo = new B)
     ;
 
-  if (int f () = 1)            // { dg-warning "extern" "extern" } 
-  // { dg-error "is initialized like a variable" "var" { target *-*-* } .-1 }
+  if (int f () = 1)            // { dg-error "function type" } 
     ;
   
-  if (int a[2] = {1, 2})       // { dg-error "extended init" "" { target { ! 
c++11 } } }
+  if (int a[2] = {1, 2})       // { dg-error "array type" }
     ;
 
 }

Reply via email to