This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Need suggestion about bug 68425
- From: Prasad Ghangal <prasad dot ghangal at gmail dot com>
- To: David Malcolm <dmalcolm at redhat dot com>
- Cc: Joseph Myers <joseph at codesourcery dot com>, Prathamesh Kulkarni <prathamesh dot kulkarni at linaro dot org>, gcc Mailing List <gcc at gcc dot gnu dot org>, Manuel LÃpez-IbÃÃez <lopezibanez at gmail dot com>
- Date: Sun, 3 Apr 2016 21:26:06 +0530
- Subject: Re: Need suggestion about bug 68425
- Authentication-results: sourceware.org; auth=none
- References: <CAE+uiWYLZ6ODDc3nR3zgAdxekTtSAD10Ba3pqJ4Y--FJYo5Fig at mail dot gmail dot com> <56C5D529 dot 9080700 at gmail dot com> <1455909237 dot 9333 dot 34 dot camel at redhat dot com> <CAESRpQA4_+f==EyAdre64OOcLYD2KBFFhhpyLb0Z_nrG9kfuXw at mail dot gmail dot com> <CAE+uiWaua28sLnrrVNmZo4Y0S-qJnbh-qWRQHA8prNCDuijQ4A at mail dot gmail dot com> <alpine dot DEB dot 2 dot 10 dot 1602222137340 dot 26576 at digraph dot polyomino dot org dot uk> <CAE+uiWYBPq0aWp=aA=JCDUkLATGx4MwhbEQu1+Bf2NrAyuWrrw at mail dot gmail dot com> <1457120185 dot 1637 dot 49 dot camel at redhat dot com>
On 5 March 2016 at 01:06, David Malcolm <dmalcolm@redhat.com> wrote:
> On Wed, 2016-02-24 at 17:56 +0530, Prasad Ghangal wrote:
>> 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
>> }
>> ^
>
> Those caret locations look wrong to me - they don't seem to be
> underlining the pertinent source. Is that what the patched compiler is
> printing, or did things get messed up somewhere via email?
>
> (I tried the patch, but it didn't quite apply cleanly against my
> working copy)
>
> As mentioned before, I think the ideal behavior would be to underline
> *all* of the surplus elements, like this:
>
> 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
> }
> ^
>
> You can use "make_location" to build a range; see e.g. this fragment
> from the C++ frontend:
>
> /* Create a location of the form:
> type_name{i, f}
> ^~~~~~~~~~~~~~~
> with caret == start at the start of the type name,
> finishing at the closing brace. */
> location_t finish_loc
> = get_finish (cp_lexer_previous_token (parser->lexer)->location);
> location_t combined_loc = make_location (start_loc, start_loc,
> finish_loc);
>
> You could do something similar, building a range covering the start of
> the first surplus initializer through to the end of the last surplus
> initializer.
>
> You can then emit an error_at combined_loc, via error_at.
>
> For an example of how to write a test case for underlines, see the
> testsuite part of:
> https://gcc.gnu.org/ml/gcc-patches/2015-12/msg01684.html
>
>
> For bonus points, it's also possible to add a 2nd location for the
> message, by using error_at_richloc, so that you could underline the
> array size:
>
> 68425.c:9:37: warning: excess elements in array initializer (6
> elements,
> expected 3)
> const int array6[3] = {1, 2, 3, 6, 89, 193};
>
> ~ ^~~~~~~~~~
>
How can I get location of array size?
> Or do that in the followup note, for something like:
>
> 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:12: note: (near initialization for âarray6â)
> const int array6[3] = {1, 2, 3, 6, 89, 193;
> ^
>
> but maybe that's over-ambitious for now.
>
> Hope this is constructive.
> Dave
Also for
int array[10];
array[100]=10;
Currently, GCC doesn't emit any warning (even with -Wall option)
Wouldn't it be nice if GCC gives some warning like Clang, which gives:
foo.c:4:3: warning: array index 100 is past the end of the array
(which contains 10 elements) [-Warray-bounds]
array[100]=10;
^ ~~~
Thanks,
Prasad Ghangal