public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Request for compiler option to disable multiple declarations in a single statement
@ 2018-04-19  8:35 Manish Jain
  2018-04-19  9:22 ` David Brown
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Manish Jain @ 2018-04-19  8:35 UTC (permalink / raw)
  To: gcc

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Request for compiler option to disable multiple declarations in a single statement
  2018-04-19  8:35 Request for compiler option to disable multiple declarations in a single statement Manish Jain
@ 2018-04-19  9:22 ` David Brown
  2018-04-19  9:39   ` Manish Jain
  2018-04-19  9:28 ` Jonathan Wakely
  2018-04-19 22:08 ` Eric Gallager
  2 siblings, 1 reply; 8+ messages in thread
From: David Brown @ 2018-04-19  9:22 UTC (permalink / raw)
  To: Manish Jain, gcc

On 19/04/18 10:09, Manish Jain 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 ?
> 

This is something that will cause a lot of mixed feelings.  Some people
see it as a very good thing that you can have multiple declarations of
connected types in the same declaration - others see it as a very bad
idea.  Certainly it is heavily used in existing code - making an option
to disable it would be impractical.

What I would suggest as a better compromise would be a warning, with
different levels:

-Wmulti-declaration=0 (the default):

	int a, b, c;		// OK
	int a, *b, c;		// OK
	int *a, *b, *c;		// OK
	int a[10], b[10];	// OK

-Wmulti-declaration=1 (Allow declarations of the same type only):

	int a, b, c;		// OK
	int a, *b, c;		// Warn
	int *a, *b, *c;		// OK
	int a[10], b[10];	// OK

-Wmulti-declaration=2 (Disallow multiple declarations)
	int a, b, c;		// Warn
	int a, *b, c;		// Warn
	int *a, *b, *c;		// Warn
	int a[10], b[10];	// Warn


A warning can always be turned into an error if you want.

Personally, I would use such a warning option at level 1, but not at
level 2.  I know others would use more or less.

(I have no idea if this is something that would be practical or easy to
implement in gcc, or if there is anyone who would be willing to do the
job.  I am just trying to refine the suggestion into something that
could have a wider appeal.)


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Request for compiler option to disable multiple declarations in a single statement
  2018-04-19  8:35 Request for compiler option to disable multiple declarations in a single statement Manish Jain
  2018-04-19  9:22 ` David Brown
@ 2018-04-19  9:28 ` Jonathan Wakely
  2018-04-19 15:45   ` Manish Jain
  2018-04-19 22:08 ` Eric Gallager
  2 siblings, 1 reply; 8+ messages in thread
From: Jonathan Wakely @ 2018-04-19  9:28 UTC (permalink / raw)
  To: Manish Jain; +Cc: gcc

On 19 April 2018 at 09:09, Manish Jain 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

I don't think these are such common misconceptions that a new compiler
flag would help, especially since that new flag would not be the
default so most beginners would not use it.

> 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.

Wars have been fought over less.

> 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.

It might be appropriate as a warning option, for those who choose to
enforce that style.

And of course there are cases where avoiding multiple declarations
changes the meaning of the code, such as this idiomatic C++:

for (auto first = c.begin(), last = c.end(); first != last; ++first)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Request for compiler option to disable multiple declarations in a single statement
  2018-04-19  9:22 ` David Brown
@ 2018-04-19  9:39   ` Manish Jain
  2018-04-19 15:41     ` David Brown
  0 siblings, 1 reply; 8+ messages in thread
From: Manish Jain @ 2018-04-19  9:39 UTC (permalink / raw)
  To: David Brown, gcc


On 04/19/18 14:46, David Brown wrote:
> Certainly it is heavily used in existing code - making an option
> to disable it would be impractical.

Thanks for replying, Mr. Brown.

What I meant was if an option could be provided, existing code could 
compile without the option, and fresh code to compile with the switch 
enabled (as per user discretion, of course - no compulsion either way 
anywhere).

If the option garners wide recognition / usage, over a period of time 
the standard pointer declaration could be fixed to everyone's happiness:

int* p; // which really is what it should be in all propriety

The current declaration style really sickens me, particularly when 
trying to explain C pointers to others.

Manish Jain

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Request for compiler option to disable multiple declarations in a single statement
  2018-04-19  9:39   ` Manish Jain
@ 2018-04-19 15:41     ` David Brown
  0 siblings, 0 replies; 8+ messages in thread
From: David Brown @ 2018-04-19 15:41 UTC (permalink / raw)
  To: Manish Jain, gcc

On 19/04/18 11:27, Manish Jain wrote:
> 
> On 04/19/18 14:46, David Brown wrote:
>> Certainly it is heavily used in existing code - making an option
>> to disable it would be impractical.
> 
> Thanks for replying, Mr. Brown.
> 
> What I meant was if an option could be provided, existing code could 
> compile without the option, and fresh code to compile with the switch 
> enabled (as per user discretion, of course - no compulsion either way 
> anywhere).

If it were an option for what code is acceptable, rather than a warning,
you would have trouble with header files that had multiple declarations
- it would quickly become unusable.  A warning is much friendlier in
such contexts.  (In particular, warnings like that are by default
disabled for system headers.)

> 
> If the option garners wide recognition / usage, over a period of time 
> the standard pointer declaration could be fixed to everyone's happiness:
> 
> int* p; // which really is what it should be in all propriety

That is a /highly/ subjective view.  I too prefer one declaration per
line in most cases, but I am not the only C programmer around - you
can't consider your personal opinion to be the only right one.

> 
> The current declaration style really sickens me, particularly when 
> trying to explain C pointers to others.
> 

There is nothing to stop you using the style you want in the code you
write yourself.  This warning would simply be a way to mark breaking a
code style guiderule.

It may be better implemented in a plugin, rather than in gcc itself.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Request for compiler option to disable multiple declarations in a single statement
  2018-04-19  9:28 ` Jonathan Wakely
@ 2018-04-19 15:45   ` Manish Jain
  2018-04-19 17:17     ` Jonathan Wakely
  0 siblings, 1 reply; 8+ messages in thread
From: Manish Jain @ 2018-04-19 15:45 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc

> Wars have been fought over less.

I joined the list to make the request. So I just hope my war is not in a 
lonely one-man army.

>> 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.
> 
> It might be appropriate as a warning option, for those who choose to
> enforce that style.

Fine - if anyone could create a warning specifically for multiple 
declarations (and which can be turned into Werror such that it breaks 
code for multiple declarations -- and absolutely nothing else), that 
serves the purpose as well as an option like --single-declarations.

I don't even mind if programmers have to use pragma directives. The end 
result I want is the programmer to have some way of getting the compiler 
break code rather than compile the code at all. Right now, it is a real 
mess that just builds on a simple oversight on the part of Dennis 
Ritchie decades back. The most maddening way to address that mess is 
drop the space between the asterisk and the pointer variable in a 
declaration. Every single thing about pointer usage goes downhill from 
that clumsy fix.

> And of course there are cases where avoiding multiple declarations
> changes the meaning of the code, such as this idiomatic C++:
> 
> for (auto first = c.begin(), last = c.end(); first != last; ++first)
> 

'idiomatic C++' looks more like 'idiotic C++' ever since v11/v14 came 
out. Does Stroustroupe have nothing better to do these days than to make 
a mockery of the once acceptable language ? I like C++ these days just 
as a seafarer likes a close encounter with a shark.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Request for compiler option to disable multiple declarations in a single statement
  2018-04-19 15:45   ` Manish Jain
@ 2018-04-19 17:17     ` Jonathan Wakely
  0 siblings, 0 replies; 8+ messages in thread
From: Jonathan Wakely @ 2018-04-19 17:17 UTC (permalink / raw)
  To: Manish Jain; +Cc: gcc

Insulting people and insisting your preferred coding style (which is
not the one used by GCC's own code, by the way) is definitely a good
way to get people interested in your proposal. </sarcasm>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Request for compiler option to disable multiple declarations in a single statement
  2018-04-19  8:35 Request for compiler option to disable multiple declarations in a single statement Manish Jain
  2018-04-19  9:22 ` David Brown
  2018-04-19  9:28 ` Jonathan Wakely
@ 2018-04-19 22:08 ` Eric Gallager
  2 siblings, 0 replies; 8+ messages in thread
From: Eric Gallager @ 2018-04-19 22:08 UTC (permalink / raw)
  To: Manish Jain; +Cc: gcc

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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2018-04-19 17:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-19  8:35 Request for compiler option to disable multiple declarations in a single statement Manish Jain
2018-04-19  9:22 ` David Brown
2018-04-19  9:39   ` Manish Jain
2018-04-19 15:41     ` David Brown
2018-04-19  9:28 ` Jonathan Wakely
2018-04-19 15:45   ` Manish Jain
2018-04-19 17:17     ` Jonathan Wakely
2018-04-19 22:08 ` Eric Gallager

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).