mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH RFC] checkpatch: add warning for unnecessary use of %h[xudi] and %hh[xudi]
@ 2020-11-26  7:28 Dwaipayan Ray
  2020-11-26  8:02 ` Joe Perches
  0 siblings, 1 reply; 2+ messages in thread
From: Dwaipayan Ray @ 2020-11-26  7:28 UTC (permalink / raw)
  To: joe; +Cc: linux-kernel-mentees, dwaipayanray1, linux-kernel, lukas.bulwahn

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.

Link: https://lore.kernel.org/lkml/4910042649a4f3ab22fac93191b8c1fa0a2e17c3.camel@perches.com/

Suggested-by: Lukas Bulwahn <lukas.bulwahn@gmail.com>
Signed-off-by: Dwaipayan Ray <dwaipayanray1@gmail.com>
---
 scripts/checkpatch.pl | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 7dc094445d83..47c1017a9973 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6570,6 +6570,19 @@ sub process {
 			}
 		}
 
+# check for unnecessary use of %h[xudi] and %hh[xudi]
+		if ($perl_version_ok &&
+		    defined $stat &&
+		    $line =~ /(?:[sd]?print[kfd]|(?:pr_|drm_)(?:info|debug|warn|error))/i) {
+			my $lc = $stat =~ tr@\n@@;
+			$lc = $lc + $linenr;
+			my $stat_real = get_stat_real($linenr, $lc);
+			if ($stat_real =~ /\"[^\"]*%[\d\.\*\-]*h+[idux].*\"/i) {
+				WARN("UNNECESSARY_MODIFIER",
+				     "Unnecessary use of length modifiers %h[xudi] or %hh[xudi]\n" . "$here\n$stat_real\n");
+			}
+		}
+
 # check for naked sscanf
 		if ($perl_version_ok &&
 		    defined $stat &&
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH RFC] checkpatch: add warning for unnecessary use of %h[xudi] and %hh[xudi]
  2020-11-26  7:28 [PATCH RFC] checkpatch: add warning for unnecessary use of %h[xudi] and %hh[xudi] Dwaipayan Ray
@ 2020-11-26  8:02 ` Joe Perches
  0 siblings, 0 replies; 2+ messages in thread
From: Joe Perches @ 2020-11-26  8:02 UTC (permalink / raw)
  To: Dwaipayan Ray; +Cc: linux-kernel-mentees, linux-kernel, lukas.bulwahn

On Thu, 2020-11-26 at 12:58 +0530, Dwaipayan Ray wrote:
> 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.
> 
> Link: https://lore.kernel.org/lkml/4910042649a4f3ab22fac93191b8c1fa0a2e17c3.camel@perches.com/
> 
> Suggested-by: Lukas Bulwahn <lukas.bulwahn@gmail.com>
> Signed-off-by: Dwaipayan Ray <dwaipayanray1@gmail.com>
> ---
>  scripts/checkpatch.pl | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 7dc094445d83..47c1017a9973 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -6570,6 +6570,19 @@ sub process {
>  			}
>  		}
>  
> 
> +# check for unnecessary use of %h[xudi] and %hh[xudi]
> +		if ($perl_version_ok &&
> +		    defined $stat &&
> +		    $line =~ /(?:[sd]?print[kfd]|(?:pr_|drm_)(?:info|debug|warn|error))/i) {

Use $logFunctions and please move this close to the lines with
"# check for logging functions with KERN_<LEVEL>"

> +			my $lc = $stat =~ tr@\n@@;
> +			$lc = $lc + $linenr;
> +			my $stat_real = get_stat_real($linenr, $lc);
> +			if ($stat_real =~ /\"[^\"]*%[\d\.\*\-]*h+[idux].*\"/i) {
> +				WARN("UNNECESSARY_MODIFIER",
> +				     "Unnecessary use of length modifiers %h[xudi] or %hh[xudi]\n" . "$here\n$stat_real\n");
> +			}
> +		}
> +
>  # check for naked sscanf
>  		if ($perl_version_ok &&
>  		    defined $stat &&



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-11-26  8:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-26  7:28 [PATCH RFC] checkpatch: add warning for unnecessary use of %h[xudi] and %hh[xudi] Dwaipayan Ray
2020-11-26  8:02 ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®