checkpatch: add warning for unnecessary use of %h[xudi] and %hh[xudi]
PATCH v3 1/1 checkpatch: add warning for unnecessary use of %h[xudi] and %hh[xudi]
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.
---
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 | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
scripts/checkpatch.pl +16 -0
@@ -6027,6 +6027,22 @@ 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*\(/) { + my $lc = $stat =~ tr@\n@@; + for (my $cur_ln = $linenr; $cur_ln <= $linenr + $lc; $cur_ln++) { + my $cur_rawline = raw_line($cur_ln, 0); + while ($cur_rawline =~ /^\+.*\"[^\"]*(%[\d\.\*\-]*h+[idux])/g) { + if (WARN("INTEGER_PROMOTION", + "Using 'h' in $1 is unnecessary\n" . "$cur_rawline\n") && + $fix && ($cur_ln == $linenr)) { + $fixed[$fixlinenr] =~ s/(\"[^\"]*%[\d\.\*\-]*)h+([idux])/$1$2/; + } + } + } + } + # check for mask then right shift without a parentheses if ($perl_version_ok && $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
Signature · 6 lines
2.27.0 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Joe Perches
maintainerQuoted 53 lines · Dwaipayan Ray
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.
Suggested-by Joe Perches <[email protected]>Suggested-by Lukas Bulwahn <[email protected]>Signed-off-by Dwaipayan Ray <[email protected]>---
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 lineChanges in v2:
- Use $logFunctions instead of the manual list.
- Relocate the check to after logging continuations check.
- Removeperl_version_okcheckscripts/checkpatch.pl | 16 ++++++++++++++++
1 file changed, 16 insertions(+)scripts/checkpatch.pl +0
-0@@ -6027,6 +6027,22 @@ sub process { "Avoid logging continuation uses where feasible\n" . $herecurr); }scripts/checkpatch.pl +6
-0+# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions + if (defined $stat && + $line =~ /\b$logFunctions\s*\(/) { + my $lc = $stat =~ tr@\n@@; + for (my $cur_ln = $linenr; $cur_ln <= $linenr + $lc; $cur_ln++) { + my $cur_rawline = raw_line($cur_ln, 0);
scripts/checkpatch.pl
Quoted 10 lines
scripts/checkpatch.pl +10
-0+ while ($cur_rawline =~ /^\+.*\"[^\"]*(%[\d\.\*\-]*h+[idux])/g) { + if (WARN("INTEGER_PROMOTION", + "Using 'h' in $1 is unnecessary\n" . "$cur_rawline\n") && + $fix && ($cur_ln == $linenr)) { + $fixed[$fixlinenr] =~ s/(\"[^\"]*%[\d\.\*\-]*)h+([idux])/$1$2/; + } + } + } + } +
Here was what I tried:
There are uses like %#06hh", so # was addedto the format block and multiple line uses were also inspected.
scripts/checkpatch.pl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
scripts/checkpatch.pl +23 -0
@@ -6027,6 +6027,29 @@ 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, '"'); + my $lineoff = substr($stat_real, 0, pos($stat_real)) =~ tr/\n//; + while ($stat_real =~ /[^"%]*(%[#\d\.\*\-]*(h+)[idux])/g) { + my $pspec = $1; + my $h = $2; + if (WARN("UNNECESSARY_MODIFIER", + "Integer promotion: '$h' use 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*>>/ &&
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 34 lines
Here was what I tried:
There are uses like %#06hh", so # was addedto the format block and multiple line uses were also inspected.
scripts/checkpatch.pl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)scripts/checkpatch.pl +16
-0@@ -6027,6 +6027,29 @@ 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, '"'); + my $lineoff = substr($stat_real, 0, pos($stat_real)) =~ tr/\n//; + while ($stat_real =~ /[^"%]*(%[#\d\.\*\-]*(h+)[idux])/g) { + my $pspec = $1; + my $h = $2; + if (WARN("UNNECESSARY_MODIFIER", + "Integer promotion: '$h' use in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") && + $fix && + $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
scripts/checkpatch.pl sub process { · line 6055
Quoted 10 lines
scripts/checkpatch.pl +7
-0+ 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*>>/ &&
Thanks, I will modify the block according to this and send it back to you.
_______________________________________________
Linux-kernel-mentees mailing list [email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees