On 09/18/2017 01:06 PM, Prasad Ghangal wrote:
Hi,

This was discussed a few time ago in this email thread :
https://gcc.gnu.org/ml/gcc/2016-02/msg00235.html

As per the discussion, the attached patch produces warning message for
excess elements in array initialization along with number of expected
and extra elements and underlined extra elements ranges.

I like the additional detail and the underlining of all the excess
elements when the whole initializer list is on the same line.  I'm
not sure it's a good idea to underline all the excess elements when
they are on separate lines.  That could make the warning really long.

For some additional improvements I would suggest either eliminating
the note (it serves no useful purpose anymore) or replacing it with
the element count(s).  The message printing the element counter
should also be adjusted to be correct for zero-length arrays as in:

  int a[0] = { 1 };

Alternatively, change the message to say just:

  warning: X excess elements in an initializer for 'int a[N]'

and when the line the caret is on is not the same as the object
being initialized, print a note pointing to the declaration as
is done by some other warnings:

  note: array 'a' declared here

As an additional improvement, string initializers with excessive
elements would benefit from the same underlining/element counts.

The C++ front end will need similar changes.

Finally, it would be nice to also be able to control the warning
(i.e., provide an option to turn it on and off).

Martin

PS Please send patches to gcc-patches.


e.g for following testcases:

int foo()
{
  int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int b[0] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int c[5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int d[5][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int e[5][0] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int f[5][2][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10};
  int h[3][2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7};
  return 0;
}


It produces (gmail may mess up with spaces):

./gcc/cc1 ../tests/test.c
 foo
../tests/test.c: In function ‘foo’:
../tests/test.c:4:15: warning: excess elements in array initializer
(10 elements, 0 expected)
   int b[0] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
                   ^~~~~~~~~~~~~~~~~~~
../tests/test.c:4:15: note: (near initialization for ‘b’)
../tests/test.c:5:30: warning: excess elements in array initializer
(10 elements, 5 expected)
   int c[5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
                                   ^~~~~~~~~~~
../tests/test.c:5:30: note: (near initialization for ‘c’)
../tests/test.c:6:49: warning: excess elements in array initializer
(20 elements, 10 expected)
   int d[5][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

^~~~~~~~~~~~~~~~~~~
../tests/test.c:6:49: note: (near initialization for ‘d[5]’)
../tests/test.c:7:18: warning: excess elements in array initializer
(20 elements, 0 expected)
   int e[5][0] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../tests/test.c:7:18: note: (near initialization for ‘e[5]’)
../tests/test.c:8:114: warning: excess elements in array initializer
(40 elements, 30 expected)
   int f[5][2][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
                                                            ^~~~~~~~~~~~~~~~~~~
../tests/test.c:8:114: note: (near initialization for ‘f[5][1]’)
../tests/test.c:9:135: warning: excess elements in array initializer
(37 elements, 36 expected)
   int h[3][2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7};

                 ^
../tests/test.c:9:135: note: (near initialization for ‘h[3][0][0]’)


Any thoughts?


Thanks,
Prasad


Reply via email to