mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Julia Lawall <Julia.Lawall@lip6.fr>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ingo Molnar <mingo@redhat.com>
Cc: linux-kernel@vger.kernel.org,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>
Subject: [PATCH 1/6] Coccinelle: Semantic patch for replacing puts with putc
Date: Fri, 12 Sep 2014 11:25:49 +0200	[thread overview]
Message-ID: <1410513954-28909-2-git-send-email-linux@rasmusvillemoes.dk> (raw)
In-Reply-To: <1410513954-28909-1-git-send-email-linux@rasmusvillemoes.dk>

Using seq_puts to write a one-character string is suboptimal,
since puts has to call strlen(). Replace those instances with
putc. This also tends to give a tiny code size reduction at the
call site.

seq_put[sc] return -1 on error, 0 on success.
trace_seq_put[sc] return how much was written, which is either 0 or 1.

In both cases, _putc is a drop-in replacement for _puts when the
argument is a one-character string.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 scripts/coccinelle/api/seq_putsc.cocci | 61 ++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 scripts/coccinelle/api/seq_putsc.cocci

diff --git a/scripts/coccinelle/api/seq_putsc.cocci b/scripts/coccinelle/api/seq_putsc.cocci
new file mode 100644
index 0000000..bb11097
--- /dev/null
+++ b/scripts/coccinelle/api/seq_putsc.cocci
@@ -0,0 +1,61 @@
+/// Using seq_puts to write a one-character string is suboptimal,
+/// since puts has to call strlen(). Replace those instances with
+/// putc. This also tends to give a tiny code size reduction at the
+/// call site.
+///
+/// seq_put[sc] return -1 on error, 0 on success.
+/// trace_seq_put[sc] return how much was written, which is either 0 or 1.
+///
+/// In both cases, _putc is a drop-in replacement for _puts when the
+/// argument is a one-character string.
+///
+//
+// Confidence: High
+// Options: --no-includes --include-headers
+//
+// Since the changes to any given line should consist of changing an s
+// to a c and two " to ', the generated patches should be easy to
+// proofread.
+
+virtual patch
+virtual context
+virtual org
+virtual report
+
+@putc1 depends on patch@
+position p;
+expression s;
+constant c;
+@@
+  \(trace_seq_puts@p\|seq_puts@p\)(s, c)
+
+// Use python to check whether the string constant consists of a
+// single character, and if so, create an "identifier" containing that
+// single character as a C literal.
+@script:python putc2@
+s << putc1.s;
+c << putc1.c;
+ch;
+@@
+import re
+import sys
+
+m = re.match("^\"(.|\\\\.)\"$", c)
+if not m:
+    cocci.include_match(False)
+else:
+    coccinelle.ch = "'" + m.group(1) + "'"
+
+@putc3 depends on patch@
+position putc1.p;
+expression putc1.s;
+constant putc1.c;
+identifier putc2.ch;
+@@
+(
+- seq_puts@p(s, c)
++ seq_putc(s, ch)
+|
+- trace_seq_puts@p(s, c)
++ trace_seq_putc(s, ch)
+)
-- 
2.0.4


  reply	other threads:[~2014-09-12  9:27 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-12  9:25 [PATCH 0/6] Small seqfile-use improvements Rasmus Villemoes
2014-09-12  9:25 ` Rasmus Villemoes [this message]
2014-09-12 11:08   ` [PATCH 1/6] Coccinelle: Semantic patch for replacing puts with putc SF Markus Elfring
2014-09-12  9:25 ` [PATCH 2/6] Coccinelle: Semantic patch for joining seq_puts calls Rasmus Villemoes
2014-09-12  9:25 ` [PATCH 3/6] Coccinelle: Semantic patch for replacing seq_printf calls with equivalent but simpler functions Rasmus Villemoes
2014-09-12  9:25 ` [PATCH 4/6] trace: Replace seq_printf by simpler equivalents Rasmus Villemoes
2014-11-05 22:34   ` Steven Rostedt
2014-11-05 22:44     ` Rasmus Villemoes
2014-11-06  0:34       ` Steven Rostedt
2014-11-05 22:51     ` Joe Perches
2014-11-06  0:38       ` Steven Rostedt
2014-11-06  0:47         ` Joe Perches
2014-11-08 20:42   ` [PATCH v2 0/3] trace: Use simpler seq_file functions Rasmus Villemoes
2014-11-08 20:42     ` [PATCH v2 1/3] trace: Replace seq_printf by simpler equivalents Rasmus Villemoes
2014-11-14  2:31       ` Steven Rostedt
2014-11-08 20:42     ` [PATCH v2 2/3] trace: Merge consecutive seq_puts calls Rasmus Villemoes
2014-11-08 20:42     ` [PATCH v2 3/3] trace: Replace single-character seq_puts with seq_putc Rasmus Villemoes
2014-11-14  2:34       ` Steven Rostedt
2014-09-12  9:25 ` [PATCH 5/6] trace: Merge consecutive seq_puts calls Rasmus Villemoes
2014-09-12  9:25 ` [PATCH 6/6] trace: Replace single-character seq_puts with seq_putc Rasmus Villemoes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1410513954-28909-2-git-send-email-linux@rasmusvillemoes.dk \
    --to=linux@rasmusvillemoes.dk \
    --cc=Julia.Lawall@lip6.fr \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®