kernel-janitors.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Markus Elfring <Markus.Elfring@web.de>
To: Coccinelle <cocci@systeme.lip6.fr>,
	kernel-janitors@vger.kernel.org,
	Gilles Muller <Gilles.Muller@lip6.fr>,
	Julia Lawall <Julia.Lawall@lip6.fr>,
	Masahiro Yamada <masahiroy@kernel.org>,
	Michal Marek <michal.lkml@markovi.net>,
	Nicolas Palix <nicolas.palix@imag.fr>
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH] Coccinelle: Add a SmPL script for the reconsideration of function calls before return statem
Date: Wed, 08 Jul 2020 17:30:38 +0000	[thread overview]
Message-ID: <de887f9e-8db2-7839-5665-8d38e75aaddc@web.de> (raw)
In-Reply-To: <4b3bb651-5db0-021c-cbea-347eda0e95e0@web.de>

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Wed, 8 Jul 2020 19:09:59 +0200

It can happen that function implementations with the return type “void”
call a special function at the end of if branches.
Such calls are occasionally followed by an immediate return.
The same function can be called at the end of the function implementation.
Thus it can be helpful to replace previous function calls
by goto statements.

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>
---
 .../misc/goto_last_function_call.cocci        | 89 +++++++++++++++++++
 1 file changed, 89 insertions(+)
 create mode 100644 scripts/coccinelle/misc/goto_last_function_call.cocci

diff --git a/scripts/coccinelle/misc/goto_last_function_call.cocci b/scripts/coccinelle/misc/goto_last_function_call.cocci
new file mode 100644
index 000000000000..92dcf3626c48
--- /dev/null
+++ b/scripts/coccinelle/misc/goto_last_function_call.cocci
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0
+/// Reconsider function calls before a return statement in if branches.
+//
+// Keywords: duplicate function calls common exception handling
+// Confidence: Low
+// See also:
+// Clarification request “Adding labels without indentation before specific statements?”
+// https://lore.kernel.org/cocci/4b3bb651-5db0-021c-cbea-347eda0e95e0@web.de/
+
+virtual context, patch, report, org
+
+@display depends on context@
+expression action;
+expression list el;
+identifier work;
+@@
+ void work(...)
+ {
+ <+...
+ if (...)
+ {
+    ...
+*   action(el);
+*   return;
+ }
+ ...+>
+*action(el);
+ }
+
+@replacement depends on patch@
+expression action, condition;
+expression list el;
+identifier work;
+@@
+ void work(...)
+ {
+ <+...
+(
+-if (condition)
+-{
+-   action(el);
+-   return;
+-}
++if (condition)
++   goto last_action;
+|
+ if (...)
+ {
+    ...
+-   action(el);
+-   return;
++   goto last_action;
+ }
+)
+ ...+>
++last_action:
+ action(el);
+ }
+
+@or depends on org || report@
+expression action;
+expression list el;
+identifier work;
+position p;
+@@
+ void work(...)
+ {
+ <+...
+ if (...)
+ {
+    ...
+    action(el);
+    return;
+ }
+ ...+>
+ action@p(el);
+ }
+
+@script:python to_do depends on org@
+p << or.p;
+@@
+coccilib.org.print_todo(p[0],
+                        "WARNING: The same function was called at the end of an if branch before. Would you like to avoid duplicate function calls?")
+
+@script:python reporting depends on report@
+p << or.p;
+@@
+coccilib.report.print_report(p[0],
+                             "WARNING: The same function was called at the end of an if branch before. Would you like to avoid duplicate function calls?")
--
2.27.0

           reply	other threads:[~2020-07-08 17:30 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <4b3bb651-5db0-021c-cbea-347eda0e95e0@web.de>]

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=de887f9e-8db2-7839-5665-8d38e75aaddc@web.de \
    --to=markus.elfring@web.de \
    --cc=Gilles.Muller@lip6.fr \
    --cc=Julia.Lawall@lip6.fr \
    --cc=cocci@systeme.lip6.fr \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=michal.lkml@markovi.net \
    --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 NNTP newsgroup(s).