7976 lines · 239.6 KB · Perl
RawDownload
			if (WARN("SPACE_BEFORE_TAB",				"please, no space before tabs\n" . $herevet) &&			    $fix) {				while ($fixed[$fixlinenr] =~					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}				while ($fixed[$fixlinenr] =~					   s/(^\+.*) +\t/$1\t/) {}			}		} # check for assignments on the start of a line		if ($sline =~ /^\+\s+($Assignment)[^=]/) {			my $operator = $1;			if (CHK("ASSIGNMENT_CONTINUATIONS",				"Assignment operator '$1' should be on the previous line\n" . $hereprev) &&			    $fix && $prevrawline =~ /^\+/) {				# add assignment operator to the previous line, remove from current line				$fixed[$fixlinenr - 1] .= " $operator";				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;			}		} # check for && or || at the start of a line		if ($rawline =~ /^\+\s*(&&|\|\|)/) {			my $operator = $1;			if (CHK("LOGICAL_CONTINUATIONS",				"Logical continuations should be on the previous line\n" . $hereprev) &&			    $fix && $prevrawline =~ /^\+/) {				# insert logical operator at last non-comment, non-whitepsace char on previous line				$prevline =~ /[\s$;]*$/;				my $line_end = substr($prevrawline, $-[0]);				$fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;			}		} # check indentation starts on a tab stop		if ($perl_version_ok &&		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {			my $indent = length($1);			if ($indent % $tabsize) {				if (WARN("TABSTOP",					 "Statements should start on a tabstop\n" . $herecurr) &&				    $fix) {					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;				}			}		} # check multi-line statement indentation matches previous line		if ($perl_version_ok &&		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {			$prevline =~ /^\+(\t*)(.*)$/;			my $oldindent = $1;			my $rest = $2; 			my $pos = pos_last_openparen($rest);			if ($pos >= 0) {				$line =~ /^(\+| )([ \t]*)/;				my $newindent = $2; 				my $goodtabindent = $oldindent .					"\t" x ($pos / $tabsize) .					" "  x ($pos % $tabsize);				my $goodspaceindent = $oldindent . " "  x $pos; 				if ($newindent ne $goodtabindent &&				    $newindent ne $goodspaceindent) { 					if (CHK("PARENTHESIS_ALIGNMENT",						"Alignment should match open parenthesis\n" . $hereprev) &&					    $fix && $line =~ /^\+/) {						$fixed[$fixlinenr] =~						    s/^\+[ \t]*/\+$goodtabindent/;					}				}			}		} # check for space after cast like "(int) foo" or "(struct foo) bar"# avoid checking a few false positives:#   "sizeof(<type>)" or "__alignof__(<type>)"#   function pointer declarations like "(*foo)(int) = bar;"#   structure definitions like "(struct foo) { 0 };"#   multiline macros that define functions#   known attributes or the __attribute__ keyword		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {			if (CHK("SPACING",				"No space is necessary after a cast\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~				    s/(\(\s*$Type\s*\))[ \t]+/$1/;			}		} # Block comments use * on subsequent lines		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */		    $rawline =~ /^\+/ &&			#line is new		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *			WARN("BLOCK_COMMENT_STYLE",			     "Block comments use * on subsequent lines\n" . $hereprev);		} # Block comments use */ on trailing lines		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */			WARN("BLOCK_COMMENT_STYLE",			     "Block comments use a trailing */ on a separate line\n" . $herecurr);		} # Block comment * alignment		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment		    $line =~ /^\+[ \t]*$;/ &&			#leading comment		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *			my $oldindent;			$prevrawline =~ m@^\+([ \t]*/?)\*@;			if (defined($1)) {				$oldindent = expand_tabs($1);			} else {				$prevrawline =~ m@^\+(.*/?)\*@;				$oldindent = expand_tabs($1);			}			$rawline =~ m@^\+([ \t]*)\*@;			my $newindent = $1;			$newindent = expand_tabs($newindent);			if (length($oldindent) ne length($newindent)) {				WARN("BLOCK_COMMENT_STYLE",				     "Block comments should align the * on each line\n" . $hereprev);			}		} # check for missing blank lines after struct/union declarations# with exceptions for various attributes and macros		if ($prevline =~ /^[\+ ]};?\s*$/ &&		    $line =~ /^\+/ &&		    !($line =~ /^\+\s*$/ ||		      $line =~ /^\+\s*(?:EXPORT_SYMBOL|early_param|ALLOW_ERROR_INJECTION)/ ||		      $line =~ /^\+\s*MODULE_/i ||		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||		      $line =~ /^\+[a-z_]*init/ ||		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||		      $line =~ /^\+\s*DECLARE/ ||		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||		      $line =~ /^\+\s*__setup/)) {			if (CHK("LINE_SPACING",				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&			    $fix) {				fix_insert_line($fixlinenr, "\+");			}		} # check for multiple consecutive blank lines		if ($prevline =~ /^[\+ ]\s*$/ &&		    $line =~ /^\+\s*$/ &&		    $last_blank_line != ($linenr - 1)) {			if (CHK("LINE_SPACING",				"Please don't use multiple blank lines\n" . $hereprev) &&			    $fix) {				fix_delete_line($fixlinenr, $rawline);			} 			$last_blank_line = $linenr;		} # check for missing blank lines after declarations# (declarations must have the same indentation and not be at the start of line)		if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {			# use temporaries			my $sl = $sline;			my $pl = $prevline;			# remove $Attribute/$Sparse uses to simplify comparisons			$sl =~ s/\b(?:$Attribute|$Sparse)\b//g;			$pl =~ s/\b(?:$Attribute|$Sparse)\b//g;			if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||			# function pointer declarations			     $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident(?:\s*\[\s*(?:$Ident|$Constant)?\s*\])?\s*\)\s*[=,;:\[\(]/ ||			# foo bar; where foo is some local typedef or #define			     $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||			# known declaration macros			     $pl =~ /^\+\s+$declaration_macros/) &&			# for "else if" which can look like "$Ident $Ident"			    !($pl =~ /^\+\s+$c90_Keywords\b/ ||			# other possible extensions of declaration lines			      $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||			# not starting a section or a macro "\" extended line			      $pl =~ /(?:\{\s*|\\)$/) &&			# looks like a declaration			    !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||			# function pointer declarations			      $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident(?:\s*\[\s*(?:$Ident|$Constant)?\s*\])?\s*\)\s*[=,;:\[\(]/ ||			# foo bar; where foo is some local typedef or #define			      $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||			# known declaration macros			      $sl =~ /^\+\s+$declaration_macros/ ||			# start of struct or union or enum			      $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||			# start or end of block or continuation of declaration			      $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||			# bitfield continuation			      $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||			# other possible extensions of declaration lines			      $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {				if (WARN("LINE_SPACING",					 "Missing a blank line after declarations\n" . $hereprev) &&				    $fix) {					fix_insert_line($fixlinenr, "\+");				}			}		} # check for spaces at the beginning of a line.# Exceptions:#  1) within comments#  2) indented preprocessor commands#  3) hanging labels		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {			my $herevet = "$here\n" . cat_vet($rawline) . "\n";			if (WARN("LEADING_SPACE",				 "please, no spaces at the start of a line\n" . $herevet) &&			    $fix) {				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;			}		} # check we are in a valid C source file if not then ignore this hunk		next if ($realfile !~ /\.(h|c)$/); # check for unusual line ending [ or (		if ($line =~ /^\+.*([\[\(])\s*$/) {			CHK("OPEN_ENDED_LINE",			    "Lines should not end with a '$1'\n" . $herecurr);		} # check if this appears to be the start function declaration, save the name		if ($sline =~ /^\+\{\s*$/ &&		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {			$context_function = $1;		} # check if this appears to be the end of function declaration		if ($sline =~ /^\+\}\s*$/) {			undef $context_function;		} # check indentation of any line with a bare else# (but not if it is a multiple line "if (foo) return bar; else return baz;")# if the previous line is a break or return and is indented 1 tab more...		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {			my $tabs = length($1) + 1;			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&			     defined $lines[$linenr] &&			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {				WARN("UNNECESSARY_ELSE",				     "else is not generally useful after a break or return\n" . $hereprev);			}		} # check indentation of a line with a break;# if the previous line is a goto, return or break# and is indented the same # of tabs		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {			my $tabs = $1;			if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {				if (WARN("UNNECESSARY_BREAK",					 "break is not useful after a $1\n" . $hereprev) &&				    $fix) {					fix_delete_line($fixlinenr, $rawline);				}			}		} # check for RCS/CVS revision markers		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {			WARN("CVS_KEYWORD",			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);		} # check for old HOTPLUG __dev<foo> section markings		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {			WARN("HOTPLUG_SECTION",			     "Using $1 is unnecessary\n" . $herecurr);		} # Check for potential 'bare' types		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,		    $realline_next);#print "LINE<$line>\n";		if ($linenr > $suppress_statement &&		    $realcnt && $sline =~ /.\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; #print "linenr<$linenr> <$stat>\n";			# If this statement has no statement boundaries within			# it there is no point in retrying a statement scan			# until we hit end of it.			my $frag = $stat; $frag =~ s/;+\s*$//;			if ($frag !~ /(?:{|;)/) {#print "skip<$line_nr_next>\n";				$suppress_statement = $line_nr_next;			} 			# Find the real next line.			$realline_next = $line_nr_next;			if (defined $realline_next &&			    (!defined $lines[$realline_next - 1] ||			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {				$realline_next++;			} 			my $s = $stat;			$s =~ s/{.*$//s; 			# Ignore goto labels.			if ($s =~ /$Ident:\*$/s) { 			# Ignore functions being called			} elsif ($s =~ /^.\s*$Ident\s*\(/s) { 			} elsif ($s =~ /^.\s*else\b/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_CASE_INDENT_LEVEL",				      "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|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {			my $pre_ctx = "$1$2"; 			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 			if ($line =~ /^\+\t{6,}/) {				WARN("DEEP_INDENTATION",				     "Too many leading tabs - consider code refactoring\n" . $herecurr);			} 			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] !~ /^-/);				$ctx_ln++;			} 			#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("OPEN_BRACE",				      "that open brace { should be on the previous line\n" .					"$here\n$ctx\n$rawlines[$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",					     "trailing semicolon indicates no statements, indent implies otherwise\n" .						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");				}			}		} # Check relative indent for conditionals and blocks.		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {			($stat, $cond, $line_nr_next, $remain_next, $off_next) =				ctx_statement_block($linenr, $realcnt, 0)					if (!defined $stat);			my ($s, $c) = ($stat, $cond); 			substr($s, 0, length($c), ''); 			# remove inline comments			$s =~ s/$;/ /g;			$c =~ s/$;/ /g; 			# Find out how long the conditional actually is.			my @newlines = ($c =~ /\n/gs);			my $cond_lines = 1 + $#newlines; 			# 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;			while ($s =~ /\n\s+\\\n/) {				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;			} 			# 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;			}			if ($s =~ s/^\s*?\n//) {				$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;			} 			my $cond_ptr = -1;			$continuation = 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;

Lines 4001–4500 of 7976. Previous lines