Hi Jonathan,

On 11/14/22 14:14, Jonathan Wakely wrote:
On Mon, 14 Nov 2022 at 11:38, Alejandro Colomar via Gcc <gcc@gcc.gnu.org> wrote:

Hi Andrew!

On 11/13/22 23:12, Andrew Pinski wrote:
On Sun, Nov 13, 2022 at 1:57 PM Alejandro Colomar via Gcc
<gcc@gcc.gnu.org> wrote:

Hi!

I'd like to get warnings if I write the following code:

char foo[3] = "foo";

This should be easy to add as it is already part of the -Wc++-compat
option as for C++ it is invalid code.

<source>:2:19: warning: initializer-string for array of 'char' is too long
      2 | char     two[2] = "foo";   // 'f' 'o'
        |                   ^~~~~
<source>:3:19: warning: initializer-string for array of 'char' is too
long for C++ [-Wc++-compat]
      3 | char   three[3] = "foo";   // 'f' 'o' 'o'
        |                   ^~~~~


... (for your more complex case [though I needed to modify one of the
strings to exactly 8]

<source>:5:7: warning: initializer-string for array of 'char' is too
long for C++ [-Wc++-compat]
      5 |       "01234567",
        |       ^~~~~~~~~~

                else if (warn_cxx_compat
                         && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
                  warning_at (init_loc, OPT_Wc___compat,
                              ("initializer-string for array of %qT "
                               "is too long for C++"), typ1);

That is the current code which does this warning even so it is just a
matter of adding an option to c-family/c.opt and then having
c++-compat enable it and using that new option here.

Thanks,
Andrew Pinski

Great!  I'd like to implement it myself, as I've never written any GCC code yet,
so it's interesting to me.  If you recall any (hopefully recent) case where a
similar thing happened (the warning was already implemented and only needed a
name), it might help me check how it was done.

`git log gcc/c-family/c.opt` will show loads of changes adding warnings.

Thanks, I will check and try it, and come back if I have any doubts.

Cheers,

Alex

--
<http://www.alejandro-colomar.es/>

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to