Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 17 Sep 2012 07:21:52 +1000 (EST)
From:      Bruce Evans <brde@optusnet.com.au>
To:        Chris Rees <utisoft@gmail.com>
Cc:        Ian Lepore <freebsd@damnhippie.dyndns.org>, Alexey Dokuchaev <danfe@freebsd.org>, src-committers@freebsd.org, svn-src-all@freebsd.org, David Chisnall <theraven@freebsd.org>, Andrew Turner <andrew@freebsd.org>, Bruce Evans <brde@optusnet.com.au>, svn-src-head@freebsd.org
Subject:   Re: svn commit: r240549 - head/sys/arm/tegra
Message-ID:  <20120917063509.E3825@besplex.bde.org>
In-Reply-To: <CADLo839RJQY6hsoryBGR382tQhfVLBRTLeJOPkNntg77KjZ4kg@mail.gmail.com>
References:  <201209160755.q8G7tnMH036332@svn.freebsd.org> <20120916092818.GA81952@FreeBSD.org> <CADLo83-5zQBdf3ndBXO4qbkXJgaFPxGfE86LSDGqJ4g3k_4gkw@mail.gmail.com> <38FB39C4-7626-4D54-8197-E302BB9452D2@FreeBSD.org> <CADLo8381qBMC0TvJ8BXd29OFBcB9=R0NNi_idXi-h4OZ=DHWMA@mail.gmail.com> <1347817284.1114.2.camel@revolution.hippie.lan> <CADLo839RJQY6hsoryBGR382tQhfVLBRTLeJOPkNntg77KjZ4kg@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, 16 Sep 2012, Chris Rees wrote:

> On 16 September 2012 18:41, Ian Lepore <freebsd@damnhippie.dyndns.org> wrote:
>> On Sun, 2012-09-16 at 18:30 +0100, Chris Rees wrote:
>>> On 16 Sep 2012 16:19, "David Chisnall" <theraven@freebsd.org> wrote:
>>>>
>>>> On 16 Sep 2012, at 10:37, Chris Rees wrote:
>>>>
>>>>> Actually:
>>>>>
>>>>> for (;;)
>>>>>        ;
>>>>
>>>> This form will generate a compiler warning, because it looks like a
>>> missing loop body.  You can silence the warning by this form:
>>>>
>>>> for (;;) {}
>>>>
>>>> This makes it clear that you have an explicit body containing no
>>> statements.

This is forbidden by style(9) (it has excessive braces).

>>> Please fix the style man page then, and make sure bde approves!
>>
>> One of the examples allows
>>
>>     for (;;)
>>         stmt;
>>
>> Using "continue;" as the statement is consistant with that.

So is using a null statement.  Actually, the informal grammar in the
example is buggy.  In more formal grammars, the semicolon is part of
a statement (unless the statement is compound).  So the above should
really be:

 	for (;;)
 		statement

but that looks strange, and we know what the current version means.

> Ok, so with approval from a docs/src guy I'd like to commit [1] to
> update the style page to use continue; instead of a lone ;
> ...
> [1] http://www.bayofrum.net/~crees/patches/style-empty-loop.diff

I don't like this.  The style is whatever it was, not what you want
it to be.  However, I couldn't find a single example in /usr/src/*bin
or libc that has an empty for loop.  Normal code just doesn't do that.
It would be better to remove the example of the empty loop in style(9),
so that style(9) doesn't require any particular style for rarely-written
code.

Oops.  The loop that you changed is not the above fully empty one, but
one with control statements but no body.  I couldn't find any like
the above, and ones with control statements are too hard to grep for.
The one that you changed is basically strlen(), and an example of
existing (mal)practice was in the 4.4BSD version of strlen.c:

% size_t
% strlen(str)
% 	const char *str;
% {
% 	const char *s;
% 
% 	for (s = str; *s; ++s);
% 	return(s - str);
% }

It doesn't even put the semicolon on a new line.  strlen.c is now
recomplicated, so it no longer provides a bad example.  strlen() is also
in K&R, with better style but a while loop:

 	while (s[i] != '\0')
 		i++;

and of course the classic obfuscated idiom for strcpy is to do all the
work in the control statment.  This obfuscation is done fairly well in
4.4BSD again:

 	for (; *to = *from; ++from, ++to);

(why not *to++, etc?).

I use a single semicolon for fully empty loops, but not put all of the
work in the control statement for loops that do something, so that the
never have an empty body.  Adding 'continue /* nothing */' to the old
BSD code wouldn't improve it.  Its only good point is that it is concise,
and this is defeated by redundant addiitons.

Bruce



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120917063509.E3825>