Skip to content
Snippets Groups Projects
checkpatch.pl 66 KiB
Newer Older
  • Learn to ignore specific revisions
  • 	# See if any suffix of this path is a path within the tree.
    	while ($file =~ s@^[^/]*/@@) {
    		if (-f "$root/$file") {
    			##print "file<$file>\n";
    			last;
    		}
    	}
    	if (! -f _)  {
    		return 0;
    	}
    
    	# It is, so see if the prefix is acceptable.
    	my $prefix = $absolute;
    	substr($prefix, -length($file)) = '';
    
    	##print "prefix<$prefix>\n";
    	if ($prefix ne ".../") {
    		WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
    	}
    }
    
    
    sub process {
    	my $filename = shift;
    
    	my $linenr=0;
    	my $prevline="";
    
    	my $prevrawline="";
    
    	my $stashline="";
    
    	my $stashrawline="";
    
    	my $indent;
    	my $previndent=0;
    	my $stashindent=0;
    
    
    	our $clean = 1;
    
    	my $signoff = 0;
    	my $is_patch = 0;
    
    
    	our @report = ();
    
    	our $cnt_lines = 0;
    	our $cnt_error = 0;
    	our $cnt_warn = 0;
    	our $cnt_chk = 0;
    
    
    	# Trace the real file/line as we go.
    	my $realfile = '';
    	my $realline = 0;
    	my $realcnt = 0;
    	my $here = '';
    	my $in_comment = 0;
    
    	my $comment_edge = 0;
    
    	my $first_line = 0;
    
    
    	my $prev_values = 'E';
    
    	# suppression flags
    
    	my %suppress_ifbraces;
    
    	# Pre-scan the patch sanitizing the lines.
    
    	# Pre-scan the patch looking for any __setup documentation.
    
    	my @setup_docs = ();
    	my $setup_docs = 0;
    
    
    	sanitise_line_reset();
    
    	my $line;
    	foreach my $rawline (@rawlines) {
    
    		$linenr++;
    		$line = $rawline;
    
    		if ($rawline=~/^\+\+\+\s+(\S+)/) {
    
    			$setup_docs = 0;
    			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
    				$setup_docs = 1;
    			}
    
    			#next;
    		}
    		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
    			$realline=$1-1;
    			if (defined $2) {
    				$realcnt=$3+1;
    			} else {
    				$realcnt=1+1;
    			}
    
    			$in_comment = 0;
    
    
    			# Guestimate if this is a continuing comment.  Run
    			# the context looking for a comment "edge".  If this
    			# edge is a close comment then we must be in a comment
    			# at context start.
    			my $edge;
    
    			my $cnt = $realcnt;
    			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
    				next if (defined $rawlines[$ln - 1] &&
    					 $rawlines[$ln - 1] =~ /^-/);
    				$cnt--;
    				#print "RAW<$rawlines[$ln - 1]>\n";
    				($edge) = (defined $rawlines[$ln - 1] &&
    					$rawlines[$ln - 1] =~ m@(/\*|\*/)@);
    
    				last if (defined $edge);
    			}
    			if (defined $edge && $edge eq '*/') {
    				$in_comment = 1;
    			}
    
    			# Guestimate if this is a continuing comment.  If this
    			# is the start of a diff block and this line starts
    			# ' *' then it is very likely a comment.
    			if (!defined $edge &&
    			    $rawlines[$linenr] =~ m@^.\s* \*(?:\s|$)@)
    			{
    				$in_comment = 1;
    			}
    
    			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
    			sanitise_line_reset($in_comment);
    
    
    		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
    
    			# Standardise the strings and chars within the input to
    
    			# simplify matching -- only bother with positive lines.
    
    			$line = sanitise_line($rawline);
    
    		push(@lines, $line);
    
    		if ($realcnt > 1) {
    			$realcnt-- if ($line =~ /^(?:\+| |$)/);
    		} else {
    			$realcnt = 0;
    		}
    
    		#print "==>$rawline\n";
    		#print "-->$line\n";
    
    
    		if ($setup_docs && $line =~ /^\+/) {
    			push(@setup_docs, $line);
    		}
    	}
    
    
    	$realcnt = 0;
    	$linenr = 0;
    
    	foreach my $line (@lines) {
    		$linenr++;
    
    
    		my $rawline = $rawlines[$linenr - 1];
    
    		my $hunk_line = ($realcnt != 0);
    
    #extract the line range in the file after the patch is applied
    
    		if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
    
    			$is_patch = 1;
    
    			$first_line = $linenr + 1;
    
    			$realline=$1-1;
    			if (defined $2) {
    				$realcnt=$3+1;
    			} else {
    				$realcnt=1+1;
    			}
    
    			annotate_reset();
    
    			$prev_values = 'E';
    
    
    			%suppress_ifbraces = ();
    
    			%suppress_whiletrailers = ();
    
    # track the line number as we move through the hunk, note that
    # new versions of GNU diff omit the leading space on completely
    # blank context lines so we need to count that too.
    
    		} elsif ($line =~ /^( |\+|$)/) {
    
    			$realline++;
    
    			$realcnt-- if ($realcnt != 0);
    
    			# Measure the line length and indent.
    
    			($length, $indent) = line_stats($rawline);
    
    
    			# Track the previous line.
    			($prevline, $stashline) = ($stashline, $line);
    			($previndent, $stashindent) = ($stashindent, $indent);
    
    			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
    
    
    			#warn "line<$line>\n";
    
    		} elsif ($realcnt == 1) {
    			$realcnt--;
    
    		}
    
    #make up the handle for any error we report on this line
    
    		$prefix = "$filename:$realline: " if ($emacs && $file);
    		$prefix = "$filename:$linenr: " if ($emacs && !$file);
    
    
    		$here = "#$linenr: " if (!$file);
    		$here = "#$realline: " if ($file);
    
    
    		# extract the filename as it passes
    		if ($line=~/^\+\+\+\s+(\S+)/) {
    			$realfile = $1;
    			$realfile =~ s@^[^/]*/@@;
    
    
    			if ($realfile =~ m@^include/asm/@) {
    
    				ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
    			}
    			next;
    		}
    
    
    		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
    
    		my $hereline = "$here\n$rawline\n";
    		my $herecurr = "$here\n$rawline\n";
    		my $hereprev = "$here\n$prevrawline\n$rawline\n";
    
    		$cnt_lines++ if ($realcnt != 0);
    
    
    #check the patch for a signoff:
    
    		if ($line =~ /^\s*signed-off-by:/i) {
    
    			# This is a signoff, if ugly, so do not double report.
    			$signoff++;
    
    			if (!($line =~ /^\s*Signed-off-by:/)) {
    
    				WARN("Signed-off-by: is the preferred form\n" .
    					$herecurr);
    
    			}
    			if ($line =~ /^\s*signed-off-by:\S/i) {
    
    				WARN("space required after Signed-off-by:\n" .
    
    # Check for wrappage within a valid hunk of the file
    
    		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
    
    			ERROR("patch seems to be corrupt (line wrapped?)\n" .
    
    				$herecurr) if (!$emitted_corrupt++);
    
    # Check for absolute kernel paths.
    		if ($tree) {
    			while ($line =~ m{(?:^|\s)(/\S*)}g) {
    				my $file = $1;
    
    				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
    				    check_absolute_file($1, $herecurr)) {
    					#
    				} else {
    					check_absolute_file($file, $herecurr);
    				}
    			}
    		}
    
    
    # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
    		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
    
    		    $rawline !~ m/^$UTF8*$/) {
    			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
    
    			my $blank = copy_spacing($rawline);
    			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
    			my $hereptr = "$hereline$ptr\n";
    
    			ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
    
    # ignore non-hunk lines and lines being removed
    		next if (!$hunk_line || $line =~ /^-/);
    
    
    #trailing whitespace
    
    		if ($line =~ /^\+.*\015/) {
    
    			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
    
    			ERROR("DOS line endings\n" . $herevet);
    
    
    		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
    			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
    
    			ERROR("trailing whitespace\n" . $herevet);
    
    
    # check we are in a valid source file if not then ignore this hunk
    		next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
    
    
    #80 column limit
    
    		if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
    
    		    $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
    		    $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
    		    $length > 80)
    
    			WARN("line over 80 characters\n" . $herecurr);
    
    # check for adding lines without a newline.
    		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
    			WARN("adding a line without newline at end of file\n" . $herecurr);
    		}
    
    
    # check we are in a valid source file C or perl if not then ignore this hunk
    		next if ($realfile !~ /\.(h|c|pl)$/);
    
    
    # at the beginning of a line any tabs must come first and anything
    # more than 8 must use tabs.
    
    		if ($rawline =~ /^\+\s* \t\s*\S/ ||
    		    $rawline =~ /^\+\s*        \s*/) {
    			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
    
    			ERROR("code indent should use tabs where possible\n" . $herevet);
    
    # check we are in a valid C source file if not then ignore this hunk
    		next if ($realfile !~ /\.(h|c)$/);
    
    
    # check for RCS/CVS revision markers
    
    		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
    
    			WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
    		}
    
    # Check for potential 'bare' types
    
    		my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
    
    		if ($realcnt && $line =~ /.\s*\S/) {
    
    			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
    
    				ctx_statement_block($linenr, $realcnt, 0);
    
    			$stat =~ s/\n./\n /g;
    			$cond =~ s/\n./\n /g;
    
    			my $s = $stat;
    			$s =~ s/{.*$//s;
    
    			# Ignore goto labels.
    
    			if ($s =~ /$Ident:\*$/s) {
    
    
    			# Ignore functions being called
    
    			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
    
    			# declarations always start with types
    
    			} elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
    
    				my $type = $1;
    				$type =~ s/\s+/ /g;
    				possible($type, "A:" . $s);
    
    
    			# definitions in global scope can only start with types
    
    			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
    
    				possible($1, "B:" . $s);
    
    
    			# any (foo ... *) is a pointer cast, and foo is a type
    
    			while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/sg) {
    
    				possible($1, "C:" . $s);
    
    			}
    
    			# Check for any sort of function declaration.
    			# int foo(something bar, other baz);
    			# void (*store_gdt)(x86_descr_ptr *);
    
    			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
    
    				my ($name_len) = length($1);
    
    
    				my $ctx = $s;
    
    				substr($ctx, 0, $name_len + 1, '');
    
    				$ctx =~ s/\)[^\)]*$//;
    
    				for my $arg (split(/\s*,\s*/, $ctx)) {
    
    					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
    
    						possible($1, "D:" . $s);
    
    #
    # Checks which may be anchored in the context.
    #
    
    # Check for switch () and associated case and default
    # statements should be at the same indent.
    
    		if ($line=~/\bswitch\s*\(.*\)/) {
    			my $err = '';
    			my $sep = '';
    			my @ctx = ctx_block_outer($linenr, $realcnt);
    			shift(@ctx);
    			for my $ctx (@ctx) {
    				my ($clen, $cindent) = line_stats($ctx);
    				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
    							$indent != $cindent) {
    					$err .= "$sep$ctx\n";
    					$sep = '';
    				} else {
    					$sep = "[...]\n";
    				}
    			}
    			if ($err ne '') {
    
    				ERROR("switch and case should be at the same indent\n$hereline$err");
    
    			}
    		}
    
    # if/while/etc brace do not go on next line, unless defining a do while loop,
    # or if that brace on the next line is for something else
    
    		if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
    
    			my $pre_ctx = "$1$2";
    
    
    			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
    
    			my $ctx_cnt = $realcnt - $#ctx - 1;
    			my $ctx = join("\n", @ctx);
    
    
    			my $ctx_ln = $linenr;
    			my $ctx_skip = $realcnt;
    
    			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
    					defined $lines[$ctx_ln - 1] &&
    					$lines[$ctx_ln - 1] =~ /^-/)) {
    				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
    				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
    
    			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
    			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
    
    			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
    				ERROR("that open brace { should be on the previous line\n" .
    
    					"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
    
    			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
    			    $ctx =~ /\)\s*\;\s*$/ &&
    			    defined $lines[$ctx_ln - 1])
    			{
    
    				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
    				if ($nindent > $indent) {
    
    					WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
    
    						"$here\n$ctx\n$lines[$ctx_ln - 1]\n");
    
    # Check relative indent for conditionals and blocks.
    		if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
    			my ($s, $c) = ($stat, $cond);
    
    			substr($s, 0, length($c), '');
    
    			# Make sure we remove the line prefixes as we have
    			# none on the first line, and are going to readd them
    			# where necessary.
    			$s =~ s/\n./\n/gs;
    
    			# Find out how long the conditional actually is.
    
    			my @newlines = ($c =~ /\n/gs);
    			my $cond_lines = 1 + $#newlines;
    
    
    			# We want to check the first line inside the block
    			# starting at the end of the conditional, so remove:
    			#  1) any blank line termination
    			#  2) any opening brace { on end of the line
    			#  3) any do (...) {
    			my $continuation = 0;
    			my $check = 0;
    			$s =~ s/^.*\bdo\b//;
    			$s =~ s/^\s*{//;
    			if ($s =~ s/^\s*\\//) {
    				$continuation = 1;
    			}
    
    				$check = 1;
    				$cond_lines++;
    			}
    
    			# Also ignore a loop construct at the end of a
    			# preprocessor statement.
    			if (($prevline =~ /^.\s*#\s*define\s/ ||
    			    $prevline =~ /\\\s*$/) && $continuation == 0) {
    				$check = 0;
    			}
    
    
    			while ($cond_ptr != $cond_lines) {
    				$cond_ptr = $cond_lines;
    
    
    				# If we see an #else/#elif then the code
    				# is not linear.
    				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
    					$check = 0;
    				}
    
    
    				# Ignore:
    				#  1) blank lines, they should be at 0,
    				#  2) preprocessor lines, and
    				#  3) labels.
    
    				if ($continuation ||
    				    $s =~ /^\s*?\n/ ||
    
    				    $s =~ /^\s*#\s*?/ ||
    				    $s =~ /^\s*$Ident\s*:/) {
    
    					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
    
    			}
    
    			my (undef, $sindent) = line_stats("+" . $s);
    			my $stat_real = raw_line($linenr, $cond_lines);
    
    			# Check if either of these lines are modified, else
    			# this is not this patch's fault.
    			if (!defined($stat_real) ||
    			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
    				$check = 0;
    			}
    			if (defined($stat_real) && $cond_lines > 1) {
    				$stat_real = "[...]\n$stat_real";
    			}
    
    
    			#print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
    
    
    			if ($check && (($sindent % 8) != 0 ||
    			    ($sindent <= $indent && $s ne ''))) {
    				WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
    			}
    		}
    
    
    		# Track the 'values' across context and added lines.
    		my $opline = $line; $opline =~ s/^./ /;
    
    		my ($curr_values, $curr_vars) =
    				annotate_values($opline . "\n", $prev_values);
    
    		$curr_values = $prev_values . $curr_values;
    
    		if ($dbg_values) {
    			my $outline = $opline; $outline =~ s/\t/ /g;
    
    			print "$linenr > .$outline\n";
    			print "$linenr > $curr_values\n";
    
    			print "$linenr >  $curr_vars\n";
    
    		$prev_values = substr($curr_values, -1);
    
    
    #ignore lines not being added
    		if ($line=~/^[^\+]/) {next;}
    
    
    # TEST: allow direct testing of the type matcher.
    
    		if ($dbg_type) {
    			if ($line =~ /^.\s*$Declare\s*$/) {
    				ERROR("TEST: is type\n" . $herecurr);
    			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
    				ERROR("TEST: is not type ($1 is)\n". $herecurr);
    			}
    
    # TEST: allow direct testing of the attribute matcher.
    		if ($dbg_attr) {
    			if ($line =~ /^.\s*$Attribute\s*$/) {
    				ERROR("TEST: is attr\n" . $herecurr);
    			} elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
    				ERROR("TEST: is not attr ($1 is)\n". $herecurr);
    			}
    			next;
    		}
    
    # check for initialisation to aggregates open brace on the next line
    		if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
    		    $line =~ /^.\s*{/) {
    
    			ERROR("that open brace { should be on the previous line\n" . $hereprev);
    
    #
    # Checks which are anchored on the added line.
    #
    
    # check for malformed paths in #include statements (uses RAW line)
    
    		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
    
    			my $path = $1;
    			if ($path =~ m{//}) {
    
    				ERROR("malformed #include filename\n" .
    					$herecurr);
    
    # no C99 // comments
    
    		if ($line =~ m{//}) {
    
    			ERROR("do not use C99 // comments\n" . $herecurr);
    
    		# Remove C99 comments.
    
    		$line =~ s@//.*@@;
    
    		$opline =~ s@//.*@@;
    
    
    #EXPORT_SYMBOL should immediately follow its function closing }.
    
    		if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
    		    ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
    			my $name = $1;
    
    			if ($prevline !~ /(?:
    				^.}|
    				^.DEFINE_$Ident\(\Q$name\E\)|
    				^.DECLARE_$Ident\(\Q$name\E\)|
    				^.LIST_HEAD\(\Q$name\E\)|
    				^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
    				\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
    			    )/x) {
    
    				WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
    
    # check for external initialisers.
    
    		if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
    
    			ERROR("do not initialise externals to 0 or NULL\n" .
    				$herecurr);
    		}
    
    # check for static initialisers.
    
    		if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
    
    			ERROR("do not initialise statics to 0 or NULL\n" .
    				$herecurr);
    
    # check for new typedefs, only function parameters and sparse annotations
    # make sense.
    		if ($line =~ /\btypedef\s/ &&
    
    		    $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
    
    		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
    
    		    $line !~ /\b$typeTypedefs\b/ &&
    
    		    $line !~ /\b__bitwise(?:__|)\b/) {
    
    			WARN("do not add new typedefs\n" . $herecurr);
    
    		}
    
    # * goes on variable not on type
    
    		if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
    
    			ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
    				$herecurr);
    
    
    		} elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
    
    			ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
    				$herecurr);
    
    		} elsif ($line =~ m{\b$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
    
    			ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
    				$herecurr);
    
    		} elsif ($line =~ m{\b$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
    
    			ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
    				$herecurr);
    
    		}
    
    # # no BUG() or BUG_ON()
    # 		if ($line =~ /\b(BUG|BUG_ON)\b/) {
    # 			print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
    # 			print "$herecurr";
    # 			$clean = 0;
    # 		}
    
    
    		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
    
    			WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
    
    # printk should use KERN_* levels.  Note that follow on printk's on the
    # same line do not need a level, so we use the current block context
    # to try and find and validate the current printk.  In summary the current
    # printk includes all preceeding printk's which have no newline on the end.
    # we assume the first bad printk is the one to report.
    
    		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
    
    			my $ok = 0;
    			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
    				#print "CHECK<$lines[$ln - 1]\n";
    				# we have a preceeding printk if it ends
    				# with "\n" ignore it, else it is to blame
    				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
    					if ($rawlines[$ln - 1] !~ m{\\n"}) {
    						$ok = 1;
    					}
    					last;
    				}
    			}
    			if ($ok == 0) {
    
    				WARN("printk() should include KERN_ facility level\n" . $herecurr);
    
    # function brace can't be on same line, except for #defines of do while,
    # or if closed on same line
    
    		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
    		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
    
    			ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
    
    # open braces for enum, union and struct go on the same line.
    		if ($line =~ /^.\s*{/ &&
    		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
    			ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
    		}
    
    
    # check for spacing round square brackets; allowed:
    #  1. with a type on the left -- int [] a;
    
    #  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
    #  3. inside a curly brace -- = { [0...10] = 5 }
    
    		while ($line =~ /(.*?\s)\[/g) {
    			my ($where, $prefix) = ($-[1], $1);
    			if ($prefix !~ /$Type\s+$/ &&
    
    			    ($where != 0 || $prefix !~ /^.\s+$/) &&
    			    $prefix !~ /{\s+$/) {
    
    				ERROR("space prohibited before open square bracket '['\n" . $herecurr);
    			}
    		}
    
    
    # check for spaces between functions and their parentheses.
    
    		while ($line =~ /($Ident)\s+\(/g) {
    
    			my $name = $1;
    
    			my $ctx_before = substr($line, 0, $-[1]);
    			my $ctx = "$ctx_before$name";
    
    
    			# Ignore those directives where spaces _are_ permitted.
    
    			if ($name =~ /^(?:
    				if|for|while|switch|return|case|
    				volatile|__volatile__|
    				__attribute__|format|__extension__|
    				asm|__asm__)$/x)
    			{
    
    
    			# cpp #define statements have non-optional spaces, ie
    			# if there is a space between the name and the open
    			# parenthesis it is simply not a parameter group.
    
    			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
    
    
    			# cpp #elif statement condition may start with a (
    
    			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
    
    
    			# If this whole things ends with a type its most
    			# likely a typedef for a function.
    
    			} elsif ($ctx =~ /$Type$/) {
    
    				WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
    
    # Check operator spacing.
    
    		if (!($line=~/\#\s*include/)) {
    
    			my $ops = qr{
    				<<=|>>=|<=|>=|==|!=|
    				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
    				=>|->|<<|>>|<|>|=|!|~|
    
    				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
    				\?|:
    
    			my @elements = split(/($ops|;)/, $opline);
    
    			my $off = 0;
    
    
    			my $blank = copy_spacing($opline);
    
    
    			for (my $n = 0; $n < $#elements; $n += 2) {
    
    				$off += length($elements[$n]);
    
    
    				# Pick up the preceeding and succeeding characters.
    				my $ca = substr($opline, 0, $off);
    				my $cc = '';
    				if (length($opline) >= ($off + length($elements[$n + 1]))) {
    					$cc = substr($opline, $off + length($elements[$n + 1]));
    				}
    				my $cb = "$ca$;$cc";
    
    
    				my $a = '';
    				$a = 'V' if ($elements[$n] ne '');
    				$a = 'W' if ($elements[$n] =~ /\s$/);
    
    				$a = 'C' if ($elements[$n] =~ /$;$/);
    
    				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
    				$a = 'O' if ($elements[$n] eq '');
    
    				$a = 'E' if ($ca =~ /^\s*$/);
    
    				my $op = $elements[$n + 1];
    
    				if (defined $elements[$n + 2]) {
    
    					$c = 'V' if ($elements[$n + 2] ne '');
    					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
    
    					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
    
    					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
    					$c = 'O' if ($elements[$n + 2] eq '');
    
    					$c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
    
    				} else {
    					$c = 'E';
    
    				my $ctx = "${a}x${c}";
    
    				my $at = "(ctx:$ctx)";
    
    
    				my $ptr = substr($blank, 0, $off) . "^";
    
    				my $hereptr = "$hereline$ptr\n";
    
    				my $op_type = substr($curr_values, $off + 1, 1);
    
    				# Get the full operator variant.
    				my $opv = $op . substr($curr_vars, $off, 1);
    
    
    				# Ignore operators passed as parameters.
    				if ($op_type ne 'V' &&
    				    $ca =~ /\s$/ && $cc =~ /^\s*,/) {
    
    
    #				# Ignore comments
    #				} elsif ($op =~ /^$;+$/) {
    
    				# ; should have either the end of line or a space or \ after it
    
    				} elsif ($op eq ';') {
    
    					if ($ctx !~ /.x[WEBC]/ &&
    					    $cc !~ /^\\/ && $cc !~ /^;/) {
    
    						ERROR("space required after that '$op' $at\n" . $hereptr);
    
    					}
    
    				# // is a comment
    				} elsif ($op eq '//') {
    
    				# No spaces for:
    				#   ->
    				#   :   when part of a bitfield
    				} elsif ($op eq '->' || $opv eq ':B') {
    
    					if ($ctx =~ /Wx.|.xW/) {
    
    						ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
    
    					}
    
    				# , must have a space on the right.
    				} elsif ($op eq ',') {
    
    					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
    
    						ERROR("space required after that '$op' $at\n" . $hereptr);
    
    				# '*' as part of a type definition -- reported already.
    
    					#warn "'*' is part of type\n";
    
    				# unary operators should have a space before and
    				# none after.  May be left adjacent to another
    				# unary operator, or a cast
    				} elsif ($op eq '!' || $op eq '~' ||
    
    					 $opv eq '&U' || $opv eq '&&U') {
    
    					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
    
    						ERROR("space required before that '$op' $at\n" . $hereptr);
    
    					if ($op eq '*' && $cc =~/\s*const\b/) {
    
    						# A unary '*' may be const
    
    					} elsif ($ctx =~ /.xW/) {
    
    						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
    
    					}
    
    				# unary ++ and unary -- are allowed no space on one side.
    				} elsif ($op eq '++' or $op eq '--') {
    
    					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
    						ERROR("space required one side of that '$op' $at\n" . $hereptr);
    					}
    					if ($ctx =~ /Wx[BE]/ ||
    					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
    						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
    
    					if ($ctx =~ /ExW/) {
    						ERROR("space prohibited after that '$op' $at\n" . $hereptr);
    
    				# << and >> may either have or not have spaces both sides
    
    				} elsif ($op eq '<<' or $op eq '>>' or
    					 $op eq '&' or $op eq '^' or $op eq '|' or
    					 $op eq '+' or $op eq '-' or
    
    					 $op eq '*' or $op eq '/' or
    					 $op eq '%')
    
    					if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
    
    						ERROR("need consistent spacing around '$op' $at\n" .
    							$hereptr);
    
    				# A colon needs no spaces before when it is
    				# terminating a case value or a label.
    				} elsif ($opv eq ':C' || $opv eq ':L') {
    					if ($ctx =~ /Wx./) {
    						ERROR("space prohibited before that '$op' $at\n" . $hereptr);
    					}
    
    
    				# All the others need spaces both sides.
    
    				} elsif ($ctx !~ /[EWC]x[CWE]/) {
    
    					# Ignore email addresses <foo@bar>
    
    					if (($op eq '<' &&
    					     $cc =~ /^\S+\@\S+>/) ||
    					    ($op eq '>' &&
    					     $ca =~ /<\S+\@\S+$/))
    					{
    					    	$ok = 1;
    					}
    
    					# Ignore ?:
    					if (($opv eq ':O' && $ca =~ /\?$/) ||
    					    ($op eq '?' && $cc =~ /^:/)) {
    					    	$ok = 1;
    					}
    
    					if ($ok == 0) {
    
    						ERROR("spaces required around that '$op' $at\n" . $hereptr);
    
    				$off += length($elements[$n + 1]);
    
    # check for multiple assignments
    		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
    
    			CHK("multiple assignments should be avoided\n" . $herecurr);
    
    ## # check for multiple declarations, allowing for a function declaration
    ## # continuation.
    ## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
    ## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
    ##
    ## 			# Remove any bracketed sections to ensure we do not
    ## 			# falsly report the parameters of functions.
    ## 			my $ln = $line;
    ## 			while ($ln =~ s/\([^\(\)]*\)//g) {
    ## 			}
    ## 			if ($ln =~ /,/) {
    ## 				WARN("declaring multiple variables together should be avoided\n" . $herecurr);
    ## 			}
    ## 		}
    
    #need space before brace following if, while, etc
    
    		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
    		    $line =~ /do{/) {
    
    			ERROR("space required before the open brace '{'\n" . $herecurr);
    
    		}
    
    # closing brace should have a space following it when it has anything
    # on the line
    		if ($line =~ /}(?!(?:,|;|\)))\S/) {
    
    			ERROR("space required after that close brace '}'\n" . $herecurr);
    
    # check spacing on square brackets
    		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
    
    			ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
    
    		}
    		if ($line =~ /\s\]/) {
    
    			ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
    
    # check spacing on parentheses
    
    		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
    		    $line !~ /for\s*\(\s+;/) {
    
    			ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
    
    		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
    
    		    $line !~ /for\s*\(.*;\s+\)/ &&
    		    $line !~ /:\s+\)/) {
    
    			ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
    
    #goto labels aren't indented, allow a single space however
    
    		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
    
    		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
    
    			WARN("labels should not be indented\n" . $herecurr);
    
    # Return is not a function.
    		if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
    			my $spacing = $1;
    			my $value = $2;
    
    			# Flatten any parentheses and braces
    
    			while ($value =~ s/\([^\(\)]*\)/1/) {
    			}
    
    			if ($value =~ /^(?:$Ident|-?$Constant)$/) {
    				ERROR("return is not a function, parentheses are not required\n" . $herecurr);
    
    			} elsif ($spacing !~ /\s+/) {
    				ERROR("space required before the open parenthesis '('\n" . $herecurr);
    			}
    		}
    
    
    # Need a space before open parenthesis after if, while etc
    
    		if ($line=~/\b(if|while|for|switch)\(/) {
    
    			ERROR("space required before the open parenthesis '('\n" . $herecurr);
    
    # Check for illegal assignment in if conditional -- and check for trailing
    # statements after the conditional.
    
    		if ($line =~ /do\s*(?!{)/) {
    			my ($stat_next) = ctx_statement_block($line_nr_next,
    						$remain_next, $off_next);
    			$stat_next =~ s/\n./\n /g;
    			##print "stat<$stat> stat_next<$stat_next>\n";
    
    			if ($stat_next =~ /^\s*while\b/) {
    				# If the statement carries leading newlines,
    				# then count those as offsets.
    				my ($whitespace) =
    					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
    				my $offset =
    					statement_rawlines($whitespace) - 1;
    
    				$suppress_whiletrailers{$line_nr_next +
    								$offset} = 1;
    			}
    		}
    		if (!defined $suppress_whiletrailers{$linenr} &&
    		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
    
    			my ($s, $c) = ($stat, $cond);
    
    
    			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
    
    				ERROR("do not use assignment in if condition\n" . $herecurr);
    
    			}
    
    			# Find out what is on the end of the line after the
    			# conditional.