From: Markus Elfring <Markus.Elfring@web.de>
To: cocci@inria.fr, kernel-janitors@vger.kernel.org,
Julia Lawall <Julia.Lawall@inria.fr>,
Nicolas Palix <nicolas.palix@imag.fr>
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: [cocci] [PATCH] Coccinelle: api: Add SmPL script “use_return_ptr.cocci”
Date: Wed, 27 Mar 2024 14:10:32 +0100 [thread overview]
Message-ID: <b1d4a52f-4dbb-48a3-be59-8e9915f21fa9@web.de> (raw)
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Wed, 27 Mar 2024 13:56:03 +0100
A wrapper macro is available since the commit 54da6a0924311c7cf5015533991e44fb8eb12773
("locking: Introduce __cleanup() based infrastructure").
Provide design options for the adjustment of affected source code
by the means of the semantic patch language (Coccinelle software).
Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
scripts/coccinelle/api/use_return_ptr.cocci | 57 +++++++++++++++++++++
1 file changed, 57 insertions(+)
create mode 100644 scripts/coccinelle/api/use_return_ptr.cocci
diff --git a/scripts/coccinelle/api/use_return_ptr.cocci b/scripts/coccinelle/api/use_return_ptr.cocci
new file mode 100644
index 000000000000..7b42a58cceb2
--- /dev/null
+++ b/scripts/coccinelle/api/use_return_ptr.cocci
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0
+/// Simplify a return statement by using a known wrapper macro.
+//
+// Keywords: wrapper macro conversion returning objects
+// Confidence: High
+// Options: --no-includes --include-headers
+
+virtual context, patch, report, org
+
+@depends on context@
+expression e;
+@@
+ if (!e)
+ return NULL;
+ <+... when any
+*return \( e \| no_free_ptr(e) \)
+ ;
+ ...+>
+
+@depends on patch@
+expression e;
+@@
+ if (!e)
+ return NULL;
+ <+... when any
+(
+-return
++return_ptr(
+ e
++)
+ ;
+|
+-return no_free_ptr
++return_ptr
+ (e);
+)
+ ...+>
+
+@x depends on org || report@
+expression e;
+position p;
+@@
+ if (!e)
+ return NULL;
+ <+... when any
+ return@p \( e \| no_free_ptr(e) \) ;
+ ...+>
+
+@script:python depends on org@
+p << x.p;
+@@
+coccilib.org.print_todo(p[0], "WARNING: opportunity for return_ptr()")
+
+@script:python depends on report@
+p << x.p;
+@@
+coccilib.report.print_report(p[0], "WARNING: opportunity for return_ptr()")
--
2.44.0
reply other threads:[~2024-03-27 13:10 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=b1d4a52f-4dbb-48a3-be59-8e9915f21fa9@web.de \
--to=markus.elfring@web.de \
--cc=Julia.Lawall@inria.fr \
--cc=cocci@inria.fr \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nicolas.palix@imag.fr \
/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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).