6972 lines · 205.6 KB · Perl
RawDownload
			}		}		# 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.*\((.*)\)/ ||		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {			# Handle definitions which produce identifiers with			# a prefix:			#   XXX(foo);			#   EXPORT_SYMBOL(something_foo);			my $name = $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.*\((.*)\)/ ||		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {#print "FOO B <$lines[$linenr - 1]>\n";			$suppress_export{$linenr} = 2;		}		if (defined $suppress_export{$linenr} &&		    $suppress_export{$linenr} == 2) {			WARN("EXPORT_SYMBOL",			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);		} # check for global initialisers.		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {			if (ERROR("GLOBAL_INITIALISERS",				  "do not initialise globals to $1\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;			}		}# check for static initialisers.		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {			if (ERROR("INITIALISED_STATIC",				  "do not initialise statics to $1\n" .				      $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;			}		} # check for misordered declarations of char/short/int/long with signed/unsigned		while ($sline =~ m{(\b$TypeMisordered\b)}g) {			my $tmp = trim($1);			WARN("MISORDERED_TYPE",			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);		} # check for unnecessary <signed> int declarations of short/long/long long		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {			my $type = trim($1);			next if ($type !~ /\bint\b/);			next if ($type !~ /\b(?:short|long\s+long|long)\b/);			my $new_type = $type;			$new_type =~ s/\b\s*int\s*\b/ /;			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;			$new_type =~ s/^const\s+//;			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);			$new_type = "const $new_type" if ($type =~ /^const\b/);			$new_type =~ s/\s+/ /g;			$new_type = trim($new_type);			if (WARN("UNNECESSARY_INT",				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;			}		} # check for static const char * arrays.		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {			WARN("STATIC_CONST_CHAR_ARRAY",			     "static const char * array should probably be static const char * const\n" .				$herecurr);		} # check for initialized const char arrays that should be static const		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {			if (WARN("STATIC_CONST_CHAR_ARRAY",				 "const array should probably be static const\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;			}		} # check for static char foo[] = "bar" declarations.		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {			WARN("STATIC_CONST_CHAR_ARRAY",			     "static char array declaration should probably be static const char\n" .				$herecurr);		} # check for const <foo> const where <foo> is not a pointer or array type		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {			my $found = $1;			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {				WARN("CONST_CONST",				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {				WARN("CONST_CONST",				     "'const $found const' should probably be 'const $found'\n" . $herecurr);			}		} # check for non-global char *foo[] = {"bar", ...} declarations.		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {			WARN("STATIC_CONST_CHAR_ARRAY",			     "char * array declaration might be better as static const\n" .				$herecurr);               } # check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {			my $array = $1;			if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {				my $array_div = $1;				if (WARN("ARRAY_SIZE",					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&				    $fix) {					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;				}			}		} # check for function declarations without arguments like "int foo()"		if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {			if (ERROR("FUNCTION_WITHOUT_ARGS",				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;			}		} # 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("NEW_TYPEDEFS",			     "do not add new typedefs\n" . $herecurr);		} # * goes on variable not on type		# (char*[ const])		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {			#print "AA<$1>\n";			my ($ident, $from, $to) = ($1, $2, $2); 			# Should start with a space.			$to =~ s/^(\S)/ $1/;			# Should not end with a space.			$to =~ s/\s+$//;			# '*'s should not have spaces between.			while ($to =~ s/\*\s+\*/\*\*/) {			} ##			print "1: from<$from> to<$to> ident<$ident>\n";			if ($from ne $to) {				if (ERROR("POINTER_LOCATION",					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&				    $fix) {					my $sub_from = $ident;					my $sub_to = $ident;					$sub_to =~ s/\Q$from\E/$to/;					$fixed[$fixlinenr] =~					    s@\Q$sub_from\E@$sub_to@;				}			}		}		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {			#print "BB<$1>\n";			my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 			# Should start with a space.			$to =~ s/^(\S)/ $1/;			# Should not end with a space.			$to =~ s/\s+$//;			# '*'s should not have spaces between.			while ($to =~ s/\*\s+\*/\*\*/) {			}			# Modifiers should have spaces.			$to =~ s/(\b$Modifier$)/$1 /; ##			print "2: from<$from> to<$to> ident<$ident>\n";			if ($from ne $to && $ident !~ /^$Modifier$/) {				if (ERROR("POINTER_LOCATION",					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&				    $fix) { 					my $sub_from = $match;					my $sub_to = $match;					$sub_to =~ s/\Q$from\E/$to/;					$fixed[$fixlinenr] =~					    s@\Q$sub_from\E@$sub_to@;				}			}		} # avoid BUG() or BUG_ON()		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {			my $msg_level = \&WARN;			$msg_level = \&CHK if ($file);			&{$msg_level}("AVOID_BUG",				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);		} # avoid LINUX_VERSION_CODE		if ($line =~ /\bLINUX_VERSION_CODE\b/) {			WARN("LINUX_VERSION_CODE",			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);		} # check for uses of printk_ratelimit		if ($line =~ /\bprintk_ratelimit\s*\(/) {			WARN("PRINTK_RATELIMITED",			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);		} # printk should use KERN_* levels		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {			WARN("PRINTK_WITHOUT_KERN_LEVEL",			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);		} 		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {			my $orig = $1;			my $level = lc($orig);			$level = "warn" if ($level eq "warning");			my $level2 = $level;			$level2 = "dbg" if ($level eq "debug");			WARN("PREFER_PR_LEVEL",			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);		} 		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {			my $orig = $1;			my $level = lc($orig);			$level = "warn" if ($level eq "warning");			$level = "dbg" if ($level eq "debug");			WARN("PREFER_DEV_LEVEL",			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);		} # ENOSYS means "bad syscall nr" and nothing else.  This will have a small# number of false positives, but assembly files are not checked, so at# least the arch entry code will not trigger this warning.		if ($line =~ /\bENOSYS\b/) {			WARN("ENOSYS",			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);		} # ENOTSUPP is not a standard error code and should be avoided in new patches.# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.# Similarly to ENOSYS warning a small number of false positives is expected.		if (!$file && $line =~ /\bENOTSUPP\b/) {			if (WARN("ENOTSUPP",				 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;			}		} # function brace can't be on same line, except for #defines of do while,# or if closed on same line		if ($perl_version_ok &&		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&		    $sline !~ /\#\s*define\b.*do\s*\{/ &&		    $sline !~ /}/) {			if (ERROR("OPEN_BRACE",				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&			    $fix) {				fix_delete_line($fixlinenr, $rawline);				my $fixed_line = $rawline;				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;				my $line1 = $1;				my $line2 = $2;				fix_insert_line($fixlinenr, ltrim($line1));				fix_insert_line($fixlinenr, "\+{");				if ($line2 !~ /^\s*$/) {					fix_insert_line($fixlinenr, "\+\t" . trim($line2));				}			}		} # 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*$/) {			if (ERROR("OPEN_BRACE",				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {				fix_delete_line($fixlinenr - 1, $prevrawline);				fix_delete_line($fixlinenr, $rawline);				my $fixedline = rtrim($prevrawline) . " {";				fix_insert_line($fixlinenr, $fixedline);				$fixedline = $rawline;				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;				if ($fixedline !~ /^\+\s*$/) {					fix_insert_line($fixlinenr, $fixedline);				}			}		} # missing space after union, struct or enum definition		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {			if (WARN("SPACING",				 "missing space after $1 definition\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;			}		} # Function pointer declarations# check spacing between type, funcptr, and args# canonical declaration is "type (*funcptr)(args...)"		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {			my $declare = $1;			my $pre_pointer_space = $2;			my $post_pointer_space = $3;			my $funcname = $4;			my $post_funcname_space = $5;			my $pre_args_space = $6; # the $Declare variable will capture all spaces after the type# so check it for a missing trailing missing space but pointer return types# don't need a space so don't warn for those.			my $post_declare_space = "";			if ($declare =~ /(\s+)$/) {				$post_declare_space = $1;				$declare = rtrim($declare);			}			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {				WARN("SPACING",				     "missing space after return type\n" . $herecurr);				$post_declare_space = " ";			} # unnecessary space "type  (*funcptr)(args...)"# This test is not currently implemented because these declarations are# equivalent to#	int  foo(int bar, ...)# and this is form shouldn't/doesn't generate a checkpatch warning.##			elsif ($declare =~ /\s{2,}$/) {#				WARN("SPACING",#				     "Multiple spaces after return type\n" . $herecurr);#			} # unnecessary space "type ( *funcptr)(args...)"			if (defined $pre_pointer_space &&			    $pre_pointer_space =~ /^\s/) {				WARN("SPACING",				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);			} # unnecessary space "type (* funcptr)(args...)"			if (defined $post_pointer_space &&			    $post_pointer_space =~ /^\s/) {				WARN("SPACING",				     "Unnecessary space before function pointer name\n" . $herecurr);			} # unnecessary space "type (*funcptr )(args...)"			if (defined $post_funcname_space &&			    $post_funcname_space =~ /^\s/) {				WARN("SPACING",				     "Unnecessary space after function pointer name\n" . $herecurr);			} # unnecessary space "type (*funcptr) (args...)"			if (defined $pre_args_space &&			    $pre_args_space =~ /^\s/) {				WARN("SPACING",				     "Unnecessary space before function pointer arguments\n" . $herecurr);			} 			if (show_type("SPACING") && $fix) {				$fixed[$fixlinenr] =~				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;			}		} # 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+$/) {				if (ERROR("BRACKET_SPACE",					  "space prohibited before open square bracket '['\n" . $herecurr) &&				    $fix) {				    $fixed[$fixlinenr] =~					s/^(\+.*?)\s+\[/$1\[/;				}			}		} # 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$/) { 			} else {				if (WARN("SPACING",					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&					     $fix) {					$fixed[$fixlinenr] =~					    s/\b$name\s+\(/$name\(/;				}			}		} # Check operator spacing.		if (!($line=~/\#\s*include/)) {			my $fixed_line = "";			my $line_fixed = 0; 			my $ops = qr{				<<=|>>=|<=|>=|==|!=|				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|				=>|->|<<|>>|<|>|=|!|~|				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|				\?:|\?|:			}x;			my @elements = split(/($ops|;)/, $opline); ##			print("element count: <" . $#elements . ">\n");##			foreach my $el (@elements) {##				print("el: <$el>\n");##			} 			my @fix_elements = ();			my $off = 0; 			foreach my $el (@elements) {				push(@fix_elements, substr($rawline, $off, length($el)));				$off += length($el);			} 			$off = 0; 			my $blank = copy_spacing($opline);			my $last_after = -1; 			for (my $n = 0; $n < $#elements; $n += 2) { 				my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 

Lines 4001–4500 of 6972. Previous lines