From: Mark Williamson Subject: [RFC] Coding style and standardisation Date: Mon, 4 Jun 2007 06:03:44 +0100 Message-ID: <200706040603.45326.mark.williamson@cl.cam.ac.uk> Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_x05YG54RUX7G7Ug" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com Cc: Keir Fraser List-Id: xen-devel@lists.xenproject.org --Boundary-00=_x05YG54RUX7G7Ug Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi all, The coding style in the Xen tree is somewhat varied these days - there are a few deviations from the "standard" that have crept in over time. I've been trying to think of ways we can help contributors understand how the code is supposed to look in a canonical form, rather than people trying to judge from code already there. Attached is a little extra documentation - my quick stab at summing up the Xen coding style. I think the main points of difference from the Linux coding style are summarised here, but if anybody has anything else they think should be included (or anything they disagree with in my summary) it would be handy to know. I've also attached a modified-for-Xen version of a (the?) Linux patch style checker. It might not be 100% accurate, but it should give folks a pretty good idea of any obvious style changes they need to make. My hope is that this will reduce review burden (for reviewers and submitters) and eventually get the codebase back towards a uniform coding style. This is a component of my exploration of automated code quality checking for the Xen codebase. Comments, anyone? Cheers, Mark -- Dave: Just a question. What use is a unicyle with no seat? And no pedals! Mark: To answer a question with a question: What use is a skateboard? Dave: Skateboards have wheels. Mark: My wheel has a wheel! --Boundary-00=_x05YG54RUX7G7Ug Content-Type: all/allfiles; name="xenstyle.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="xenstyle.patch" # HG changeset patch # User mark.williamson@cl.cam.ac.uk # Date 1180932960 -3600 # Node ID 19dea8353434ba799f5b7e46e1d6ffdf2943a74c # Parent bd3d6b4c52ec809f080c89c4ffcf61dc6e445978 Basic Xen CodingStyle guide, plus automated patch style checker. Signed-off-by: Mark Williamson diff -r bd3d6b4c52ec -r 19dea8353434 docs/misc/CodingStyle --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/docs/misc/CodingStyle Mon Jun 04 05:56:00 2007 +0100 @@ -0,0 +1,77 @@ +Coding Style for the Xen Hypervisor +=================================== + +This coding style is used for C code that is part of the Xen +Hypervisor itself, or part of the associated user space tools. Guest +operating system code may use the native coding style of that +code base. The goal of this file is to document the main points of the +Xen coding style, particularly where they differ from normal Linux +convention. + +Indentation +----------- + +Indenting uses spaces, not tabs - in contrast to Linux. An indent +level consists of four spaces. Code within blocks is indented by one +extra indent level. The enclosing braces of a block are indented the +same as the code _outside_ the block. E.g. + +void fun(void) +{ + /* One level of indent. */ + + { + /* A second level of indent. */ + } +} + +White space +----------- + +Space characters are used to spread out logical statements, such as in +the condition of an if or while. Spaces are placed between the +keyword and the brackets surrounding the condition, between the +brackets and the condition itself, and surrounding brackets and +operators within the expression itself. E.g. + +if ( ( wibble & wombat ) == 42 ) +{ + ... + +Bracing +------- + +Braces ('{' and '}') are always placed on a line of their own. This +is unlike the Linux coding style and unlike K&R. E.g.: + +if ( condition ) +{ + /* Do stuff. */ +} + +while ( condition ) +{ + /* Do stuff. */ +} + +etc. + +Comments +-------- + +Only C style /* ... */ comments are to be used. C++ style // comments +should not be used in submitted code. Multi-word comments should +begin with a capital letter and end with a full stop. + +Style checking +-------------- + +Automated style checking for patches to the Xen code base is provided +by tools/misc/xenstylecheck.pl . This tool will attempt to spot +common style problems. If it complains at you, it doesn't necessarily +mean you need to change your code but you should understand why you're +not following the style. xenstylecheck.pl is derived closely from +patchstyleemailcheck.pl by Dave Jones and Joel Scohpp. Bug reports, +queries and suggestions regarding this derivative should be addressed +to Mark Williamson or +xen-devel@lists.xensource.com and not to the original authors. diff -r bd3d6b4c52ec -r 19dea8353434 tools/misc/xenstylecheck.pl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/misc/xenstylecheck.pl Mon Jun 04 05:56:00 2007 +0100 @@ -0,0 +1,215 @@ +#!/usr/bin/perl -w +# (c) 2001, Dave Jones. (the file handling bit) +# (c) 2005, Joel Scohpp (the ugly bit) +# (c) 2007, XenSource Inc, modifications by Mark Williamson +# + +# Xen-specific note: I've hacked around the fine code for purposes of +# style-checking Xen patches, rather than Linux. Much is the same, +# some is different. Credit for this handy tool should go to the +# original authors but any bug reports should be directed at me (Mark +# Williamson). +# +# Licensed under the terms of the GNU GPL License version 2 + +use strict; + +my @lines = (); +while (<>) { + chomp; + push(@lines, $_); + if (eof(ARGV)) { + process($ARGV, @lines); + @lines = (); + } +} + +sub process { + my $filename = shift; + my @lines = @_; + + my $linenr=0; + my $prevline=""; + + + my $clean = 1; + + # Trace the real file/line as we go. + my $realfile = ''; + my $realline = 0; + my $realcnt = 0; + my $here = ''; + + foreach my $line (@lines) { + + $linenr++; + +#extract the filename as it passes + if($line=~/^\+\+\+\s+(\S+)/) { + $realfile=$1; + next; + } +#extract the line range in the file after the patch is applied + if($line=~/^@@ -\d+,\d+ \+(\d+),(\d+) @@/) { + $realline=$1-1; + $realcnt=$2+1; + next; + } +#track the line number as we move thorugh the hunk + if($line=~/^( |\+)/) { + $realline++; + $realcnt-- if ($realcnt); + } +#ignore lines not being added + if($line=~/^[^\+]/) {next;} + +#make up the handle for any error we report on this line + $here = "PATCH: $ARGV:$linenr:"; + $here .= "\nFILE: $realfile:$realline:" if ($realcnt); + +# * goes on variable not on type + if($line=~/[A-Za-z\d_]+\* [A-Za-z\d_]+/) { + print ("\"foo* bar\" should be \"foo *bar\"\n"); + print "$linenr:\n$line\n\n"; + $clean = 0; + } + +#trailing whitespace + if($line=~/\S\s+$/) { + print ("trailing whitespace\n"); + print "$here\n$line\n\n"; + $clean = 0; + } +#80 column limit + my $lineforcounting; + + $lineforcounting = $line; + $lineforcounting =~ s/^\+//; + $lineforcounting =~ s/\t/ /; + if(length($lineforcounting) > 80){ + print "line over 80 characters\n"; + print "$here\n$line\n\n"; + $clean = 0; + } +#function brace can't be on same line, except for #defines of do while, or if closed on same line + if(($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and + !($line=~/\#define.*do.*while/) and !($line=~/}/)){ + print "braces following function declarations go on the next line\n"; + print "$here\n$line\n\n"; + $clean = 0; + } +#need spaces around = in assignments unless they are in quotes + if((($line=~/[A-Za-z\d_]+=/) or ($line=~/=[A-Za-z\d_]+/)) and !($line=~/\".*=.*\"/)) { + print ("need spaces around that =\n"); + print "$here\n$line\n\n"; + } + +#need space before brace following if, while, etc + if($line=~/if\s*\(.*\)/) { + if(!($line=~/if \(/)) { + print "need one space before the (\n"; + print "$here\n$line\n\n"; + $clean = 0; + } + if(!($line=~/\( /)) { + print "need a space after the (\n"; + print "$here\n$line\n\n"; + $clean = 0; + } + if (!($line=~/ \)/)) { + print "need a space before the )\n"; + print "$here\n$line\n\n"; + $clean = 0; + } + } + +#spaces vs tabs + elsif($line=~/^\+\t+[A-Za-z\d_]/) { + print ("use spaces not tabs\n"); + print "$here\n$line\n\n"; + $clean = 0; + } +#studly caps, commented out until figure out how to distinguish between use of existing and adding new +# if(($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { +# print ("No studly caps, use _\n"); +# print "$here\n$line\n\n"; +# $clean = 0; +# } +#no spaces allowed after \ in define + if($line=~/\#define.*\\\s$/){ + print("Whitepspace after \\ makes next lines useless\n"); + print "$here\n$line\n\n"; + $clean = 0; + } + +#if/while/etc brace should go on next line + if ($prevline=~/(if|while|for)\s*\(/) { + my @opened = $prevline=~/\(/g; + my @closed = $prevline=~/\)/g; + my $nr_line = $linenr; + my $next_line = $line; + my $extra_lines = 0; + + while (scalar @opened > scalar @closed) { + $prevline .= $next_line; + $next_line = $lines[$nr_line]; + $nr_line++; + #This test is here to take into account the contextual lines + # i.e. lines not added to the file + # Normally there are 3 contextual lines before/after the modified lines + if (!$next_line=~/^\+/) { + if ($extra_lines < 3) { + $extra_lines++; + } else { + last; + } + } + + @opened = $prevline=~/\(/g; + @closed = $prevline=~/\)/g; + } + + if($prevline=~/{/) { + print "test: $lines[$linenr-1]\n:"; + print "\'$prevline\' "; + print "That { should be on the next line\n"; + print "$here\n$line\n\n"; + $clean = 0 + } + } + +#multiline macros should be enclosed in a do while loop + if(($prevline=~/\#define.*\\/) and ($line=~/\\/) and !($line=~/do.*{/)){ + print "Macros with multiple statements should be enclosed in a do - while loop\n"; + print "$here\n$line\n\n"; + $clean = 0; + } + + +#assignment in an if or while statement not soo good + if($line=~/(if|while)\(.*\s=\s.*\)/){ + print "Assignment inside of if statements is generally frowned upon\n"; + print "$here\n$line\n\n"; + $clean = 0; + } + + +#prevline + $prevline = $line; + + } + + + if($clean == 1){ + print "Your patch is now worthy to be reviewed by a real person\n"; + print "This small program couldn't find anything wrong\n"; + print "Not to say there isn't anything wrong, but you are off to a good start\n"; + print "If you find any style errors that this program missed please send them to \n"; + print "Mark Williamson so he can try to incorporate them\n" + } + if($clean == 0){ + print "If any of these errors are false positives please send them to\n"; + print "Mark Williamson so he can fix things."; + } + return 1; +} --Boundary-00=_x05YG54RUX7G7Ug Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --Boundary-00=_x05YG54RUX7G7Ug--