Message ID | 20201128200046.78739-1-dwaipayanray1@gmail.com |
---|---|
State | Accepted |
Commit | 70eb2275ff8e0b4cafe67176674d580c987c071d |
Headers | show |
Series |
|
Related | show |
On Sun, 2020-11-29 at 01:30 +0530, Dwaipayan Ray wrote: > Modifiers %h and %hh should never be used. Seems OK, thanks. Acked-by: Joe Perches <joe@perches.com> > Commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use > of unnecessary %h[xudi] and %hh[xudi]") specifies that: > > "Standard integer promotion is already done and %hx and %hhx is useless > so do not encourage the use of %hh[xudi] or %h[xudi]." > > "The "h" and "hh" things should never be used. The only reason for them > being used if you have an "int", but you want to print it out as a > "char" (and honestly, that is a really bad reason, you'd be better off > just using a proper cast to make the code more obvious)." > > Add a new check to emit a warning on finding an unneeded use of %h or > %hh modifier. > > Also add a fix option to the check. > > Link: https://lore.kernel.org/lkml/4910042649a4f3ab22fac93191b8c1fa0a2e17c3.camel@perches.com/ > > Suggested-by: Joe Perches <joe@perches.com> > Suggested-by: Lukas Bulwahn <lukas.bulwahn@gmail.com> > Signed-off-by: Dwaipayan Ray <dwaipayanray1@gmail.com> > --- > Changes in v4: > - Change warning type and message > - Use $stat_real and calculate line offset during matches > - fix option now works for any patch line > - # was added to format block to match %#06hh > > Changes in v3: > - Change warning message > - Fix regex match to include capture group > - Warn on every unnecesary use of %h on a line > - Add fix option when the format line matches current line > > Changes in v2: > - Use $logFunctions instead of the manual list. > - Relocate the check to after logging continuations check. > - Remove perl_version_ok check > > > scripts/checkpatch.pl | 22 ++++++++++++++++++++++ > 1 file changed, 22 insertions(+) > > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl > index 7dc094445d83..3c86ea737e9c 100755 > --- a/scripts/checkpatch.pl > +++ b/scripts/checkpatch.pl > @@ -6027,6 +6027,28 @@ sub process { > "Avoid logging continuation uses where feasible\n" . $herecurr); > } > > +# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions > + if (defined $stat && > + $line =~ /\b$logFunctions\s*\(/ && > + index($stat, '"') >= 0) { > + my $lc = $stat =~ tr@\n@@; > + $lc = $lc + $linenr; > + my $stat_real = get_stat_real($linenr, $lc); > + pos($stat_real) = index($stat_real, '"'); > + while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) { > + my $pspec = $1; > + my $h = $2; > + my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@; > + if (WARN("UNNECESSARY_MODIFIER", > + "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") && > + $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) { > + my $nspec = $pspec; > + $nspec =~ s/h//g; > + $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/; > + } > + } > + } > + > # check for mask then right shift without a parentheses > if ($perl_version_ok && > $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 7dc094445d83..3c86ea737e9c 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl @@ -6027,6 +6027,28 @@ sub process { "Avoid logging continuation uses where feasible\n" . $herecurr); } +# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions + if (defined $stat && + $line =~ /\b$logFunctions\s*\(/ && + index($stat, '"') >= 0) { + my $lc = $stat =~ tr@\n@@; + $lc = $lc + $linenr; + my $stat_real = get_stat_real($linenr, $lc); + pos($stat_real) = index($stat_real, '"'); + while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) { + my $pspec = $1; + my $h = $2; + my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@; + if (WARN("UNNECESSARY_MODIFIER", + "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") && + $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) { + my $nspec = $pspec; + $nspec =~ s/h//g; + $fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/; + } + } + } + # check for mask then right shift without a parentheses if ($perl_version_ok && $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
Modifiers %h and %hh should never be used. Commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of unnecessary %h[xudi] and %hh[xudi]") specifies that: "Standard integer promotion is already done and %hx and %hhx is useless so do not encourage the use of %hh[xudi] or %h[xudi]." "The "h" and "hh" things should never be used. The only reason for them being used if you have an "int", but you want to print it out as a "char" (and honestly, that is a really bad reason, you'd be better off just using a proper cast to make the code more obvious)." Add a new check to emit a warning on finding an unneeded use of %h or %hh modifier. Also add a fix option to the check. Link: https://lore.kernel.org/lkml/4910042649a4f3ab22fac93191b8c1fa0a2e17c3.camel@perches.com/ Suggested-by: Joe Perches <joe@perches.com> Suggested-by: Lukas Bulwahn <lukas.bulwahn@gmail.com> Signed-off-by: Dwaipayan Ray <dwaipayanray1@gmail.com> --- Changes in v4: - Change warning type and message - Use $stat_real and calculate line offset during matches - fix option now works for any patch line - # was added to format block to match %#06hh Changes in v3: - Change warning message - Fix regex match to include capture group - Warn on every unnecesary use of %h on a line - Add fix option when the format line matches current line Changes in v2: - Use $logFunctions instead of the manual list. - Relocate the check to after logging continuations check. - Remove perl_version_ok check scripts/checkpatch.pl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)