Skip to content

Commit d1fe9c0

Browse files
JoePerchestorvalds
authored andcommitted
checkpatch: add some --strict coding style checks
Argument alignment across multiple lines should match the open parenthesis. Logical continuations should be at the end of the previous line, not the start of a new line. These are not required by CodingStyle so make the tests active only when using --strict. Improved by some examples from Bruce Allen. Signed-off-by: Joe Perches <joe@perches.com> Cc: "Bruce W. Allen" <bruce.w.allan@intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 6061d94 commit d1fe9c0

1 file changed

Lines changed: 84 additions & 10 deletions

File tree

scripts/checkpatch.pl

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,15 @@ sub build_types {
330330
}
331331
build_types();
332332

333-
our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/;
334333

335334
our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
336-
our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*};
335+
336+
# Using $balanced_parens, $LvalOrFunc, or $FuncArg
337+
# requires at least perl version v5.10.0
338+
# Any use must be runtime checked with $^V
339+
340+
our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
341+
our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*};
337342
our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)};
338343

339344
sub deparenthesize {
@@ -1330,6 +1335,36 @@ sub check_absolute_file {
13301335
}
13311336
}
13321337

1338+
sub pos_last_openparen {
1339+
my ($line) = @_;
1340+
1341+
my $pos = 0;
1342+
1343+
my $opens = $line =~ tr/\(/\(/;
1344+
my $closes = $line =~ tr/\)/\)/;
1345+
1346+
my $last_openparen = 0;
1347+
1348+
if (($opens == 0) || ($closes >= $opens)) {
1349+
return -1;
1350+
}
1351+
1352+
my $len = length($line);
1353+
1354+
for ($pos = 0; $pos < $len; $pos++) {
1355+
my $string = substr($line, $pos);
1356+
if ($string =~ /^($FuncArg|$balanced_parens)/) {
1357+
$pos += length($1) - 1;
1358+
} elsif (substr($line, $pos, 1) eq '(') {
1359+
$last_openparen = $pos;
1360+
} elsif (index($string, '(') == -1) {
1361+
last;
1362+
}
1363+
}
1364+
1365+
return $last_openparen + 1;
1366+
}
1367+
13331368
sub process {
13341369
my $filename = shift;
13351370

@@ -1783,6 +1818,37 @@ sub process {
17831818
"please, no space before tabs\n" . $herevet);
17841819
}
17851820

1821+
# check for && or || at the start of a line
1822+
if ($rawline =~ /^\+\s*(&&|\|\|)/) {
1823+
CHK("LOGICAL_CONTINUATIONS",
1824+
"Logical continuations should be on the previous line\n" . $hereprev);
1825+
}
1826+
1827+
# check multi-line statement indentation matches previous line
1828+
if ($^V && $^V ge 5.10.0 &&
1829+
$prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) {
1830+
$prevline =~ /^\+(\t*)(.*)$/;
1831+
my $oldindent = $1;
1832+
my $rest = $2;
1833+
1834+
my $pos = pos_last_openparen($rest);
1835+
if ($pos >= 0) {
1836+
$line =~ /^\+([ \t]*)/;
1837+
my $newindent = $1;
1838+
1839+
my $goodtabindent = $oldindent .
1840+
"\t" x ($pos / 8) .
1841+
" " x ($pos % 8);
1842+
my $goodspaceindent = $oldindent . " " x $pos;
1843+
1844+
if ($newindent ne $goodtabindent &&
1845+
$newindent ne $goodspaceindent) {
1846+
CHK("PARENTHESIS_ALIGNMENT",
1847+
"Alignment should match open parenthesis\n" . $hereprev);
1848+
}
1849+
}
1850+
}
1851+
17861852
# check for spaces at the beginning of a line.
17871853
# Exceptions:
17881854
# 1) within comments
@@ -3142,12 +3208,13 @@ sub process {
31423208
}
31433209

31443210
# Check for misused memsets
3145-
if (defined $stat &&
3211+
if ($^V && $^V ge 5.10.0 &&
3212+
defined $stat &&
31463213
$stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) {
31473214

31483215
my $ms_addr = $2;
3149-
my $ms_val = $8;
3150-
my $ms_size = $14;
3216+
my $ms_val = $7;
3217+
my $ms_size = $12;
31513218

31523219
if ($ms_size =~ /^(0x|)0$/i) {
31533220
ERROR("MEMSET",
@@ -3159,17 +3226,18 @@ sub process {
31593226
}
31603227

31613228
# typecasts on min/max could be min_t/max_t
3162-
if (defined $stat &&
3229+
if ($^V && $^V ge 5.10.0 &&
3230+
defined $stat &&
31633231
$stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
3164-
if (defined $2 || defined $8) {
3232+
if (defined $2 || defined $7) {
31653233
my $call = $1;
31663234
my $cast1 = deparenthesize($2);
31673235
my $arg1 = $3;
3168-
my $cast2 = deparenthesize($8);
3169-
my $arg2 = $9;
3236+
my $cast2 = deparenthesize($7);
3237+
my $arg2 = $8;
31703238
my $cast;
31713239

3172-
if ($cast1 ne "" && $cast2 ne "") {
3240+
if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
31733241
$cast = "$cast1 or $cast2";
31743242
} elsif ($cast1 ne "") {
31753243
$cast = $cast1;
@@ -3391,6 +3459,12 @@ sub process {
33913459
}
33923460

33933461
if ($quiet == 0) {
3462+
3463+
if ($^V lt 5.10.0) {
3464+
print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
3465+
print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
3466+
}
3467+
33943468
# If there were whitespace errors which cleanpatch can fix
33953469
# then suggest that.
33963470
if ($rpt_cleaners) {

0 commit comments

Comments
 (0)