Thanks Prathamesh and Joseph for your suggestions.
Here is my updated patch :
for test cases:
const int array[5] = {1, 2, 3};
const int array1[3] = {1, 2, 3, 6};
const int array2[4] = {1, 2, 3, 6, 89};
const int array3[5] = {1, 2, 3, 6, 89, 193};
const int array4[3] = {1, 2, 3, 6, 89, 193};
const int array5[1] = {1, 2, 3, 6, 89, 193};
const int array6[3] = {1, 2, 3, 6, 89, 193;
const int array7[5] = {1, 2, 3, 6, 89, 193}
const int array8[5] = {1, 2, 3, 6, 89, 193
It gives:
68425.c: In function ‘main’:
68425.c:4:37: warning: excess elements in array initializer (4
elements, expected 3)
const int array1[3] = {1, 2, 3, 6};
^
68425.c:4:37: note: (near initialization for ‘array1’)
68425.c:5:40: warning: excess elements in array initializer (5
elements, expected 4)
const int array2[4] = {1, 2, 3, 6, 89};
^~
68425.c:5:40: note: (near initialization for ‘array2’)
68425.c:6:44: warning: excess elements in array initializer (6
elements, expected 5)
const int array3[5] = {1, 2, 3, 6, 89, 193};
^~~
68425.c:6:44: note: (near initialization for ‘array3’)
68425.c:7:37: warning: excess elements in array initializer (6
elements, expected 3)
const int array4[3] = {1, 2, 3, 6, 89, 193};
^
68425.c:7:37: note: (near initialization for ‘array4’)
68425.c:8:31: warning: excess elements in array initializer (6
elements, expected 1)
const int array5[1] = {1, 2, 3, 6, 89, 193};
^
68425.c:8:31: note: (near initialization for ‘array5’)
68425.c:9:37: warning: excess elements in array initializer (6
elements, expected 3)
const int array6[3] = {1, 2, 3, 6, 89, 193;
^
68425.c:9:37: note: (near initialization for ‘array6’)
68425.c:9:47: error: expected ‘}’ before ‘;’ token
const int array6[3] = {1, 2, 3, 6, 89, 193;
^
68425.c:14:1: error: expected declaration or statement at end of input
}
^
Any suggestions?
--
Thanks and Regards,
Prasad Ghangal
Index: gcc/c/c-parser.c
===================================================================
diff --git a/trunk/gcc/c/c-parser.c b/trunk/gcc/c/c-parser.c
--- a/trunk/gcc/c/c-parser.c (revision 233263)
+++ b/trunk/gcc/c/c-parser.c (working copy)
@@ -1264,6 +1264,7 @@
NUM_PRECS
};
+extern void pedwarn_init (location_t, int, const char *, ...);
static void c_parser_external_declaration (c_parser *);
static void c_parser_asm_definition (c_parser *);
static void c_parser_declaration_or_fndef (c_parser *, bool, bool, bool,
@@ -1294,9 +1295,11 @@
static struct c_type_name *c_parser_type_name (c_parser *);
static struct c_expr c_parser_initializer (c_parser *);
static struct c_expr c_parser_braced_init (c_parser *, tree, bool);
-static void c_parser_initelt (c_parser *, struct obstack *);
+static void c_parser_initelt (c_parser *, struct obstack *, bool *, location_t *,
+ HOST_WIDE_INT *, HOST_WIDE_INT *);
static void c_parser_initval (c_parser *, struct c_expr *,
- struct obstack *);
+ struct obstack *, bool *, location_t *, HOST_WIDE_INT *,
+ HOST_WIDE_INT *);
static tree c_parser_compound_statement (c_parser *);
static void c_parser_compound_statement_nostart (c_parser *);
static void c_parser_label (c_parser *);
@@ -4347,9 +4350,14 @@
{
/* Parse a non-empty initializer list, possibly with a trailing
comma. */
+ bool to_warn = false;
+ location_t warn_loc = 0;
+ HOST_WIDE_INT warn_extra_elements = 0;
+ HOST_WIDE_INT warn_expected_elements = 0;
while (true)
{
- c_parser_initelt (parser, &braced_init_obstack);
+ c_parser_initelt (parser, &braced_init_obstack, &to_warn,
+ &warn_loc, &warn_extra_elements, &warn_expected_elements);
if (parser->error)
break;
if (c_parser_next_token_is (parser, CPP_COMMA))
@@ -4359,6 +4367,13 @@
if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
break;
}
+ if(to_warn == true && !(parser->error))
+ {
+ pedwarn_init (warn_loc, 0, "excess elements in array initializer "
+ "(%wu elements, expected %wu)",
+ warn_expected_elements + warn_extra_elements,
+ warn_expected_elements);
+ }
}
c_token *next_tok = c_parser_peek_token (parser);
if (next_tok->type != CPP_CLOSE_BRACE)
@@ -4382,7 +4397,9 @@
/* Parse a nested initializer, including designators. */
static void
-c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack)
+c_parser_initelt (c_parser *parser, struct obstack * braced_init_obstack,
+ bool *to_warn, location_t *warn_loc, HOST_WIDE_INT *warn_extra_elements,
+ HOST_WIDE_INT *warn_expected_elements)
{
/* Parse any designator or designator list. A single array
designator may have the subsequent "=" omitted in GNU C, but a
@@ -4516,7 +4533,8 @@
/* Now parse and process the remainder of the
initializer, starting with this message
expression as a primary-expression. */
- c_parser_initval (parser, &mexpr, braced_init_obstack);
+ c_parser_initval (parser, &mexpr, braced_init_obstack, to_warn,
+ warn_loc, warn_extra_elements, warn_expected_elements);
return;
}
c_parser_consume_token (parser);
@@ -4576,7 +4594,8 @@
}
}
}
- c_parser_initval (parser, NULL, braced_init_obstack);
+ c_parser_initval (parser, NULL, braced_init_obstack, to_warn,
+ warn_loc, warn_extra_elements, warn_expected_elements);
}
/* Parse a nested initializer; as c_parser_initializer but parses
@@ -4587,7 +4606,9 @@
static void
c_parser_initval (c_parser *parser, struct c_expr *after,
- struct obstack * braced_init_obstack)
+ struct obstack * braced_init_obstack, bool *to_warn,
+ location_t *warn_loc, HOST_WIDE_INT *warn_extra_elements,
+ HOST_WIDE_INT *warn_expected_elements)
{
struct c_expr init;
gcc_assert (!after || c_dialect_objc ());
@@ -4603,7 +4624,9 @@
&& TREE_CODE (init.value) != COMPOUND_LITERAL_EXPR)
init = convert_lvalue_to_rvalue (loc, init, true, true);
}
- process_init_element (loc, init, false, braced_init_obstack);
+ process_init_element (loc, init, false, braced_init_obstack,
+ to_warn, warn_loc, warn_extra_elements,
+ warn_expected_elements);
}
/* Parse a compound statement (possibly a function body) (C90 6.6.2,
Index: gcc/c/c-tree.h
===================================================================
diff --git a/trunk/gcc/c/c-tree.h b/trunk/gcc/c/c-tree.h
--- a/trunk/gcc/c/c-tree.h (revision 233263)
+++ b/trunk/gcc/c/c-tree.h (working copy)
@@ -629,8 +629,11 @@
extern struct c_expr pop_init_level (location_t, int, struct obstack *);
extern void set_init_index (location_t, tree, tree, struct obstack *);
extern void set_init_label (location_t, tree, struct obstack *);
-extern void process_init_element (location_t, struct c_expr, bool,
- struct obstack *);
+extern void process_init_element (location_t, struct c_expr, bool,
+ struct obstack *, bool *to_warn = NULL,
+ location_t *warn_loc = NULL,
+ HOST_WIDE_INT *warn_extra_elements = 0,
+ HOST_WIDE_INT *warn_expected_elements = 0);
extern tree build_compound_literal (location_t, tree, tree, bool);
extern void check_compound_literal_type (location_t, struct c_type_name *);
extern tree c_start_case (location_t, location_t, tree, bool);
Index: gcc/c/c-typeck.c
===================================================================
diff --git a/trunk/gcc/c/c-typeck.c b/trunk/gcc/c/c-typeck.c
--- a/trunk/gcc/c/c-typeck.c (revision 233263)
+++ b/trunk/gcc/c/c-typeck.c (working copy)
@@ -5855,19 +5855,26 @@
it is unconditionally given. GMSGID identifies the message. The
component name is taken from the spelling stack. */
-static void
-pedwarn_init (location_t location, int opt, const char *gmsgid)
+void
+pedwarn_init (location_t location, int opt, const char *gmsgid, ...)
{
char *ofwhat;
bool warned;
+ diagnostic_info diagnostic;
+ va_list ap;
+ rich_location richloc (line_table, location);
+ va_start (ap, gmsgid);
+ diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_PEDWARN);
+ diagnostic.option_index = opt;
+
/* The gmsgid may be a format string with %< and %>. */
- warned = pedwarn (location, opt, gmsgid);
+ warned = report_diagnostic (&diagnostic);
ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
if (*ofwhat && warned)
inform (location, "(near initialization for %qs)", ofwhat);
+ va_end (ap);
}
-
/* Issue a warning for a bad initializer component.
OPT is the OPT_W* value corresponding to the warning option that
@@ -8967,7 +8974,9 @@
void
process_init_element (location_t loc, struct c_expr value, bool implicit,
- struct obstack * braced_init_obstack)
+ struct obstack * braced_init_obstack, bool *to_warn,
+ location_t *warn_loc, HOST_WIDE_INT *warn_extra_elements,
+ HOST_WIDE_INT *warn_expected_elements)
{
tree orig_value = value.value;
int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
@@ -9269,8 +9278,13 @@
&& (tree_int_cst_lt (constructor_max_index, constructor_index)
|| integer_all_onesp (constructor_max_index)))
{
- pedwarn_init (loc, 0,
- "excess elements in array initializer");
+ if (to_warn && *to_warn == false)
+ {
+ *to_warn = true;
+ *warn_expected_elements = tree_to_uhwi (constructor_max_index) + 1;
+ *warn_loc = loc;
+ }
+ ++(*warn_extra_elements);
break;
}