diff mbox

[C++] report better diagnostic for static following '[' in parameter declaration

Message ID CAAgBjMndUJru9TjbUm_DQzGvdJkH1Zhq-pq2-RnWJn=Kz+gVVw@mail.gmail.com
State New
Headers show

Commit Message

Prathamesh Kulkarni Jan. 28, 2016, 11:16 p.m. UTC
Hi,
For the test-case,
void f(int a[static 10]);

g++ gives following errors:
test-foo.cpp:1:14: error: expected primary-expression before ‘static’
 void f(int a[static 10]);
              ^
test-foo.cpp:1:14: error: expected ‘]’ before ‘static’
test-foo.cpp:1:14: error: expected ‘)’ before ‘static’
test-foo.cpp:1:14: error: expected initializer before ‘static’

and clang++ gives:
test-foo.cpp:1:13: error: static array size is a C99 feature, not
permitted in C++
void f(int a[static 10]);
            ^
I have attached patch that attempts to report the same diagnostic.
With patch, g++ reports:

test-foo.cpp:1:14: error: static array size is a C99 feature,not
permitted in C++
 void f(int a[static 10])
              ^~~~~~
test-foo.cpp:1:14: error: expected ‘]’ before ‘static’
test-foo.cpp:1:14: error: expected ‘)’ before ‘static’
test-foo.cpp:1:14: error: expected initializer before ‘static’

I tried to remove the 3 errors that follow it (expected X before static)
but without luck :/

Bootstrap and tested on x86_64-unknown-linux-gnu.
OK for trunk ?

Thanks,
Prathamesh
diff mbox

Patch

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index d03b0c9..4d3e38a 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -19016,10 +19017,22 @@  cp_parser_direct_declarator (cp_parser* parser,
 	  cp_lexer_consume_token (parser->lexer);
 	  /* Peek at the next token.  */
 	  token = cp_lexer_peek_token (parser->lexer);
+
+	  /* If static keyword immediately follows [, report error.  */
+	  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC)
+	      && current_binding_level->kind == sk_function_parms)
+	    {
+	      error_at (token->location,
+			"static array size is a C99 feature,"
+			"not permitted in C++");
+	      bounds = error_mark_node;
+	    }
+
 	  /* If the next token is `]', then there is no
 	     constant-expression.  */
-	  if (token->type != CPP_CLOSE_SQUARE)
+	  else if (token->type != CPP_CLOSE_SQUARE)
 	    {
+
 	      bool non_constant_p;
 	      bounds
 		= cp_parser_constant_expression (parser,
diff --git a/gcc/testsuite/g++.dg/parse/static-array-error.C b/gcc/testsuite/g++.dg/parse/static-array-error.C
new file mode 100644
index 0000000..8b58588
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/static-array-error.C
@@ -0,0 +1,6 @@ 
+// { dg-do compile }
+
+void f(int a[static 10]);  /* { dg-error "static array size is a C99 feature" } */
+/* { dg-error "expected ']' before 'static'" "" { target *-*-* } 3 } */
+/* { dg-error "expected ')' before 'static'" "" { target *-*-* } 3 } */
+/* { dg-error "expected initializer before 'static'" "" { target *-*-* } 3 } */