Hi,
On 18/05/2018 01:21, Jason Merrill wrote:
On Thu, May 17, 2018 at 5:54 PM, Paolo Carlini
<paolo.carl...@oracle.com> wrote:
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?!?
That doesn't matter; if it parses as a declarator, it's a declarator,
even if it's an ill-formed declarator. But...
+ bool decl_p = cp_parser_parse_definitely (parser);
+ if (!cp_parser_check_condition_declarator (parser,
declarator, loc))
+ return error_mark_node;
...if cp_parser_parse_definitely returns false, parsing as a
declarator failed, so we shouldn't look at "declarator".
What I'm attaching below isn't affected by this problem: I moved the
check further up - *before* maybe calling cp_parser_simulated_error
because an initializer isn't in sight - and is carried out only when
!cp_parser_error_occurred, thus cp_parser_declarator succeeded .
cp_parser_commit_to_tentative_parse is called when the check fails.
Bootstrapped and tested on x86_64-linux.
Thanks!
Paolo.
//////////////////////
Index: cp/parser.c
===================================================================
--- cp/parser.c (revision 260347)
+++ cp/parser.c (working copy)
@@ -11527,6 +11527,34 @@ cp_parser_selection_statement (cp_parser* parser,
}
}
+/* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
+ The declarator shall not specify a function or an array. Returns
+ TRUE if the declarator 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)
+ {
+ cp_parser_commit_to_tentative_parse (parser);
+ if (declarator->kind == cdk_array)
+ error_at (loc, "condition declares an array");
+ else
+ error_at (loc, "condition declares a function");
+ 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 +11599,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,
@@ -11582,6 +11611,11 @@ cp_parser_condition (cp_parser* parser)
attributes = cp_parser_attributes_opt (parser);
/* Parse the asm-specification. */
asm_specification = cp_parser_asm_specification_opt (parser);
+
+ if (!cp_parser_error_occurred (parser)
+ && !cp_parser_check_condition_declarator (parser, declarator, loc))
+ return error_mark_node;
+
/* If the next token is not an `=' or '{', then we might still be
looking at an expression. For example:
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 "condition declares a function" }
+ ;
+
+ for (;int bar() {};) // { dg-error "condition declares a function" }
+ ;
+
+ while (int bar() {}) // { dg-error "condition declares a function" }
+ ;
+
+ if (int a[] {}) // { dg-error "condition declares an array" }
+ ;
+
+ for (;int a[] {};) // { dg-error "condition declares an array" }
+ ;
+
+ while (int a[] {}) // { dg-error "condition declares an array" }
+ ;
+}
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|condition
declares a function" }
+ ;
+ }) {}
+};
+
+struct d {
+ void e() {}
+ void f(void (*) () = [] {
+ for (;d d(int auto) {};) // { dg-error "two or more data
types|condition declares a function" }
+ ;
+ }) {}
+};
+
+struct g {
+ void h() {}
+ void i(void (*) () = [] {
+ while (g g(int auto) {}) // { dg-error "two or more data
types|condition declares a function" }
+ ;
+ }) {}
+};
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|condition
declares a function" }
+ ;
+ }) {}
+};
+
+struct d {
+ void e() {}
+ void f(void (*) () = [] {
+ for (;d d(int auto);) // { dg-error "two or more data types|condition
declares a function" }
+ ;
+ }) {}
+};
+
+struct g {
+ void h() {}
+ void i(void (*) () = [] {
+ while (g g(int auto)) // { dg-error "two or more data types|condition
declares a function" }
+ ;
+ }) {}
+};
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 "condition declares a function" }
+ ;
+
+ for (;int bar();) // { dg-error "condition declares a function" }
+ ;
+
+ while (int bar()) // { dg-error "condition declares a function" }
+ ;
+
+ if (int a[]) // { dg-error "condition declares an array" }
+ ;
+
+ for (;int a[];) // { dg-error "condition declares an array" }
+ ;
+
+ while (int a[]) // { dg-error "condition declares an array" }
+ ;
+}
Index: testsuite/g++.old-deja/g++.jason/cond.C
===================================================================
--- testsuite/g++.old-deja/g++.jason/cond.C (revision 260347)
+++ 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 "declares a function" }
;
- if (int a[2] = {1, 2}) // { dg-error "extended init" "" { target { !
c++11 } } }
+ if (int a[2] = {1, 2}) // { dg-error "declares an array" }
;
}