From: "Hannes Eder" <hannes@hanneseder.net>
To: "Linus Torvalds" <torvalds@linux-foundation.org>
Cc: "Krzysztof Halasa" <khc@pm.waw.pl>,
"Harvey Harrison" <harvey.harrison@gmail.com>,
"Håkon Løvdal" <hlovdal@gmail.com>,
netdev@vger.kernel.org, kernel-janitors@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 02/27] drivers/net: fix sparse warnings: make do-while a compound statement
Date: Mon, 29 Dec 2008 15:35:31 +0100 [thread overview]
Message-ID: <154e089b0812290635o5792af25jd26fa184a02dc7af@mail.gmail.com> (raw)
In-Reply-To: <alpine.LFD.2.00.0812231529030.3535@localhost.localdomain>
On Wed, Dec 24, 2008 at 12:38 AM, Linus Torvalds
<torvalds@linux-foundation.org> wrote:
>
>
> On Wed, 24 Dec 2008, Krzysztof Halasa wrote:
>>
>> So is a case like
>> do
>> x;
>> while (y);
>> It can't be made more clear with brackets.
>
> Oh yes it can. People look at that, and it's so uncommon that they
> literally believe it is a mis-indent.
>
> [snip]
>
>> kmalloc(sizeof i) just doesn't look good, the operator looks like
>> a variable name.
>
> And I agree with you. "sizeof i" doesn't look good. It's uncommon, and
> doesn't match peoples expectations.
>
>> But there is this return statement. Some people tend to write
>> return (x); I simply write return x;
>
> I do to. And it's the common thing to do, and only totally confused people
> think that 'return' is a somehow remotely like a "function" of its
> arguments (the way 'sizeof' is - sizeof _is_ a function of its arguments,
> albeit a very rare one).
>
>> It's clear, and so is a simple do-while.
>
> No. "return x;" is clear because it's the common thing, which means that
> peopel are good at reading it.
I got curious and conducted a little experiment, here is the outcome
for the linux-2.6 kernel tree:
hannes@vmbox:~/linux-2.6$ find . -name "*.[ch]" -print0 | xargs -0 cat
| ../sparse/cstats -
stats for '-':
do's: 8092, non compound: 79 ( 1.0%)
sizeof's: 51216, without parenthesis: 1543 ( 3.0%)
return's: 286167, with parenthesis: 13552 ( 4.7%)
'cstats' is a little program I wrote using the sparse library, see below.
The value for "return's with parenthesis" is a bit of an estimation,
as 'cstats' operates only at the token level, so
"return (x) ? y : z;" counts as "return with parenthesis".
some sanity checks, to ensure the magnitudes are right:
hannes@vmbox:~/linux-2.6$ git grep -w do -- *.[ch] | wc -l
20599
hannes@vmbox:~/linux-2.6$ git grep '\bdo[ \t]*{' -- *.[ch] | wc -l
7805
I assume 'do' is used frequently in comments:
hannes@vmbox:~/linux-2.6$ git grep '\*.*\bdo\b' -- *.[ch] | wc -l
11358
hannes@vmbox:~/linux-2.6$ git grep '^[ \t]*do$' -- *.[ch] | wc -l
34
hannes@vmbox:~/linux-2.6$ git grep -w sizeof -- *.[ch] | wc -l
49631
constructs like sizeof(array)/sizeof(array[0]) or common:
hannes@vmbox:~/linux-2.6$ git grep 'sizeof.*sizeof' -- *.[ch] | wc -l
1827
hannes@vmbox:~/linux-2.6$ git grep '\bsizeof [^(]' -- *.[ch] | wc -l
1534
hannes@vmbox:~/linux-2.6$ git grep -w return -- *.[ch] | wc -l
295304
hannes@vmbox:~/linux-2.6$ git grep '\breturn (' -- *.[ch] | wc -l
11067
Best,
Hannes
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "token.h"
#include "parse.h"
int main(int argc, char **argv)
{
struct string_list *filelist = NULL;
char *filename;
sparse_initialize(argc, argv, &filelist);
FOR_EACH_PTR_NOTAG(filelist, filename) {
int fd;
struct token *token;
int do_stats = 0;
int do_stats_nc = 0;
int sizeof_exprs = 0;
int sizeof_exprs_np = 0;
int return_stats = 0;
int return_stats_wp = 0;
if (strcmp(filename, "-") == 0) {
fd = 0;
} else {
fd = open(filename, O_RDONLY);
if (fd < 0)
die("No such file: %s", filename);
}
// Tokenize the input stream
token = tokenize(filename, fd, NULL, includepath);
close(fd);
for ( ; !eof_token(token); token = token->next) {
if (token_type(token) == TOKEN_IDENT) {
if (token->ident == &do_ident) {
do_stats++;
if (!match_op(token->next, '{'))
do_stats_nc++;
}
else if (token->ident == &sizeof_ident) {
sizeof_exprs++;
if (!match_op(token->next, '('))
sizeof_exprs_np++;
}
else if (token->ident == &return_ident) {
return_stats++;
if (match_op(token->next, '('))
return_stats_wp++;
}
}
}
printf("stats for '%s':\n", filename);
printf(" do's: %8d, non compound: %8d (%5.1f%%)\n",
do_stats, do_stats_nc,
100.0 * do_stats_nc / (do_stats?:1) );
printf(" sizeof's: %8d, without parenthesis: %8d (%5.1f%%)\n",
sizeof_exprs, sizeof_exprs_np,
100.0 * sizeof_exprs_np / (sizeof_exprs?:1) );
printf(" return's: %8d, with parenthesis: %8d (%5.1f%%)\n",
return_stats, return_stats_wp,
100.0 * return_stats_wp / (return_stats?:1) );
} END_FOR_EACH_PTR_NOTAG(filename);
return 0;
}
next prev parent reply other threads:[~2008-12-29 14:35 UTC|newest]
Thread overview: 74+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-12-22 19:14 [PATCH 00/27] drivers/net: fix sparse warnings Hannes Eder
2008-12-22 19:14 ` [PATCH 01/27] drivers/net: fix sparse warning: use ANSI-style function declaration Hannes Eder
2008-12-26 7:53 ` David Miller
2008-12-22 19:15 ` [PATCH 02/27] drivers/net: fix sparse warnings: make do-while a compound statement Hannes Eder
2008-12-22 22:14 ` Krzysztof Halasa
2008-12-22 23:44 ` Håkon Løvdal
2008-12-23 16:31 ` Krzysztof Halasa
2008-12-23 17:26 ` Harvey Harrison
2008-12-23 17:36 ` Krzysztof Halasa
2008-12-23 18:08 ` Linus Torvalds
2008-12-23 23:18 ` Krzysztof Halasa
2008-12-23 23:38 ` Linus Torvalds
2008-12-24 2:03 ` Krzysztof Halasa
2008-12-24 2:10 ` Linus Torvalds
2008-12-25 17:02 ` Krzysztof Halasa
2008-12-25 6:17 ` Junio C Hamano
2008-12-29 14:35 ` Hannes Eder [this message]
2008-12-26 7:55 ` David Miller
2008-12-22 19:15 ` [PATCH 03/27] drivers/net: fix sparse warning: returning void-valued expression Hannes Eder
2008-12-26 0:17 ` David Miller
2008-12-26 14:39 ` Hannes Eder
2008-12-26 19:59 ` Randy Dunlap
2008-12-27 19:11 ` Hannes Eder
2008-12-27 19:20 ` Sam Ravnborg
2008-12-27 21:38 ` [PATCH] Makefile: disable sparse warning "returning void-valued expression" Hannes Eder
2008-12-27 21:57 ` Sam Ravnborg
2008-12-26 7:56 ` [PATCH 03/27] drivers/net: fix sparse warning: returning void-valued expression David Miller
2008-12-22 19:15 ` [PATCH 04/27] drivers/net: fix sparse warnings: make symbols static Hannes Eder
2008-12-26 7:57 ` David Miller
2008-12-22 19:15 ` [PATCH 05/27] drivers/net/arcnet: " Hannes Eder
2008-12-26 7:57 ` David Miller
2008-12-22 19:15 ` [PATCH 06/27] drivers/net/atlx: " Hannes Eder
2008-12-26 7:58 ` David Miller
2008-12-22 19:15 ` [PATCH 07/27] drivers/net/bonding: fix sparse warnings: move decls to header file Hannes Eder
2008-12-26 7:59 ` David Miller
2008-12-22 19:16 ` [PATCH 08/27] drivers/net/cxgb3: comment out dead code Hannes Eder
2008-12-26 7:59 ` David Miller
2008-12-22 19:16 ` [PATCH 09/27] drivers/net/e1000e: fix sparse warnings: make symbols static Hannes Eder
2008-12-22 19:16 ` [PATCH 10/27] drivers/net/enic: fix sparse warning: make symbol static Hannes Eder
2008-12-26 8:01 ` David Miller
2008-12-22 19:16 ` [PATCH 11/27] drivers/net/igb: remove dead code (function 'igb_read_pci_cfg') Hannes Eder
2008-12-22 22:30 ` Jeff Kirsher
2008-12-26 8:03 ` David Miller
2008-12-22 19:16 ` [PATCH 12/27] drivers/net/irda: fix sparse warnings: make symbols static Hannes Eder
2008-12-26 8:03 ` David Miller
2008-12-22 19:16 ` [PATCH 13/27] drivers/net/ixgbe: " Hannes Eder
2008-12-26 8:04 ` David Miller
2008-12-22 19:16 ` [PATCH 14/27] drivers/net/netxen: fix sparse warnings: use NULL pointer instead of plain integer Hannes Eder
2008-12-26 8:04 ` David Miller
2008-12-22 19:17 ` [PATCH 15/27] drivers/net/qlge: fix sparse warnings: make symbols static Hannes Eder
2008-12-26 8:05 ` David Miller
2008-12-22 19:17 ` [PATCH 16/27] drivers/net/skfp: " Hannes Eder
2008-12-26 8:06 ` David Miller
2008-12-22 19:17 ` [PATCH 17/27] drivers/net/tokenring: " Hannes Eder
2008-12-26 8:07 ` David Miller
2008-12-22 19:17 ` [PATCH 18/27] drivers/net/tulip: fix sparse warnings: make do-while a compound statement Hannes Eder
2008-12-26 8:07 ` David Miller
2008-12-22 19:17 ` [PATCH 19/27] drivers/net/usb: fix sparse warnings: make symbols static Hannes Eder
2008-12-26 8:08 ` David Miller
2008-12-22 19:17 ` [PATCH 20/27] drivers/net/wan: fix sparse warnings: make do-while a compound statement Hannes Eder
2008-12-22 22:09 ` Krzysztof Halasa
2008-12-22 19:18 ` [PATCH 21/27] drivers/net/wan: fix sparse warning: make symbol static Hannes Eder
2008-12-26 8:11 ` David Miller
2008-12-22 19:18 ` [PATCH 22/27] drivers/net/wan/z85230.c: fix sparse warnings: un-EXPORT symbols Hannes Eder
2008-12-26 8:12 ` David Miller
2008-12-22 19:18 ` [PATCH 23/27] drivers/net/wireless: fix sparse warnings: make symbols static Hannes Eder
2008-12-26 8:13 ` David Miller
2008-12-22 19:18 ` [PATCH 24/27] drivers/net/wireless/ath9k: " Hannes Eder
2008-12-22 19:18 ` [PATCH 25/27] drivers/net/wireless/b43: " Hannes Eder
2008-12-26 8:13 ` David Miller
2008-12-22 19:18 ` [PATCH 26/27] drivers/net/wireless/ipw2x00: " Hannes Eder
2008-12-26 8:14 ` David Miller
2008-12-22 19:19 ` [PATCH 27/27] drivers/net/wireless/prism54: " Hannes Eder
2008-12-26 8:15 ` David Miller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=154e089b0812290635o5792af25jd26fa184a02dc7af@mail.gmail.com \
--to=hannes@hanneseder.net \
--cc=harvey.harrison@gmail.com \
--cc=hlovdal@gmail.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=khc@pm.waw.pl \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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 NNTP newsgroup(s).