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.

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

Attachment: PR68425.patch
Description: Binary data

Reply via email to