This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: Warning about -Wmain for local variables
- From: Jeff Law <law at redhat dot com>
- To: Prathamesh Kulkarni <prathamesh dot kulkarni at linaro dot org>, Jonathan Wakely <jwakely dot gcc at gmail dot com>
- Cc: gcc Patches <gcc-patches at gcc dot gnu dot org>, Marek Polacek <polacek at redhat dot com>
- Date: Wed, 27 Jun 2018 21:16:30 -0600
- Subject: Re: Warning about -Wmain for local variables
- References: <CAAgBjM=_+e15aot6rZVEF3zZuNi5c9d2UFEQu5aSdF6nD-shgw@mail.gmail.com> <CAH6eHdS-LNdgsJo4PBaAJS0_U+q4LhAfsQnF=P5-87qkifkk9Q@mail.gmail.com> <CAAgBjM=zN5=VaW0Lx_gN8KsP2MbH+qdkMxWO1q4a7AC1JKEGZw@mail.gmail.com>
On 05/30/2018 09:51 AM, Prathamesh Kulkarni wrote:
> On 30 May 2018 at 18:12, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
>> On 30 May 2018 at 11:40, Prathamesh Kulkarni wrote:
>>> gcc with -Wmain warns for local variables named main.
>>>
>>> int foo()
>>> {
>>> int main = 0;
>>> return main;
>>> }
>>>
>>> a.c: In function ‘foo’:
>>> a.c:3:7: warning: ‘main’ is usually a function [-Wmain]
>>> int main = 1;
>>> ^~~~
>>>
>>> Is this intended ? I assumed that the warning's intent was for
>>> diagnosing variable named main having only external linkage.
>> It was added more than 20 years ago by https://gcc.gnu.org/r13517 and
>> looks like it has always worked as it does now, without considering
>> linkage.
>>
>> Only warning for entities with external linkage seems reasonable to
>> me, but that would be for the C front-end or diagnostics maintainers
>> to decide.
> Thanks for the suggestions. I have attached (untested) patch that warns
> for Wmain if TREE_PUBLIC (decl) is true.
> Does it look OK ?
>
> Thanks,
> Prathamesh
>
>
> wmain-1.txt
>
>
> 2018-05-30 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
>
> * c/c-decl.c (start_decl): Add check TREE_PUBLIC (decl) for -Wmain.
>
> testsuite/
> * gcc.dg/wmain.c: New.
After the usual testing, this is OK.
jeff