Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756742AbbDOVHB (ORCPT ); Wed, 15 Apr 2015 17:07:01 -0400 Received: from smtprelay0018.hostedemail.com ([216.40.44.18]:52152 "EHLO smtprelay.hostedemail.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1756462AbbDOVGn (ORCPT ); Wed, 15 Apr 2015 17:06:43 -0400 X-Session-Marker: 6A6F6540706572636865732E636F6D X-Spam-Summary: 2,0,0,,d41d8cd98f00b204,joe@perches.com,:::::::,RULES_HIT:2:41:355:379:541:973:982:988:989:1260:1277:1311:1313:1314:1345:1359:1373:1437:1515:1516:1518:1535:1593:1594:1605:1606:1730:1747:1777:1792:1801:2110:2198:2199:2393:2559:2562:2828:3138:3139:3140:3141:3142:3622:3653:3865:3866:3867:3868:3870:3871:3872:3873:3874:4250:4321:4605:4823:5007:6117:6119:6261:7903:7904:8828:8957:9010:9149:10004:10559:10848:11026:11232:11658:11914:12043:12296:12438:12517:12519:12555:12663:12740:13019:13161:13229:21080,0,RBL:none,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0 X-HE-Tag: crack97_9169fc7c91662 X-Filterd-Recvd-Size: 5912 Message-ID: <1429131997.2850.15.camel@perches.com> Subject: CodingStyle parenthesis alignment: was: Re: align to open paren From: Joe Perches To: "Hubbe, Allen" Cc: "apw@canonical.com" , LKML , netdev Date: Wed, 15 Apr 2015 14:06:37 -0700 In-Reply-To: <40F65EF2B5E2254199711F58E3ACB84D781CD892@MX104CL02.corp.emc.com> References: <40F65EF2B5E2254199711F58E3ACB84D781CD892@MX104CL02.corp.emc.com> Content-Type: text/plain; charset="ISO-8859-1" X-Mailer: Evolution 3.12.10-0ubuntu1~14.10.1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 5126 Lines: 140 On Wed, 2015-04-15 at 20:34 +0000, Hubbe, Allen wrote: > Hello Andy, Joe, Hello Allen. As this is a discussion better suited for linux development lists I've cc'd LKML and netdev. > I would like to find the origin of the decision to align to the open > paren in Linux. Mostly it's a style decision shared by net/ and drivers/net/ and a few other directories. It's a checkpatch --strict only test so it's not on by default except in net/ and drivers/net/. > I found the commit where this was added a few years ago, d1fe9c0. > The email thread just says the style should be that way, but not why > or how the decision was made. The how and the why is what I'm after, > since I have a critique of the chosen style. > > I realize there is a lot of code written using this stile, and > changing it would be disruptive. I certainly would not ask for that. > > Indenting to the open paren might cause ambiguous indentation between > the parenthesized expression and the next logical thing. In the > following, next_thing aligns to the same column as baz, even though > baz is part of the condition expression, and next_thing is the > continued statement. > > = if (foo(bar, > = baz)) > = next_thing(); > > It would be necessary to reindent to maintain style, even though the > code of the next lines is the same. This has consequences like > changing the blame, for instance. In the following, 4 + 5 is the bug, > but whoever replaced the global with an instance variable gets the > blame. blame is overrated. git blame -w ignores most of the whitespace noise. > = global_variable = foo(bar, > = baz(1 + 3), > = baz(4 + 5) + 6); > with > = obj->var = foo(bar, > = baz(1 + 3), > = baz(4 + 5) + 6); > > I'm used to the default in many editors, which is to indent twice > following each open paren, as opposed to once following each open > brace or continued statement. It is a simpler rule for automatic > formatting to implement. It also avoids mixing tabs and spaces, the > spacing is unambiguous, and to maintain style, there is no need to > re-indent lines following an edit if the position of the open paren > changes. > > It's interesting to me that this style is only enforced by > checkpatch.pl --strict. It is not in Documents/CodingStyle. That > being the case, would it be acceptable to relax the rule in > checkpatch.pl to accept either style? If not, well, I'll just accept > the chosen style and fix my code. I personally don't care much either way. > If the following looks acceptable to you, I'll submit the patch to the > list. The patch most likely wouldn't be appropriate for net/ and drivers/net/ where the developers seem to strongly prefer alignment to open parenthesis. Also I think the open parenthesis count isn't right as it would ask for multiple indents for code like: if ((foo(bar)) && (baz(bar))) { I think checkpatch would now want: if ((foo(bar)) && (baz(bar))) { and the --fix option would be wrong too. cheers, Joe > Thanks, > Allen > > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl > index d124359..8e49125 100755 > --- a/scripts/checkpatch.pl > +++ b/scripts/checkpatch.pl > @@ -1834,6 +1834,15 @@ sub pos_last_openparen { > return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; > } > > +sub count_openparen { > + my ($line) = @_; > + > + my $opens = $line =~ tr/\(/\(/; > + my $closes = $line =~ tr/\)/\)/; > + > + return $opens - $closes; > +} > + > sub process { > my $filename = shift; > > @@ -2539,11 +2548,16 @@ sub process { > " " x ($pos % 8); > my $goodspaceindent = $oldindent . " " x $pos; > > + my $goodtwotabindent = $oldindent . > + "\t\t" x count_openparen($rest); > + > if ($newindent ne $goodtabindent && > - $newindent ne $goodspaceindent) { > + $newindent ne $goodspaceindent && > + $newindent ne $goodtwotabindent) { > > if (CHK("PARENTHESIS_ALIGNMENT", > - "Alignment should match open parenthesis\n" . $hereprev) && > + "Alignment should match open parenthesis, " . > + "or be twice indented for each open parenthesis\n" . $hereprev) && > $fix && $line =~ /^\+/) { > $fixed[$fixlinenr] =~ > s/^\+[ \t]*/\+$goodtabindent/; -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/