mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* seq_putc is preferable to seq_puts
@ 2018-05-18 14:09 Matthew Wilcox
  2018-05-18 17:50 ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Matthew Wilcox @ 2018-05-18 14:09 UTC (permalink / raw)
  To: Joe Perches; +Cc: linux-kernel


Hi Joe,

A nice checkpatch addition would be to look for seq_puts(x, "s")
and recommend the author use seq_putc(x, 's') instead.  The primary
offender is seq_puts(x, "\n") (a hundred or so, scattered throughout
the kernel), but seq_puts(x, " ") has its adherents too.

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

* Re: seq_putc is preferable to seq_puts
  2018-05-18 14:09 seq_putc is preferable to seq_puts Matthew Wilcox
@ 2018-05-18 17:50 ` Joe Perches
  2018-05-18 18:35   ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Perches @ 2018-05-18 17:50 UTC (permalink / raw)
  To: Matthew Wilcox; +Cc: linux-kernel, Andrew Morton

On Fri, 2018-05-18 at 07:09 -0700, Matthew Wilcox wrote:
> Hi Joe,
> 
> A nice checkpatch addition would be to look for seq_puts(x, "s")
> and recommend the author use seq_putc(x, 's') instead.  The primary
> offender is seq_puts(x, "\n") (a hundred or so, scattered throughout
> the kernel), but seq_puts(x, " ") has its adherents too.

Firstly, has Markus Elfring left the building?

Here's a grep to find the existing uses:

$ git grep -P '\b(?:seq_puts|seq_printf)\s*\(\s*[^,]+,\s*"(?:\\.|.)"\s*\)\s*;' | \
  wc -l
326

and a trivial script to convert them all treewide

$ git grep -P --name-only '\b(?:seq_puts|seq_printf)\s*\(\s*[^,]+,\s*"(?:\\.|.)"\s*\)\s*;' | \
  xargs perl -i -e 'local $/; while (<>) { s/\b(?:seq_puts|seq_printf)\s*\(\s*([^,]+),\s*"(\\.|.)"\s*\)\s*;/seq_putc(\1, '"'\2'"')/g; print;}'

Anyway, I still like a variant of Steven Rostedt's
trace_puts macro silliness hack that avoids all
this by allowing seq_printf without format arguments
to call the appropriate seq_puts/seq_putc invisibly.

https://groups.google.com/forum/#!topic/linux.kernel/YGZhTD-n4UA

It's relatively easy to expand this for seq_putc, but here's
an expanded checkpatch rule anyway that seems to work.
---
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index baddac9379f0..62b36d7bb3af 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -5791,11 +5791,23 @@ sub process {
 			     "struct spinlock should be spinlock_t\n" . $herecurr);
 		}
 
-# check for seq_printf uses that could be seq_puts
-		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
+# check for seq_printf/seq_puts uses that could be seq_puts or seq_putc
+		if ($sline =~ /\b(seq_(?:printf|puts))\s*\(.*"\s*\)\s*;\s*$/) {
+			my $call = $1;
 			my $fmt = get_quoted_string($line, $rawline);
+			my $fmt_nq = substr($fmt, 1, length($fmt) - 2); #strip outer quotes
 			$fmt =~ s/%%//g;
-			if ($fmt !~ /%/) {
+
+			if (length($fmt_nq) == 1 ||
+			    (length($fmt_nq) == 2 && ($fmt_nq =~ /^\\/ || $fmt_nq =~ /\%\%/))) {
+				my $fmt_sub = $fmt_nq;
+				$fmt_sub =~ s/\%\%/%/;
+				if (WARN("PREFER_SEQ_PUTC",
+					 "Prefer seq_putc to $call\n" . $herecurr) &&
+				    $fix) {
+					$fixed[$fixlinenr] =~ s/\b$call\s*\(\s*($FuncArg)\s*,\s*"\Q$fmt_nq\E"\s*\)/seq_putc($1, '$fmt_sub')/x;
+				}
+			} elsif ($fmt !~ /%/) {
 				if (WARN("PREFER_SEQ_PUTS",
 					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
 				    $fix) {

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

* Re: seq_putc is preferable to seq_puts
  2018-05-18 17:50 ` Joe Perches
@ 2018-05-18 18:35   ` Joe Perches
  0 siblings, 0 replies; 3+ messages in thread
From: Joe Perches @ 2018-05-18 18:35 UTC (permalink / raw)
  To: Matthew Wilcox; +Cc: linux-kernel, Andrew Morton

On Fri, 2018-05-18 at 10:50 -0700, Joe Perches wrote:
> and a trivial script to convert them all treewide
> 
> $ git grep -P --name-only '\b(?:seq_puts|seq_printf)\s*\(\s*[^,]+,\s*"(?:\\.|.)"\s*\)\s*;' | \
>   xargs perl -i -e 'local $/; while (<>) { s/\b(?:seq_puts|seq_printf)\s*\(\s*([^,]+),\s*"(\\.|.)"\s*\)\s*;/seq_putc(\1, '"'\2'"')/g; print;}'

Hmm, pity this was missing a trailing semicolon in the substitution

$ git grep -P --name-only '\b(?:seq_puts|seq_printf)\s*\(\s*[^,]+,\s*"(?:\\.|.)"\s*\)\s*;' | \
  xargs perl -i -e 'local $/; while (<>) { s/\b(?:seq_puts|seq_printf)\s*\(\s*([^,]+),\s*"(\\.|.)"\s*\)\s*;/seq_putc(\1, '"'\2'"');/g; print;}'

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

end of thread, other threads:[~2018-05-18 18:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-18 14:09 seq_putc is preferable to seq_puts Matthew Wilcox
2018-05-18 17:50 ` Joe Perches
2018-05-18 18:35   ` 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®