PR68425 warn message enhancement
Martin Sebor
msebor@gmail.com
Tue Sep 19 14:10:00 GMT 2017
On 09/19/2017 12:50 AM, Prasad Ghangal wrote:
> On 19 September 2017 at 01:35, Martin Sebor <msebor@gmail.com> wrote:
>> 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).
>>
> Currently gcc raises warning for excess elements in an initializer by default.
> Should we introduce new flag/option (like
> Wextra-elements-initializers) or use any existing one?
I can't find or think of a suitable existing option to use so I'd
say add a new one. -Wmissing-field-initializers controls the opposite
case for structs, suggesting that either -Wexcess-array-initializers
or -Wexcess-initializers might be an intuitive choice (depending on
whether we want two distinct options, one for arrays and another for
structs, or just one for both). This, incidentally, also implies
that the struct case should also be diagnosed, perhaps under its
own bug/patch.
As an aside, providing an option for every warning is being tracked
in bug 44209.
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
>>>
>>
More information about the Gcc
mailing list