lkml.org 
[lkml]   [2015]   [May]   [19]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH 1/1] scripts/coccinelle/misc: add swap.cocci
Date
Operations like

int a, b, tmp;

tmp = a;
a = b;
b = tmp;

can be replaced by

int a, b;

swap(a, b);

This uses kernel.h macro definition and simplifies the code.

Thanks to Julia Lawall for suggestions about expressions and sgen

Signed-off-by: Fabian Frederick <fabf@skynet.be>
---
scripts/coccinelle/misc/swap.cocci | 62 ++++++++++++++++++++++++++++++++++++++
1 file changed, 62 insertions(+)
create mode 100644 scripts/coccinelle/misc/swap.cocci

diff --git a/scripts/coccinelle/misc/swap.cocci b/scripts/coccinelle/misc/swap.cocci
new file mode 100644
index 0000000..0f0929c
--- /dev/null
+++ b/scripts/coccinelle/misc/swap.cocci
@@ -0,0 +1,62 @@
+/// Use swap macro instead of complete operation to simplify the code
+///
+// # Generated with sgen
+//
+// Confidence: Moderate
+// Copyright: (C) 2015 Fabian Frederick. GPLv2.
+
+virtual patch
+virtual context
+virtual org
+virtual report
+
+@r depends on patch && !context && !org && !report@
+identifier i1, i2, tmp;
+type t1;
+@@
+
+- t1 tmp;
+<+... when any
+- tmp = i1;
+- i1 = i2;
+- i2 = tmp;
++ swap(i1, i2);
+...+>
+
+
+// ----------------------------------------------------------------------------
+
+@r_context depends on !patch && (context || org || report)@
+type t1;
+identifier i1, i2, tmp;
+position j0, j1;
+@@
+
+* t1 tmp@j0;
+<+... when any
+* tmp@j1 = i1;
+* i1 = i2;
+* i2 = tmp;
+...+>
+
+// ----------------------------------------------------------------------------
+
+@script:python r_org depends on org@
+j0 << r_context.j0;
+j1 << r_context.j1;
+@@
+
+msg = "swap.cocci"
+coccilib.org.print_todo(j0[0], msg)
+coccilib.org.print_link(j1[0], "")
+
+// ----------------------------------------------------------------------------
+
+@script:python r_report depends on report@
+j0 << r_context.j0;
+j1 << r_context.j1;
+@@
+
+msg = "WARNING: use swap() and remove temporary variable if it's not used elsewhere around line %s." % (j1[0].line)
+coccilib.report.print_report(j0[0], msg)
+
--
2.4.0


\
 
 \ /
  Last update: 2015-05-19 21:21    [W:0.061 / U:0.260 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site