7586 lines · 225.6 KB · Perl
RawDownload
		} # 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;				} 				# 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;					if ($s =~ s/^.*?\n//) {						$cond_lines++;					}				}			} 			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 && $s ne '' &&			    (($sindent % $tabsize) != 0 ||			     ($sindent < $indent) ||			     ($sindent == $indent &&			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||			     ($sindent > $indent + $tabsize))) {				WARN("SUSPECT_CODE_INDENT",				     "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		next if ($line =~ /^[^\+]/); # check for self assignments used to avoid compiler warnings# e.g.:	int foo = foo, *bar = NULL;#	struct foo bar = *(&(bar));		if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {			my $var = $1;			if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {				WARN("SELF_ASSIGNMENT",				     "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);			}		} # check for dereferences that span multiple lines		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;			my $ref = $1;			$line =~ /^.\s*($Lval)/;			$ref .= $1;			$ref =~ s/\s//g;			WARN("MULTILINE_DEREFERENCE",			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);		} # check for declarations of signed or unsigned without int		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {			my $type = $1;			my $var = $2;			$var = "" if (!defined $var);			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {				my $sign = $1;				my $pointer = $2; 				$pointer = "" if (!defined $pointer); 				if (WARN("UNSPECIFIED_INT",					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&				    $fix) {					my $decl = trim($sign) . " int ";					my $comp_pointer = $pointer;					$comp_pointer =~ s/\s//g;					$decl .= $comp_pointer;					$decl = rtrim($decl) if ($var eq "");					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;				}			}		} # TEST: allow direct testing of the type matcher.		if ($dbg_type) {			if ($line =~ /^.\s*$Declare\s*$/) {				ERROR("TEST_TYPE",				      "TEST: is type\n" . $herecurr);			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {				ERROR("TEST_NOT_TYPE",				      "TEST: is not type ($1 is)\n". $herecurr);			}			next;		}# TEST: allow direct testing of the attribute matcher.		if ($dbg_attr) {			if ($line =~ /^.\s*$Modifier\s*$/) {				ERROR("TEST_ATTR",				      "TEST: is attr\n" . $herecurr);			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {				ERROR("TEST_NOT_ATTR",				      "TEST: is not attr ($1 is)\n". $herecurr);			}			next;		} # check for initialisation to aggregates open brace on the next line		if ($line =~ /^.\s*{/ &&		    $prevline =~ /(?:^|[^=])=\s*$/) {			if (ERROR("OPEN_BRACE",				  "that open brace { should be on the previous line\n" . $hereprev) &&			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {				fix_delete_line($fixlinenr - 1, $prevrawline);				fix_delete_line($fixlinenr, $rawline);				my $fixedline = $prevrawline;				$fixedline =~ s/\s*=\s*$/ = {/;				fix_insert_line($fixlinenr, $fixedline);				$fixedline = $line;				$fixedline =~ s/^(.\s*)\{\s*/$1/;				fix_insert_line($fixlinenr, $fixedline);			}		} ## 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",				      "malformed #include filename\n" . $herecurr);			}			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {				ERROR("UAPI_INCLUDE",				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);			}		} # no C99 // comments		if ($line =~ m{//}) {			if (ERROR("C99_COMMENTS",				  "do not use C99 // comments\n" . $herecurr) &&			    $fix) {				my $line = $fixed[$fixlinenr];				if ($line =~ /\/\/(.*)$/) {					my $comment = trim($1);					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;				}			}		}		# Remove C99 comments.		$line =~ s@//.*@@;		$opline =~ s@//.*@@; # EXPORT_SYMBOL should immediately follow the thing it is exporting, consider# the whole statement.#print "APW <$lines[$realline_next - 1]>\n";		if (defined $realline_next &&		    exists $lines[$realline_next - 1] &&		    !defined $suppress_export{$realline_next} &&		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {			# Handle definitions which produce identifiers with			# a prefix:			#   XXX(foo);			#   EXPORT_SYMBOL(something_foo);			my $name = $1;			$name =~ s/^\s*($Ident).*/$1/;			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&			    $name =~ /^${Ident}_$2/) {#print "FOO C name<$name>\n";				$suppress_export{$realline_next} = 1; 			} elsif ($stat !~ /(?:				\n.}\s*$|				^.DEFINE_$Ident\(\Q$name\E\)|				^.DECLARE_$Ident\(\Q$name\E\)|				^.LIST_HEAD\(\Q$name\E\)|				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()			    )/x) {#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";				$suppress_export{$realline_next} = 2;			} else {				$suppress_export{$realline_next} = 1;			}		}		if (!defined $suppress_export{$linenr} &&		    $prevline =~ /^.\s*$/ &&		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {#print "FOO B <$lines[$linenr - 1]>\n";			$suppress_export{$linenr} = 2;

Lines 4001–4500 of 7586. Previous lines