public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH v2] c: Error message for incorrect use of static in array declarations
@ 2024-06-26 18:01 Martin Uecker
  2024-06-26 21:17 ` Marek Polacek
  0 siblings, 1 reply; 2+ messages in thread
From: Martin Uecker @ 2024-06-26 18:01 UTC (permalink / raw)
  To: gcc-patches; +Cc: Joseph Myers, Marek Polacek


Thanks Marek, here is the second version which should
implement all your suggestions.  

(BTW: Without the newline of the end, the test case has
undefined behavior..., not that we need to care.)


Bootstrapped and regression tested on x86_64.


    [PATCH] c: Error message for incorrect use of static in array declarations.
    
    Add an explicit error messages when c99's static is
    used without a size expression in an array declarator.
    
    gcc/c:
            c-parser.cc (c_parser_direct_declarator_inner): Add
            error message.
    
    gcc/testsuite:
            gcc.dg/c99-arraydecl-4.c: New test.

diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 6a3f96d5b61..db60507252b 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -4715,8 +4715,6 @@ c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
       location_t brace_loc = c_parser_peek_token (parser)->location;
       struct c_declarator *declarator;
       struct c_declspecs *quals_attrs = build_null_declspecs ();
-      bool static_seen;
-      bool star_seen;
       struct c_expr dimen;
       dimen.value = NULL_TREE;
       dimen.original_code = ERROR_MARK;
@@ -4724,7 +4722,8 @@ c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
       c_parser_consume_token (parser);
       c_parser_declspecs (parser, quals_attrs, false, false, true,
 			  false, false, false, false, cla_prefer_id);
-      static_seen = c_parser_next_token_is_keyword (parser, RID_STATIC);
+      const bool static_seen = c_parser_next_token_is_keyword (parser,
+							       RID_STATIC);
       if (static_seen)
 	c_parser_consume_token (parser);
       if (static_seen && !quals_attrs->declspecs_seen_p)
@@ -4735,38 +4734,34 @@ c_parser_direct_declarator_inner (c_parser *parser, bool id_present,
       /* If "static" is present, there must be an array dimension.
 	 Otherwise, there may be a dimension, "*", or no
 	 dimension.  */
-      if (static_seen)
+      bool star_seen = false;
+      if (c_parser_next_token_is (parser, CPP_MULT)
+	  && c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
 	{
-	  star_seen = false;
-	  dimen = c_parser_expr_no_commas (parser, NULL);
+	  star_seen = true;
+	  c_parser_consume_token (parser);
 	}
-      else
+      else if (!c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
+	dimen = c_parser_expr_no_commas (parser, NULL);
+
+      if (static_seen)
 	{
-	  if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
+	  if (star_seen)
 	    {
-	      dimen.value = NULL_TREE;
+	      error_at (c_parser_peek_token (parser)->location,
+			"%<static%> may not be used with an unspecified "
+			"variable length array size");
+	      /* Prevent further errors.  */
 	      star_seen = false;
+	      dimen.value = error_mark_node;
 	    }
-	  else if (c_parser_next_token_is (parser, CPP_MULT))
-	    {
-	      if (c_parser_peek_2nd_token (parser)->type == CPP_CLOSE_SQUARE)
-		{
-		  dimen.value = NULL_TREE;
-		  star_seen = true;
-		  c_parser_consume_token (parser);
-		}
-	      else
-		{
-		  star_seen = false;
-		  dimen = c_parser_expr_no_commas (parser, NULL);
-		}
-	    }
-	  else
+	  else if (!dimen.value)
 	    {
-	      star_seen = false;
-	      dimen = c_parser_expr_no_commas (parser, NULL);
+	      error_at (c_parser_peek_token (parser)->location,
+			"%<static%> may not be used without an array size");
 	    }
 	}
+
       if (c_parser_next_token_is (parser, CPP_CLOSE_SQUARE))
 	c_parser_consume_token (parser);
       else
diff --git a/gcc/testsuite/gcc.dg/c99-arraydecl-4.c b/gcc/testsuite/gcc.dg/c99-arraydecl-4.c
new file mode 100644
index 00000000000..f8cad3b9429
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c99-arraydecl-4.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-std=c99 -pedantic-errors" } */
+
+void fo(char buf[static]);	/* { dg-error "'static' may not be used without an array size" } */
+void fo(char buf[static]) { }	/* { dg-error "'static' may not be used without an array size" } */
+
+void fu(char buf[static *]);	/* { dg-error "'static' may not be used with an unspecified variable length array size" } */
+void fu(char buf[static *]) { }	/* { dg-error "'static' may not be used with an unspecified variable length array size" } */
+
+void fe(int n, char buf[static n]);
+void fe(int n, char buf[static *]) { }	/* { dg-error "'static' may not be used with an unspecified variable length array size" } */
+
+void fa(int *n, char buf[static *n]);
+void fa(int *n, char buf[static *n]) { }


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-06-26 21:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-26 18:01 [PATCH v2] c: Error message for incorrect use of static in array declarations Martin Uecker
2024-06-26 21:17 ` Marek Polacek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).