* Re: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
[not found] <001201cde13f$af3ad4b0$0db07e10$%muller@ics-cnrs.unistra.fr>
@ 2012-12-23 19:29 ` Eli Zaretskii
2012-12-23 21:58 ` Pierre Muller
` (2 more replies)
0 siblings, 3 replies; 12+ messages in thread
From: Eli Zaretskii @ 2012-12-23 19:29 UTC (permalink / raw)
To: Pierre Muller; +Cc: gdb-patches
> From: "Pierre Muller" <pierre.muller@ics-cnrs.unistra.fr>
> Date: Sun, 23 Dec 2012 19:59:49 +0100
>
> Looks almost obvious, but
> as it's the first of that kind,
> I thought I will send it as RFA nonetheless...
>
> Can similar changes be committed as obvious?
> They usually require some formatting changes,
> but generated behavior should not change.
>
>
> Pierre Muller
> as ARI maintainer
>
>
> 2012-12-20 Pierre Muller <muller@sourceware.org>
>
> ARI fixes: Assignment within if rule.
> * cli/cli-cmds.c (shell_escape): Do not set variable value inside
> if statement.
> (edit_command): Likewise.
Since when is that bad C, so much so that we would need to enforce it?
^ permalink raw reply [flat|nested] 12+ messages in thread* RE: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
2012-12-23 19:29 ` [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement Eli Zaretskii
@ 2012-12-23 21:58 ` Pierre Muller
2012-12-23 22:28 ` Andreas Schwab
2012-12-23 22:28 ` Sergio Durigan Junior
2 siblings, 0 replies; 12+ messages in thread
From: Pierre Muller @ 2012-12-23 21:58 UTC (permalink / raw)
To: 'Eli Zaretskii'; +Cc: gdb-patches
> -----Message d'origine-----
> De : gdb-patches-owner@sourceware.org [mailto:gdb-patches-
> owner@sourceware.org] De la part de Eli Zaretskii
> Envoyé : dimanche 23 décembre 2012 20:29
> À : Pierre Muller
> Cc : gdb-patches@sourceware.org
> Objet : Re: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if
> statement
>
> > From: "Pierre Muller" <pierre.muller@ics-cnrs.unistra.fr>
> > Date: Sun, 23 Dec 2012 19:59:49 +0100
> >
> > Looks almost obvious, but
> > as it's the first of that kind,
> > I thought I will send it as RFA nonetheless...
> >
> > Can similar changes be committed as obvious?
> > They usually require some formatting changes,
> > but generated behavior should not change.
> >
> >
> > Pierre Muller
> > as ARI maintainer
> >
> >
> > 2012-12-20 Pierre Muller <muller@sourceware.org>
> >
> > ARI fixes: Assignment within if rule.
> > * cli/cli-cmds.c (shell_escape): Do not set variable value
inside
> > if statement.
> > (edit_command): Likewise.
>
> Since when is that bad C, so much so that we would need to enforce it?
Eli, as you know,
I am not a C specialist at all...
But I didn't invent that rule either.
This rule part of gdb_ari.sh since it's introduction in CVS ss repository
back in 2002.
It's probably now your chance to challenge this rule...
I would tend to agree that an assignment should be a statement on it's
own, but you could rightly consider this as a 'pascal' language
preference!
Pierre Muller
both ARI and pascal language support maintainer!
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
2012-12-23 19:29 ` [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement Eli Zaretskii
2012-12-23 21:58 ` Pierre Muller
@ 2012-12-23 22:28 ` Andreas Schwab
2012-12-24 3:41 ` Eli Zaretskii
2012-12-23 22:28 ` Sergio Durigan Junior
2 siblings, 1 reply; 12+ messages in thread
From: Andreas Schwab @ 2012-12-23 22:28 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: Pierre Muller, gdb-patches
Eli Zaretskii <eliz@gnu.org> writes:
> Since when is that bad C, so much so that we would need to enforce it?
It's part of the GNU coding standards.
Andreas.
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
2012-12-23 22:28 ` Andreas Schwab
@ 2012-12-24 3:41 ` Eli Zaretskii
2012-12-24 10:17 ` Andreas Schwab
2013-01-10 12:28 ` Pierre Muller
0 siblings, 2 replies; 12+ messages in thread
From: Eli Zaretskii @ 2012-12-24 3:41 UTC (permalink / raw)
To: Andreas Schwab; +Cc: pierre.muller, gdb-patches
> From: Andreas Schwab <schwab@linux-m68k.org>
> Cc: Pierre Muller <pierre.muller@ics-cnrs.unistra.fr>, gdb-patches@sourceware.org
> Date: Sun, 23 Dec 2012 23:28:45 +0100
>
> Eli Zaretskii <eliz@gnu.org> writes:
>
> > Since when is that bad C, so much so that we would need to enforce it?
>
> It's part of the GNU coding standards.
Which says "Try not to...".
FWIW, Emacs sources are replete with such assignments, and I don't see
anything wrong with that in the first place.
So yes, I guess I'm challenging this ARI rule.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
2012-12-24 3:41 ` Eli Zaretskii
@ 2012-12-24 10:17 ` Andreas Schwab
2013-01-10 12:28 ` Pierre Muller
1 sibling, 0 replies; 12+ messages in thread
From: Andreas Schwab @ 2012-12-24 10:17 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: pierre.muller, gdb-patches
Eli Zaretskii <eliz@gnu.org> writes:
> FWIW, Emacs sources are replete with such assignments,
Yes, they should be avoided.
Andreas.
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 12+ messages in thread
* RE: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
2012-12-24 3:41 ` Eli Zaretskii
2012-12-24 10:17 ` Andreas Schwab
@ 2013-01-10 12:28 ` Pierre Muller
2013-01-11 4:29 ` Joel Brobecker
1 sibling, 1 reply; 12+ messages in thread
From: Pierre Muller @ 2013-01-10 12:28 UTC (permalink / raw)
To: 'Eli Zaretskii', 'Andreas Schwab'; +Cc: gdb-patches
Hi all,
I would like to know what the status of this patch is,
and more generally of the ARI rule: "Avoid assignment inside if statement".
Eli wanted to challenge this rule,
but others seems to be in favor...
I really do find code easier to read if assignments are done
before the if statement, but this is due to my pascal background,
which forbids it.
Pierre Muller,
as ARI maintainer.
> -----Message d'origine-----
> De : gdb-patches-owner@sourceware.org [mailto:gdb-patches-
> owner@sourceware.org] De la part de Eli Zaretskii
> Envoyé : lundi 24 décembre 2012 04:41
> À : Andreas Schwab
> Cc : pierre.muller@ics-cnrs.unistra.fr; gdb-patches@sourceware.org
> Objet : Re: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if
> statement
>
> > From: Andreas Schwab <schwab@linux-m68k.org>
> > Cc: Pierre Muller <pierre.muller@ics-cnrs.unistra.fr>, gdb-
> patches@sourceware.org
> > Date: Sun, 23 Dec 2012 23:28:45 +0100
> >
> > Eli Zaretskii <eliz@gnu.org> writes:
> >
> > > Since when is that bad C, so much so that we would need to enforce it?
> >
> > It's part of the GNU coding standards.
>
> Which says "Try not to...".
>
> FWIW, Emacs sources are replete with such assignments, and I don't see
> anything wrong with that in the first place.
>
> So yes, I guess I'm challenging this ARI rule.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
2013-01-10 12:28 ` Pierre Muller
@ 2013-01-11 4:29 ` Joel Brobecker
0 siblings, 0 replies; 12+ messages in thread
From: Joel Brobecker @ 2013-01-11 4:29 UTC (permalink / raw)
To: Pierre Muller
Cc: 'Eli Zaretskii', 'Andreas Schwab', gdb-patches
> I would like to know what the status of this patch is,
> and more generally of the ARI rule: "Avoid assignment inside if statement".
I think that we should continue to discourage the use of assignments
inside if statements, and the ARI is a good tool for that. If someone
happens to come across a situation where it is clearly advantageous
to break this rule, then we can document it using an ARI comment
that clearly allows it.
--
Joel
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
2012-12-23 19:29 ` [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement Eli Zaretskii
2012-12-23 21:58 ` Pierre Muller
2012-12-23 22:28 ` Andreas Schwab
@ 2012-12-23 22:28 ` Sergio Durigan Junior
2012-12-24 4:11 ` Joel Brobecker
2 siblings, 1 reply; 12+ messages in thread
From: Sergio Durigan Junior @ 2012-12-23 22:28 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: Pierre Muller, gdb-patches
On Sunday, December 23 2012, Eli Zaretskii wrote:
>> From: "Pierre Muller" <pierre.muller@ics-cnrs.unistra.fr>
>> Date: Sun, 23 Dec 2012 19:59:49 +0100
>>
>> Looks almost obvious, but
>> as it's the first of that kind,
>> I thought I will send it as RFA nonetheless...
>>
>> Can similar changes be committed as obvious?
>> They usually require some formatting changes,
>> but generated behavior should not change.
>>
>>
>> Pierre Muller
>> as ARI maintainer
>>
>>
>> 2012-12-20 Pierre Muller <muller@sourceware.org>
>>
>> ARI fixes: Assignment within if rule.
>> * cli/cli-cmds.c (shell_escape): Do not set variable value inside
>> if statement.
>> (edit_command): Likewise.
>
> Since when is that bad C, so much so that we would need to enforce it?
Interesting... I remember when I started hacking GDB, I was strongly
discouraged to do assignments inside `if' checkings. I don't remember
who told me that, but the reason was something related to the Coding
Standards (I don't have any references either). Anyway, since that
moment I stopped doing this...
FWIW, I agree that it is not bad C, and totally valid, so I for one vote
to remove this rule from ARI. Nevertheless, I will take a closer look
at the Coding Standards to make sure we're not doing something "wrong"
in this case...
--
Sergio
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
2012-12-23 22:28 ` Sergio Durigan Junior
@ 2012-12-24 4:11 ` Joel Brobecker
0 siblings, 0 replies; 12+ messages in thread
From: Joel Brobecker @ 2012-12-24 4:11 UTC (permalink / raw)
To: Sergio Durigan Junior; +Cc: Eli Zaretskii, Pierre Muller, gdb-patches
> > Since when is that bad C, so much so that we would need to enforce it?
>
> Interesting... I remember when I started hacking GDB, I was strongly
> discouraged to do assignments inside `if' checkings. I don't remember
> who told me that, but the reason was something related to the Coding
> Standards (I don't have any references either). Anyway, since that
> moment I stopped doing this...
It is in the GNU Coding Standards:
http://www.gnu.org/prep/standards/standards.html#Syntactic-Conventions
In particular:
| Try to avoid assignments inside if-conditions (assignments inside
| while-conditions are ok). For example, donâÂÂt write this:
|
| if ((foo = (char *) malloc (sizeof *foo)) == 0)
| fatal ("virtual memory exhausted");
| instead, write this:
|
| foo = (char *) malloc (sizeof *foo);
| if (foo == 0)
| fatal ("virtual memory exhausted");
FWIW, I mostly agree with this suggestion, particulary in the
examples above. I do remember seeing some examples where it was
more practical if we could have assignments inside conditions
(in an "if ... else if ..." situation), but even then, I find
that it makes it harder to notice the assignment.
In the examples that Pierre chose to fix, it seems obvious to me
(this is a judgement call, so potentially subject to disagreement)
that there was no need to have the assignment inside the condition,
and that the patched code is easier to grasp. I think it is
a clear improvement.
> FWIW, I agree that it is not bad C, and totally valid
I think that the point is not that this is bad C, or not
valid/portable. I think that the point is that it is judged
to be a poor practice.
--
Joel
^ permalink raw reply [flat|nested] 12+ messages in thread
* [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
@ 2012-12-23 19:00 Pierre Muller
2012-12-24 4:17 ` Joel Brobecker
0 siblings, 1 reply; 12+ messages in thread
From: Pierre Muller @ 2012-12-23 19:00 UTC (permalink / raw)
To: gdb-patches
Looks almost obvious, but
as it's the first of that kind,
I thought I will send it as RFA nonetheless...
Can similar changes be committed as obvious?
They usually require some formatting changes,
but generated behavior should not change.
Pierre Muller
as ARI maintainer
2012-12-20 Pierre Muller <muller@sourceware.org>
ARI fixes: Assignment within if rule.
* cli/cli-cmds.c (shell_escape): Do not set variable value inside
if statement.
(edit_command): Likewise.
Index: src/gdb/cli/cli-cmds.c
===================================================================
RCS file: /cvs/src/src/gdb/cli/cli-cmds.c,v
retrieving revision 1.143
diff -u -p -r1.143 cli-cmds.c
--- src/gdb/cli/cli-cmds.c 18 Dec 2012 19:27:35 -0000 1.143
+++ src/gdb/cli/cli-cmds.c 20 Dec 2012 16:10:27 -0000
@@ -724,11 +724,13 @@ shell_escape (char *arg, int from_tty)
#else /* Can fork. */
int status, pid;
- if ((pid = vfork ()) == 0)
+ pid = vfork ();
+ if (pid == 0)
{
const char *p, *user_shell;
- if ((user_shell = (char *) getenv ("SHELL")) == NULL)
+ user_shell = (char *) getenv ("SHELL");
+ if (user_shell == NULL)
user_shell = "/bin/sh";
/* Get the name of the shell for arg0. */
@@ -837,7 +839,8 @@ edit_command (char *arg, int from_tty)
error (_("No line number known for %s."), arg);
}
- if ((editor = (char *) getenv ("EDITOR")) == NULL)
+ editor = (char *) getenv ("EDITOR");
+ if (editor == NULL)
editor = "/bin/ex";
/* If we don't already know the full absolute file name of the
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
2012-12-23 19:00 Pierre Muller
@ 2012-12-24 4:17 ` Joel Brobecker
2012-12-24 10:22 ` Andreas Schwab
0 siblings, 1 reply; 12+ messages in thread
From: Joel Brobecker @ 2012-12-24 4:17 UTC (permalink / raw)
To: Pierre Muller; +Cc: gdb-patches
> 2012-12-20 Pierre Muller <muller@sourceware.org>
>
> ARI fixes: Assignment within if rule.
> * cli/cli-cmds.c (shell_escape): Do not set variable value inside
> if statement.
> (edit_command): Likewise.
Thanks for doing this, Pierre!
Aside from the meta-discussion on the rule itself, does anyone
see a problem with this patch? I see it as a clear improvement,
so I would like to approve it.
One tiny comment below...
> Index: src/gdb/cli/cli-cmds.c
> ===================================================================
> RCS file: /cvs/src/src/gdb/cli/cli-cmds.c,v
> retrieving revision 1.143
> diff -u -p -r1.143 cli-cmds.c
> --- src/gdb/cli/cli-cmds.c 18 Dec 2012 19:27:35 -0000 1.143
> +++ src/gdb/cli/cli-cmds.c 20 Dec 2012 16:10:27 -0000
> @@ -724,11 +724,13 @@ shell_escape (char *arg, int from_tty)
> #else /* Can fork. */
> int status, pid;
>
> - if ((pid = vfork ()) == 0)
> + pid = vfork ();
> + if (pid == 0)
> {
> const char *p, *user_shell;
>
> - if ((user_shell = (char *) getenv ("SHELL")) == NULL)
> + user_shell = (char *) getenv ("SHELL");
> + if (user_shell == NULL)
> user_shell = "/bin/sh";
>
> /* Get the name of the shell for arg0. */
> @@ -837,7 +839,8 @@ edit_command (char *arg, int from_tty)
> error (_("No line number known for %s."), arg);
> }
>
> - if ((editor = (char *) getenv ("EDITOR")) == NULL)
> + editor = (char *) getenv ("EDITOR");
According to POSIX, getenv returns char *, so I do not think
that the cast is necessary. Let's try without and see what happens...
> + if (editor == NULL)
> editor = "/bin/ex";
>
> /* If we don't already know the full absolute file name of the
--
Joel
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement
2012-12-24 4:17 ` Joel Brobecker
@ 2012-12-24 10:22 ` Andreas Schwab
0 siblings, 0 replies; 12+ messages in thread
From: Andreas Schwab @ 2012-12-24 10:22 UTC (permalink / raw)
To: Joel Brobecker; +Cc: Pierre Muller, gdb-patches
Joel Brobecker <brobecker@adacore.com> writes:
>> Index: src/gdb/cli/cli-cmds.c
>> ===================================================================
>> RCS file: /cvs/src/src/gdb/cli/cli-cmds.c,v
>> retrieving revision 1.143
>> diff -u -p -r1.143 cli-cmds.c
>> --- src/gdb/cli/cli-cmds.c 18 Dec 2012 19:27:35 -0000 1.143
>> +++ src/gdb/cli/cli-cmds.c 20 Dec 2012 16:10:27 -0000
>> @@ -724,11 +724,13 @@ shell_escape (char *arg, int from_tty)
>> #else /* Can fork. */
>> int status, pid;
>>
>> - if ((pid = vfork ()) == 0)
>> + pid = vfork ();
>> + if (pid == 0)
>> {
>> const char *p, *user_shell;
>>
>> - if ((user_shell = (char *) getenv ("SHELL")) == NULL)
>> + user_shell = (char *) getenv ("SHELL");
>> + if (user_shell == NULL)
>> user_shell = "/bin/sh";
>>
>> /* Get the name of the shell for arg0. */
>> @@ -837,7 +839,8 @@ edit_command (char *arg, int from_tty)
>> error (_("No line number known for %s."), arg);
>> }
>>
>> - if ((editor = (char *) getenv ("EDITOR")) == NULL)
>> + editor = (char *) getenv ("EDITOR");
>
> According to POSIX, getenv returns char *, so I do not think
> that the cast is necessary. Let's try without and see what happens...
Even more so as editor should really be const char * (and user_shell
already is).
Andreas.
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2013-01-11 4:29 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <001201cde13f$af3ad4b0$0db07e10$%muller@ics-cnrs.unistra.fr>
2012-12-23 19:29 ` [RFA] (cli/cli-cmds.c) ARI fix: Avoid assignment inside if statement Eli Zaretskii
2012-12-23 21:58 ` Pierre Muller
2012-12-23 22:28 ` Andreas Schwab
2012-12-24 3:41 ` Eli Zaretskii
2012-12-24 10:17 ` Andreas Schwab
2013-01-10 12:28 ` Pierre Muller
2013-01-11 4:29 ` Joel Brobecker
2012-12-23 22:28 ` Sergio Durigan Junior
2012-12-24 4:11 ` Joel Brobecker
2012-12-23 19:00 Pierre Muller
2012-12-24 4:17 ` Joel Brobecker
2012-12-24 10:22 ` Andreas Schwab
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox