7976 lines · 239.6 KB · Perl
RawDownload
## 				WARN("MULTIPLE_DECLARATION",##				     "declaring multiple variables together should be avoided\n" . $herecurr);## 			}## 		} #need space before brace following if, while, etc		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||		    $line =~ /\b(?:else|do)\{/) {			if (ERROR("SPACING",				  "space required before the open brace '{'\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;			}		} ## # check for blank lines before declarations##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&##		    $prevrawline =~ /^.\s*$/) {##			WARN("SPACING",##			     "No blank lines before declarations\n" . $hereprev);##		}## # closing brace should have a space following it when it has anything# on the line		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {			if (ERROR("SPACING",				  "space required after that close brace '}'\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~				    s/}((?!(?:,|;|\)))\S)/} $1/;			}		} # check spacing on square brackets		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {			if (ERROR("SPACING",				  "space prohibited after that open square bracket '['\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~				    s/\[\s+/\[/;			}		}		if ($line =~ /\s\]/) {			if (ERROR("SPACING",				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~				    s/\s+\]/\]/;			}		} # check spacing on parentheses		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&		    $line !~ /for\s*\(\s+;/) {			if (ERROR("SPACING",				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~				    s/\(\s+/\(/;			}		}		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&		    $line !~ /for\s*\(.*;\s+\)/ &&		    $line !~ /:\s+\)/) {			if (ERROR("SPACING",				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~				    s/\s+\)/\)/;			}		} # check unnecessary parentheses around addressof/dereference single $Lvals# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {			my $var = $1;			if (CHK("UNNECESSARY_PARENTHESES",				"Unnecessary parentheses around $var\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;			}		} # check for unnecessary parentheses around function pointer uses# ie: (foo->bar)(); should be foo->bar();# but not "if (foo->bar) (" to avoid some false positives		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {			my $var = $2;			if (CHK("UNNECESSARY_PARENTHESES",				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&			    $fix) {				my $var2 = deparenthesize($var);				$var2 =~ s/\s//g;				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;			}		} # check for unnecessary parentheses around comparisons# except in drivers/staging		if (($realfile !~ m@^(?:drivers/staging/)@) &&		    $perl_version_ok && defined($stat) &&		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {			my $if_stat = $1;			my $test = substr($2, 1, -1);			my $herectx;			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {				my $match = $1;				# avoid parentheses around potential macro args				next if ($match =~ /^\s*\w+\s*$/);				if (!defined($herectx)) {					$herectx = $here . "\n";					my $cnt = statement_rawlines($if_stat);					for (my $n = 0; $n < $cnt; $n++) {						my $rl = raw_line($linenr, $n);						$herectx .=  $rl . "\n";						last if $rl =~ /^[ \+].*\{/;					}				}				CHK("UNNECESSARY_PARENTHESES",				    "Unnecessary parentheses around '$match'\n" . $herectx);			}		} # check that goto labels aren't indented (allow a single space indentation)# and ignore bitfield definitions like foo:1# Strictly, labels can have whitespace after the identifier and before the :# but this is not allowed here as many ?: uses would appear to be labels		if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&		    $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&		    $sline !~ /^.\s+default:/) {			if (WARN("INDENTED_LABEL",				 "labels should not be indented\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~				    s/^(.)\s+/$1/;			}		} # check if a statement with a comma should be two statements like:#	foo = bar(),	/* comma should be semicolon */#	bar = baz();		if (defined($stat) &&		    $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {			my $cnt = statement_rawlines($stat);			my $herectx = get_stat_here($linenr, $cnt, $here);			WARN("SUSPECT_COMMA_SEMICOLON",			     "Possible comma where semicolon could be used\n" . $herectx);		} # return is not a function		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {			my $spacing = $1;			if ($perl_version_ok &&			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {				my $value = $1;				$value = deparenthesize($value);				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {					ERROR("RETURN_PARENTHESES",					      "return is not a function, parentheses are not required\n" . $herecurr);				}			} elsif ($spacing !~ /\s+/) {				ERROR("SPACING",				      "space required before the open parenthesis '('\n" . $herecurr);			}		} # unnecessary return in a void function# at end-of-function, with the previous line a single leading tab, then return;# and the line before that not a goto label target like "out:"		if ($sline =~ /^[ \+]}\s*$/ &&		    $prevline =~ /^\+\treturn\s*;\s*$/ &&		    $linenr >= 3 &&		    $lines[$linenr - 3] =~ /^[ +]/ &&		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {			WARN("RETURN_VOID",			     "void function return statements are not generally useful\n" . $hereprev);		} # if statements using unnecessary parentheses - ie: if ((foo == bar))		if ($perl_version_ok &&		    $line =~ /\bif\s*((?:\(\s*){2,})/) {			my $openparens = $1;			my $count = $openparens =~ tr@\(@\(@;			my $msg = "";			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {				my $comp = $4;	#Not $1 because of $LvalOrFunc				$msg = " - maybe == should be = ?" if ($comp eq "==");				WARN("UNNECESSARY_PARENTHESES",				     "Unnecessary parentheses$msg\n" . $herecurr);			}		} # comparisons with a constant or upper case identifier on the left#	avoid cases like "foo + BAR < baz"#	only fix matches surrounded by parentheses to avoid incorrect#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"		if ($perl_version_ok &&		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {			my $lead = $1;			my $const = $2;			my $comp = $3;			my $to = $4;			my $newcomp = $comp;			if ($lead !~ /(?:$Operators|\.)\s*$/ &&			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&			    WARN("CONSTANT_COMPARISON",				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&			    $fix) {				if ($comp eq "<") {					$newcomp = ">";				} elsif ($comp eq "<=") {					$newcomp = ">=";				} elsif ($comp eq ">") {					$newcomp = "<";				} elsif ($comp eq ">=") {					$newcomp = "<=";				}				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;			}		} # Return of what appears to be an errno should normally be negative		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {			my $name = $1;			if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {				WARN("USE_NEGATIVE_ERRNO",				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);			}		} # Need a space before open parenthesis after if, while etc		if ($line =~ /\b(if|while|for|switch)\(/) {			if (ERROR("SPACING",				  "space required before the open parenthesis '('\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~				    s/\b(if|while|for|switch)\(/$1 \(/;			}		} # Check for illegal assignment in if conditional -- and check for trailing# statements after the conditional.		if ($line =~ /do\s*(?!{)/) {			($stat, $cond, $line_nr_next, $remain_next, $off_next) =				ctx_statement_block($linenr, $realcnt, 0)					if (!defined $stat);			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} &&		    defined($stat) && defined($cond) &&		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {			my ($s, $c) = ($stat, $cond);			my $fixed_assign_in_if = 0; 			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {				if (ERROR("ASSIGN_IN_IF",					  "do not use assignment in if condition\n" . $herecurr) &&				    $fix && $perl_version_ok) {					if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {						my $space = $1;						my $not = $2;						my $statement = $3;						my $assigned = $4;						my $test = $8;						my $against = $9;						my $brace = $15;						fix_delete_line($fixlinenr, $rawline);						fix_insert_line($fixlinenr, "$space$statement;");						my $newline = "${space}if (";						$newline .= '!' if defined($not);						$newline .= '(' if (defined $not && defined($test) && defined($against));						$newline .= "$assigned";						$newline .= " $test $against" if (defined($test) && defined($against));						$newline .= ')' if (defined $not && defined($test) && defined($against));						$newline .= ')';						$newline .= " {" if (defined($brace));						fix_insert_line($fixlinenr + 1, $newline);						$fixed_assign_in_if = 1;					}				}			} 			# Find out what is on the end of the line after the			# conditional.			substr($s, 0, length($c), '');			$s =~ s/\n.*//g;			$s =~ s/$;//g;	# Remove any comments			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&			    $c !~ /}\s*while\s*/)			{				# Find out how long the conditional actually is.				my @newlines = ($c =~ /\n/gs);				my $cond_lines = 1 + $#newlines;				my $stat_real = ''; 				$stat_real = raw_line($linenr, $cond_lines)							. "\n" if ($cond_lines);				if (defined($stat_real) && $cond_lines > 1) {					$stat_real = "[...]\n$stat_real";				} 				if (ERROR("TRAILING_STATEMENTS",					  "trailing statements should be on next line\n" . $herecurr . $stat_real) &&				    !$fixed_assign_in_if &&				    $cond_lines == 0 &&				    $fix && $perl_version_ok &&				    $fixed[$fixlinenr] =~ /^\+(\s*)((?:if|while|for)\s*$balanced_parens)\s*(.*)$/) {					my $indent = $1;					my $test = $2;					my $rest = rtrim($4);					if ($rest =~ /;$/) {						$fixed[$fixlinenr] = "\+$indent$test";						fix_insert_line($fixlinenr + 1, "$indent\t$rest");					}				}			}		} # Check for bitwise tests written as boolean		if ($line =~ /			(?:				(?:\[|\(|\&\&|\|\|)				\s*0[xX][0-9]+\s*				(?:\&\&|\|\|)			|				(?:\&\&|\|\|)				\s*0[xX][0-9]+\s*				(?:\&\&|\|\||\)|\])			)/x)		{			WARN("HEXADECIMAL_BOOLEAN_TEST",			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);		} # if and else should not have general statements after it		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {			my $s = $1;			$s =~ s/$;//g;	# Remove any comments			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {				ERROR("TRAILING_STATEMENTS",				      "trailing statements should be on next line\n" . $herecurr);			}		}# if should not continue a brace		if ($line =~ /}\s*if\b/) {			ERROR("TRAILING_STATEMENTS",			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .				$herecurr);		}# case and default should not have general statements after them		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&		    $line !~ /\G(?:			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|			\s*return\s+		    )/xg)		{			ERROR("TRAILING_STATEMENTS",			      "trailing statements should be on next line\n" . $herecurr);		} 		# Check for }<nl>else {, these must be at the same		# indent level to be relevant to each other.		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&		    $previndent == $indent) {			if (ERROR("ELSE_AFTER_BRACE",				  "else should follow close brace '}'\n" . $hereprev) &&			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {				fix_delete_line($fixlinenr - 1, $prevrawline);				fix_delete_line($fixlinenr, $rawline);				my $fixedline = $prevrawline;				$fixedline =~ s/}\s*$//;				if ($fixedline !~ /^\+\s*$/) {					fix_insert_line($fixlinenr, $fixedline);				}				$fixedline = $rawline;				$fixedline =~ s/^(.\s*)else/$1} else/;				fix_insert_line($fixlinenr, $fixedline);			}		} 		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&		    $previndent == $indent) {			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 			# Find out what is on the end of the line after the			# conditional.			substr($s, 0, length($c), '');			$s =~ s/\n.*//g; 			if ($s =~ /^\s*;/) {				if (ERROR("WHILE_AFTER_BRACE",					  "while should follow close brace '}'\n" . $hereprev) &&				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {					fix_delete_line($fixlinenr - 1, $prevrawline);					fix_delete_line($fixlinenr, $rawline);					my $fixedline = $prevrawline;					my $trailing = $rawline;					$trailing =~ s/^\+//;					$trailing = trim($trailing);					$fixedline =~ s/}\s*$/} $trailing/;					fix_insert_line($fixlinenr, $fixedline);				}			}		} #Specific variable tests		while ($line =~ m{($Constant|$Lval)}g) {			my $var = $1; #CamelCase			if ($var !~ /^$Constant$/ &&			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&#Ignore C keywords			    $var !~ /^_Generic$/ &&#Ignore some autogenerated defines and enum values			    $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&#Ignore Page<foo> variants			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&#Ignore ETHTOOL_LINK_MODE_<foo> variants			    $var !~ /^ETHTOOL_LINK_MODE_/ &&#Ignore SI style variants like nS, mV and dB#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&#Ignore some three character SI units explicitly, like MiB and KHz			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {				while ($var =~ m{\b($Ident)}g) {					my $word = $1;					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);					if ($check) {						seed_camelcase_includes();						if (!$file && !$camelcase_file_seeded) {							seed_camelcase_file($realfile);							$camelcase_file_seeded = 1;						}					}					if (!defined $camelcase{$word}) {						$camelcase{$word} = 1;						CHK("CAMELCASE",						    "Avoid CamelCase: <$word>\n" . $herecurr);					}				}			}		} #no spaces allowed after \ in define		if ($line =~ /\#\s*define.*\\\s+$/) {			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&			    $fix) {				$fixed[$fixlinenr] =~ s/\s+$//;			}		} # warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes# itself <asm/foo.h> (uses RAW line)		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {			my $file = "$1.h";			my $checkfile = "include/linux/$file";			if (-f "$root/$checkfile" &&			    $realfile ne $checkfile &&			    $1 !~ /$allowed_asm_includes/)			{				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;				if ($asminclude > 0) {					if ($realfile =~ m{^arch/}) {						CHK("ARCH_INCLUDE_LINUX",						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);					} else {						WARN("INCLUDE_LINUX",						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);					}				}			}		} # Usually multi-statement macros should be enclosed in a do {} while# (0) loop.  Grab the first statement and ensure its the whole macro# if its not enclosed in a known good container		if ($realfile !~ m@/vmlinux.lds.h$@ &&		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {			my $ln = $linenr;			my $cnt = $realcnt;			my ($off, $dstat, $dcond, $rest);

Lines 5501–6000 of 7976. Previous lines