lint/checkpatch.pl: Update to v5.18-2 lines related to "CONFIG_"

Signed-off-by: Elyes Haouas <ehaouas@noos.fr>
Change-Id: I8589d053871ad9ac64ae2f8fc380710be8e4556b
Reviewed-on: https://review.coreboot.org/c/coreboot/+/63576
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Martin L Roth <martinroth@google.com>
This commit is contained in:
Elyes Haouas 2022-04-12 12:17:50 +02:00 committed by Martin L Roth
parent 0b1a03cd18
commit f62a98939d
1 changed files with 87 additions and 51 deletions

View File

@ -71,6 +71,7 @@ my $color = "auto";
my $allow_c99_comments = 1; my $allow_c99_comments = 1;
my $git_command ='git'; # coreboot my $git_command ='git'; # coreboot
my $tabsize = 8; my $tabsize = 8;
my ${CONFIG_} = "CONFIG_";
# For coreboot jenkins # For coreboot jenkins
# If taint mode is enabled, Untaint the path - files must be in /bin, /usr/bin or /usr/local/bin # If taint mode is enabled, Untaint the path - files must be in /bin, /usr/bin or /usr/local/bin
if ( ${^TAINT} ) { if ( ${^TAINT} ) {
@ -141,6 +142,8 @@ Options:
--typedefsfile Read additional types from this file --typedefsfile Read additional types from this file
--color[=WHEN] Use colors 'always', 'never', or only when output --color[=WHEN] Use colors 'always', 'never', or only when output
is a terminal ('auto'). Default is 'auto'. is a terminal ('auto'). Default is 'auto'.
--kconfig-prefix=WORD use WORD as a prefix for Kconfig symbols (default
${CONFIG_})
-h, --help, --version display this help and exit -h, --help, --version display this help and exit
When FILE is - read standard input. When FILE is - read standard input.
@ -327,6 +330,7 @@ GetOptions(
'color=s' => \$color, 'color=s' => \$color,
'no-color' => \$color, #keep old behaviors of -nocolor 'no-color' => \$color, #keep old behaviors of -nocolor
'nocolor' => \$color, #keep old behaviors of -nocolor 'nocolor' => \$color, #keep old behaviors of -nocolor
'kconfig-prefix=s' => \${CONFIG_},
'h|help' => \$help, 'h|help' => \$help,
'version' => \$help 'version' => \$help
) or $help = 2; ) or $help = 2;
@ -2382,6 +2386,28 @@ sub pos_last_openparen {
return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
} }
sub get_raw_comment {
my ($line, $rawline) = @_;
my $comment = '';
for my $i (0 .. (length($line) - 1)) {
if (substr($line, $i, 1) eq "$;") {
$comment .= substr($rawline, $i, 1);
}
}
return $comment;
}
sub exclude_global_initialisers {
my ($realfile) = @_;
# Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ ||
$realfile =~ m@^samples/bpf/.*_kern\.c$@ ||
$realfile =~ m@/bpf/.*\.bpf\.c$@;
}
sub process { sub process {
my $filename = shift; my $filename = shift;
@ -2538,6 +2564,7 @@ sub process {
$sline =~ s/$;/ /g; #with comments as spaces $sline =~ s/$;/ /g; #with comments as spaces
my $rawline = $rawlines[$linenr - 1]; my $rawline = $rawlines[$linenr - 1];
my $raw_comment = get_raw_comment($line, $rawline);
# check if it's a mode change, rename or start of a patch # check if it's a mode change, rename or start of a patch
if (!$in_commit_log && if (!$in_commit_log &&
@ -3016,51 +3043,47 @@ sub process {
# Kconfig supports named choices), so use a word boundary # Kconfig supports named choices), so use a word boundary
# (\b) rather than a whitespace character (\s) # (\b) rather than a whitespace character (\s)
$line =~ /^\+\s*(?:config|menuconfig|choice)\b/) { $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
my $length = 0; my $ln = $linenr;
my $cnt = $realcnt; my $needs_help = 0;
my $ln = $linenr + 1; my $has_help = 0;
my $f; my $help_length = 0;
my $is_start = 0; while (defined $lines[$ln]) {
my $is_end = 0; my $f = $lines[$ln++];
for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
$f = $lines[$ln - 1];
$cnt-- if ($lines[$ln - 1] !~ /^-/);
$is_end = $lines[$ln - 1] =~ /^\+/;
next if ($f =~ /^-/); next if ($f =~ /^-/);
last if (!$file && $f =~ /^\@\@/); last if ($f !~ /^[\+ ]/); # !patch context
if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) { if ($f =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
$is_start = 1; $needs_help = 1;
} elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) { next;
if ($lines[$ln - 1] =~ "---help---") {
WARN("CONFIG_DESCRIPTION",
"prefer 'help' over '---help---' for new help texts\n" . $herecurr);
} }
$length = -1; if ($f =~ /^\+\s*help\s*$/) {
$has_help = 1;
next;
} }
$f =~ s/^.//; $f =~ s/^.//; # strip patch context [+ ]
$f =~ s/#.*//; $f =~ s/#.*//; # strip # directives
$f =~ s/^\s+//; $f =~ s/^\s+//; # strip leading blanks
next if ($f =~ /^$/); next if ($f =~ /^$/); # skip blank lines
# At the end of this Kconfig block:
# This only checks context lines in the patch # This only checks context lines in the patch
# and so hopefully shouldn't trigger false # and so hopefully shouldn't trigger false
# positives, even though some of these are # positives, even though some of these are
# common words in help texts # common words in help texts
if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice| if ($f =~ /^(?:config|menuconfig|choice|endchoice|
if|endif|menu|endmenu|source)\b/x) { if|endif|menu|endmenu|source)\b/x) {
$is_end = 1;
last; last;
} }
$length++; $help_length++ if ($has_help);
} }
if ($is_start && $is_end && $length < $min_conf_desc_length) { if ($needs_help &&
$help_length < $min_conf_desc_length) {
my $stat_real = get_stat_real($linenr, $ln - 1);
WARN("CONFIG_DESCRIPTION", WARN("CONFIG_DESCRIPTION",
"please write a paragraph that describes the config symbol fully\n" . $herecurr); "please write a help paragraph that fully describes the config symbol\n" . "$here\n$stat_real\n");
} }
#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
} }
# check for MAINTAINERS entries that don't have the right form # check for MAINTAINERS entries that don't have the right form
@ -6388,28 +6411,41 @@ sub process {
} }
} }
# check for case / default statements not preceded by break/fallthrough/switch # check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
my $has_break = 0; WARN("IS_ENABLED_CONFIG",
my $has_statement = 0; "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
my $count = 0; }
my $prevline = $linenr;
while ($prevline > 1 && ($file || $count < 3) && !$has_break) { # check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
$prevline--; if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
my $rline = $rawlines[$prevline - 1]; my $config = $1;
my $fline = $lines[$prevline - 1]; if (WARN("PREFER_IS_ENABLED",
last if ($fline =~ /^\@\@/); "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
next if ($fline =~ /^\-/); $fix) {
next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); }
next if ($fline =~ /^.[\s$;]*$/); }
$has_statement = 1;
$count++; # check for /* fallthrough */ like comment, prefer fallthrough;
$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/); my @fallthroughs = (
'fallthrough',
'@fallthrough@',
'lint -fallthrough[ \t]*',
'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
);
if ($raw_comment ne '') {
foreach my $ft (@fallthroughs) {
if ($raw_comment =~ /$ft/) {
my $msg_level = \&WARN;
$msg_level = \&CHK if ($file);
&{$msg_level}("PREFER_FALLTHROUGH",
"Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
last;
} }
if (!$has_break && $has_statement) {
WARN("MISSING_BREAK",
"Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
} }
} }