cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
From: Denis Efremov <efremov@linux.com>
To: linux-kernel@vger.kernel.org, cocci@systeme.lip6.fr
Cc: Michal Marek <michal.lkml@markovi.net>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Nicolas Palix <nicolas.palix@imag.fr>,
	Markus Elfring <Markus.Elfring@web.de>,
	Joe Perches <joe@perches.com>
Subject: [Cocci] [PATCH v2] scripts: coccinelle: check for !(un)?likely usage
Date: Thu, 29 Aug 2019 20:10:13 +0300	[thread overview]
Message-ID: <20190829171013.22956-1-efremov@linux.com> (raw)
In-Reply-To: <20190825130536.14683-1-efremov@linux.com>

This patch adds coccinelle script for detecting !likely and
!unlikely usage. These notations are confusing. It's better
to replace !likely(x) with unlikely(!x) and !unlikely(x) with
likely(!x) for readability.

The rule transforms !likely(x) to unlikely(!x) based on this logic:
  !likely(x) iff
  !__builtin_expect(!!(x), 1) iff
   __builtin_expect(!!!(x), 0) iff
  unlikely(!x)

For !unlikely(x) to likely(!x):
  !unlikely(x) iff
  !__builtin_expect(!!(x), 0) iff
  __builtin_expect(!!!(x), 1) iff
  likely(!x)

Signed-off-by: Denis Efremov <efremov@linux.com>
Cc: Julia Lawall <Julia.Lawall@lip6.fr>
Cc: Gilles Muller <Gilles.Muller@lip6.fr>
Cc: Nicolas Palix <nicolas.palix@imag.fr>
Cc: Michal Marek <michal.lkml@markovi.net>
Cc: Markus Elfring <Markus.Elfring@web.de>
Cc: Joe Perches <joe@perches.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 scripts/coccinelle/misc/neg_unlikely.cocci | 89 ++++++++++++++++++++++
 1 file changed, 89 insertions(+)
 create mode 100644 scripts/coccinelle/misc/neg_unlikely.cocci

diff --git a/scripts/coccinelle/misc/neg_unlikely.cocci b/scripts/coccinelle/misc/neg_unlikely.cocci
new file mode 100644
index 000000000000..9aac0a7ff042
--- /dev/null
+++ b/scripts/coccinelle/misc/neg_unlikely.cocci
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Use unlikely(!x) instead of !likely(x) and vice versa.
+/// Notations !unlikely(x) and !likely(x) are confusing.
+//
+// Confidence: High
+// Copyright: (C) 2019 Denis Efremov, ISPRAS.
+// Options: --no-includes --include-headers
+
+virtual patch
+virtual context
+virtual org
+virtual report
+
+//----------------------------------------------------------
+//  For context mode
+//----------------------------------------------------------
+
+@depends on context disable unlikely@
+expression E;
+@@
+
+*! \( likely \| unlikely \) (E)
+
+//----------------------------------------------------------
+//  For patch mode
+//----------------------------------------------------------
+
+@depends on patch disable unlikely@
+expression E;
+@@
+
+(
+-!likely(!E)
++unlikely(E)
+|
+-!likely(E)
++unlikely(!E)
+|
+-!unlikely(!E)
++likely(E)
+|
+-!unlikely(E)
++likely(!E)
+)
+
+//----------------------------------------------------------
+//  For org and report mode
+//----------------------------------------------------------
+
+@r1 depends on (org || report) disable unlikely@
+expression E;
+position p;
+@@
+
+!likely@p(E)
+
+@r2 depends on (org || report) disable unlikely@
+expression E;
+position p;
+@@
+
+!unlikely@p(E)
+
+@script:python depends on org && r1@
+p << r1.p;
+@@
+
+coccilib.org.print_todo(p[0], "unlikely(!x) is more readable than !likely(x)")
+
+@script:python depends on org && r2@
+p << r2.p;
+@@
+
+coccilib.org.print_todo(p[0], "likely(!x) is more readable than !unlikely(x)")
+
+@script:python depends on report && r1@
+p << r1.p;
+@@
+
+coccilib.report.print_report(p[0],
+	"unlikely(!x) is more readable than !likely(x)")
+
+@script:python depends on report && r2@
+p << r2.p;
+@@
+
+coccilib.report.print_report(p[0],
+	"likely(!x) is more readable than !unlikely(x)")
+
-- 
2.21.0

_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

  parent reply	other threads:[~2019-08-29 17:10 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-25 13:05 [Cocci] [PATCH] scripts: coccinelle: check for !(un)?likely usage Denis Efremov
2019-08-25 15:30 ` Markus Elfring
2019-08-25 21:06   ` Denis Efremov
2019-08-25 15:30 ` Markus Elfring
2019-08-25 16:37 ` Joe Perches
2019-08-25 18:59   ` Denis Efremov
2019-08-25 19:19     ` Julia Lawall
2019-08-28 11:33       ` Rasmus Villemoes
2019-08-28 11:59         ` Joe Perches
2019-08-28 12:33         ` Denis Efremov
2019-08-28 12:33         ` Julia Lawall
2019-08-28 12:41       ` Denis Efremov
2019-08-28 13:57         ` Denis Efremov
2019-08-25 21:19     ` Denis Efremov
     [not found]   ` <20190901172403.GA1047@bug>
2019-09-01 17:39     ` Denis Efremov
2019-08-29 17:10 ` Denis Efremov [this message]
2019-08-29 17:13   ` [Cocci] [PATCH v2] " Denis Efremov
2019-08-30  0:42     ` Julia Lawall
2019-08-30  6:56       ` Denis Efremov
2019-08-30  8:06         ` Rasmus Villemoes
2019-08-29 20:07   ` Markus Elfring
2019-08-30  7:55   ` Markus Elfring
2019-09-06 20:19   ` Julia Lawall
2019-09-06 20:55     ` Denis Efremov
2019-09-07  8:05       ` [Cocci] [v2] " Markus Elfring

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=20190829171013.22956-1-efremov@linux.com \
    --to=efremov@linux.com \
    --cc=Markus.Elfring@web.de \
    --cc=cocci@systeme.lip6.fr \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --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).