mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Lars-Peter Clausen <lars@metafoo.de>
To: Julia Lawall <julia@diku.dk>,
	Gilles Muller <Gilles.Muller@lip6.fr>,
	Nicolas Palix <npalix.work@gmail.com>
Cc: <cocci@diku.dk>, <linux-kernel@vger.kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>
Subject: [PATCH 2/2] coccinelle: Add patch for replacing open-coded IS_ERR_OR_NULL
Date: Mon, 19 Dec 2011 14:24:23 +0100	[thread overview]
Message-ID: <1324301063-16117-2-git-send-email-lars@metafoo.de> (raw)
In-Reply-To: <1324301063-16117-1-git-send-email-lars@metafoo.de>

The IS_ERR_OR_NULL function returns true if the passed parameter is either a
ERR_PTR or NULL. This patch adds a semantic patch which finds open-coded
instances of this check and replaces them.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
 scripts/coccinelle/api/is_err_or_null.cocci |   98 +++++++++++++++++++++++++++
 1 files changed, 98 insertions(+), 0 deletions(-)
 create mode 100644 scripts/coccinelle/api/is_err_or_null.cocci

diff --git a/scripts/coccinelle/api/is_err_or_null.cocci b/scripts/coccinelle/api/is_err_or_null.cocci
new file mode 100644
index 0000000..04afd1e
--- /dev/null
+++ b/scripts/coccinelle/api/is_err_or_null.cocci
@@ -0,0 +1,98 @@
+///
+/// Use IS_ERR_OR_NULL function instead of open coding it
+///
+// Confidence: High
+// Options:
+//
+// Keywords: IS_ERR, IS_ERR_OR_NULL
+// Version min: 2.6.33
+//
+
+virtual context
+virtual patch
+virtual org
+virtual report
+
+// Since the logical or operator by itself is not commutative we need rules for
+// both cases as well as for their De Morgan equivalents.
+
+@depends on context@
+expression x;
+@@
+*(IS_ERR(x) || !x)
+
+@depends on context@
+expression x;
+@@
+*(!x || IS_ERR(x))
+
+@depends on context@
+expression x;
+@@
+*(!IS_ERR(x) && x)
+
+@depends on context@
+expression x;
+@@
+*(x && !IS_ERR(x))
+
+@depends on patch@
+expression x;
+@@
+-(IS_ERR(x) || !x)
++IS_ERR_OR_NULL(x)
+
+@depends on patch@
+expression x;
+@@
+-(!x || IS_ERR(x))
++IS_ERR_OR_NULL(x)
+
+@depends on patch@
+expression x;
+@@
+-(!IS_ERR(x) && x)
++ !IS_ERR_OR_NULL(x)
+
+@depends on patch@
+expression x;
+@@
+-(x && !IS_ERR(x))
++ !IS_ERR_OR_NULL(x)
+
+@r depends on org || report@
+expression x;
+position p;
+statement S;
+@@
+// We'll probably miss some some cases with this, but the if gives us an anchor
+// and we do not have to write a indivual rule for each case. patch and context
+// mode will report all cases
+ if(@p
+(
+ IS_ERR(x) || !x
+|
+ !x || IS_ERR(x)
+|
+ !IS_ERR(x) && x
+|
+ x && !IS_ERR(x)
+)
+ ) S
+
+@script:python depends on org@
+p << r.p;
+x << r.x;
+@@
+
+msg="IS_ERR_OR_NULL can be used with %s" % (x)
+msg_safe=msg.replace("[","@(").replace("]",")")
+coccilib.org.print_todo(p[0], msg_safe)
+
+@script:python depends on report@
+p << r.p;
+x << r.x;
+@@
+
+msg="IS_ERR_OR_NULL can be used with %s" % (x)
+coccilib.report.print_report(p[0], msg)
-- 
1.7.7.3



  reply	other threads:[~2011-12-19 13:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-19 13:24 [PATCH 1/2] coccinelle: Add patch for replacing open-coded PTR_RET Lars-Peter Clausen
2011-12-19 13:24 ` Lars-Peter Clausen [this message]
2011-12-20 22:24   ` [Cocci] [PATCH 2/2] coccinelle: Add patch for replacing open-coded IS_ERR_OR_NULL Julia Lawall
2011-12-21 11:49     ` Lars-Peter Clausen
2011-12-21 12:10       ` Julia Lawall
2011-12-23 12:19         ` Lars-Peter Clausen
2011-12-23 12:26           ` Julia Lawall
2011-12-20 22:19 ` [Cocci] [PATCH 1/2] coccinelle: Add patch for replacing open-coded PTR_RET Julia Lawall

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=1324301063-16117-2-git-send-email-lars@metafoo.de \
    --to=lars@metafoo.de \
    --cc=Gilles.Muller@lip6.fr \
    --cc=cocci@diku.dk \
    --cc=julia@diku.dk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=npalix.work@gmail.com \
    /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®