Commit 713a09de authored by Antonio Borneo's avatar Antonio Borneo Committed by Linus Torvalds

checkpatch: add command-line option for TAB size

Linux kernel coding style requires a size of 8 characters for both TAB and
indentation, and such value is embedded as magic value allover the
checkpatch script.

This makes hard to reuse the script by other projects with different
requirements in their coding style (e.g.  OpenOCD [1] requires TAB size of
4 characters [2]).

Replace the magic value 8 with a variable.

Add a command-line option "--tab-size" to let the user select a
TAB size value other than 8.

[1] http://openocd.org/
[2] http://openocd.org/doc/doxygen/html/stylec.html#styleformatSigned-off-by: default avatarAntonio Borneo <borneo.antonio@gmail.com>
Signed-off-by: default avatarErik Ahlén <erik.ahlen@avalonenterprise.com>
Signed-off-by: default avatarSpencer Oliver <spen@spen-soft.co.uk>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Cc: Joe Perches <joe@perches.com>
Link: http://lkml.kernel.org/r/20200122163852.124417-3-borneo.antonio@gmail.comSigned-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 7b18496c
...@@ -64,6 +64,7 @@ my $color = "auto"; ...@@ -64,6 +64,7 @@ my $color = "auto";
my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
# git output parsing needs US English output, so first set backtick child process LANGUAGE # git output parsing needs US English output, so first set backtick child process LANGUAGE
my $git_command ='export LANGUAGE=en_US.UTF-8; git'; my $git_command ='export LANGUAGE=en_US.UTF-8; git';
my $tabsize = 8;
sub help { sub help {
my ($exitcode) = @_; my ($exitcode) = @_;
...@@ -98,6 +99,7 @@ Options: ...@@ -98,6 +99,7 @@ Options:
--show-types show the specific message type in the output --show-types show the specific message type in the output
--max-line-length=n set the maximum line length, if exceeded, warn --max-line-length=n set the maximum line length, if exceeded, warn
--min-conf-desc-length=n set the min description length, if shorter, warn --min-conf-desc-length=n set the min description length, if shorter, warn
--tab-size=n set the number of spaces for tab (default 8)
--root=PATH PATH to the kernel tree root --root=PATH PATH to the kernel tree root
--no-summary suppress the per-file summary --no-summary suppress the per-file summary
--mailback only produce a report in case of warnings/errors --mailback only produce a report in case of warnings/errors
...@@ -215,6 +217,7 @@ GetOptions( ...@@ -215,6 +217,7 @@ GetOptions(
'list-types!' => \$list_types, 'list-types!' => \$list_types,
'max-line-length=i' => \$max_line_length, 'max-line-length=i' => \$max_line_length,
'min-conf-desc-length=i' => \$min_conf_desc_length, 'min-conf-desc-length=i' => \$min_conf_desc_length,
'tab-size=i' => \$tabsize,
'root=s' => \$root, 'root=s' => \$root,
'summary!' => \$summary, 'summary!' => \$summary,
'mailback!' => \$mailback, 'mailback!' => \$mailback,
...@@ -267,6 +270,9 @@ if ($color =~ /^[01]$/) { ...@@ -267,6 +270,9 @@ if ($color =~ /^[01]$/) {
die "Invalid color mode: $color\n"; die "Invalid color mode: $color\n";
} }
# skip TAB size 1 to avoid additional checks on $tabsize - 1
die "Invalid TAB size: $tabsize\n" if ($tabsize < 2);
sub hash_save_array_words { sub hash_save_array_words {
my ($hashRef, $arrayRef) = @_; my ($hashRef, $arrayRef) = @_;
...@@ -1239,7 +1245,7 @@ sub expand_tabs { ...@@ -1239,7 +1245,7 @@ sub expand_tabs {
if ($c eq "\t") { if ($c eq "\t") {
$res .= ' '; $res .= ' ';
$n++; $n++;
for (; ($n % 8) != 0; $n++) { for (; ($n % $tabsize) != 0; $n++) {
$res .= ' '; $res .= ' ';
} }
next; next;
...@@ -2252,7 +2258,7 @@ sub string_find_replace { ...@@ -2252,7 +2258,7 @@ sub string_find_replace {
sub tabify { sub tabify {
my ($leading) = @_; my ($leading) = @_;
my $source_indent = 8; my $source_indent = $tabsize;
my $max_spaces_before_tab = $source_indent - 1; my $max_spaces_before_tab = $source_indent - 1;
my $spaces_to_tab = " " x $source_indent; my $spaces_to_tab = " " x $source_indent;
...@@ -3231,7 +3237,7 @@ sub process { ...@@ -3231,7 +3237,7 @@ sub process {
next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
# at the beginning of a line any tabs must come first and anything # at the beginning of a line any tabs must come first and anything
# more than 8 must use tabs. # more than $tabsize must use tabs.
if ($rawline =~ /^\+\s* \t\s*\S/ || if ($rawline =~ /^\+\s* \t\s*\S/ ||
$rawline =~ /^\+\s* \s*/) { $rawline =~ /^\+\s* \s*/) {
my $herevet = "$here\n" . cat_vet($rawline) . "\n"; my $herevet = "$here\n" . cat_vet($rawline) . "\n";
...@@ -3250,7 +3256,7 @@ sub process { ...@@ -3250,7 +3256,7 @@ sub process {
"please, no space before tabs\n" . $herevet) && "please, no space before tabs\n" . $herevet) &&
$fix) { $fix) {
while ($fixed[$fixlinenr] =~ while ($fixed[$fixlinenr] =~
s/(^\+.*) {8,8}\t/$1\t\t/) {} s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
while ($fixed[$fixlinenr] =~ while ($fixed[$fixlinenr] =~
s/(^\+.*) +\t/$1\t/) {} s/(^\+.*) +\t/$1\t/) {}
} }
...@@ -3272,11 +3278,11 @@ sub process { ...@@ -3272,11 +3278,11 @@ sub process {
if ($perl_version_ok && if ($perl_version_ok &&
$sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) { $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
my $indent = length($1); my $indent = length($1);
if ($indent % 8) { if ($indent % $tabsize) {
if (WARN("TABSTOP", if (WARN("TABSTOP",
"Statements should start on a tabstop\n" . $herecurr) && "Statements should start on a tabstop\n" . $herecurr) &&
$fix) { $fix) {
$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e; $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
} }
} }
} }
...@@ -3294,8 +3300,8 @@ sub process { ...@@ -3294,8 +3300,8 @@ sub process {
my $newindent = $2; my $newindent = $2;
my $goodtabindent = $oldindent . my $goodtabindent = $oldindent .
"\t" x ($pos / 8) . "\t" x ($pos / $tabsize) .
" " x ($pos % 8); " " x ($pos % $tabsize);
my $goodspaceindent = $oldindent . " " x $pos; my $goodspaceindent = $oldindent . " " x $pos;
if ($newindent ne $goodtabindent && if ($newindent ne $goodtabindent &&
...@@ -3766,11 +3772,11 @@ sub process { ...@@ -3766,11 +3772,11 @@ sub process {
#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"; #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 '' && if ($check && $s ne '' &&
(($sindent % 8) != 0 || (($sindent % $tabsize) != 0 ||
($sindent < $indent) || ($sindent < $indent) ||
($sindent == $indent && ($sindent == $indent &&
($s !~ /^\s*(?:\}|\{|else\b)/)) || ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
($sindent > $indent + 8))) { ($sindent > $indent + $tabsize))) {
WARN("SUSPECT_CODE_INDENT", WARN("SUSPECT_CODE_INDENT",
"suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment