This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Request for compiler option to disable multiple declarations in a single statement
- From: Eric Gallager <egall at gwmail dot gwu dot edu>
- To: Manish Jain <bourne dot identity at hotmail dot com>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Thu, 19 Apr 2018 13:25:32 -0400
- Subject: Re: Request for compiler option to disable multiple declarations in a single statement
- References: <VI1PR02MB120069566F352087BFB27930F6B50@VI1PR02MB1200.eurprd02.prod.outlook.com>
On 4/19/18, Manish Jain <bourne.identity@hotmail.com> wrote:
> Hi all,
>
> One of the historical artefacts of the C language has been the burden of
> lugging around multiple declarations in a single statement, with some
> well-known pitfalls:
>
> int* ptr1, ptr2;
>
> Since ptr2 looks like a pointer but actually is not, standard coding
> guidelines recommend declaring like this:
>
> int *p1, *p2;
>
> If anything, this leads to bizarre statements - very misleading for
> those trying to understand pointer usage in C or just read code:
>
> int i;
> int *j = &i; // impression: *j is being assigned &i
>
> char *k = "Text"; // impression: *k is "Text"
>
> void *fx(char *z); // impression: *fx is will accept char & return void
>
>
> Each of these idiosyncrasies is best avoided by retaining the space
> after the asterisk (and removing the one before) in a pointer
> declaration. This really ought to be the standard coding guideline.
>
> As for the problem of multiple declarations fraught in the suggestion
> above, I would like gcc developers to please consider a compiler option
> (--single-declarations perhaps) under which the programmer can only
> introduce one declaration in one statement. If such an option could be
> made available, it takes care of all declaration woes and lets declared
> types bear close resemblance to what they appear to be from signatures.
>
> Would my idea have takers on this list ?
>
> --
> Thank you & Regards,
> Manish Jain
>
Clang has a warning flag called -Wcomma to warn about code like this;
perhaps GCC could add it as well.