Re: seq_putc is preferable to seq_puts

From: Joe Perches
Date: Fri May 18 2018 - 12:54:45 EST


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) {