Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755301AbbESSfT (ORCPT ); Tue, 19 May 2015 14:35:19 -0400 Received: from mailrelay116.isp.belgacom.be ([195.238.20.143]:45819 "EHLO mailrelay116.isp.belgacom.be" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754952AbbESSfP (ORCPT ); Tue, 19 May 2015 14:35:15 -0400 X-Belgacom-Dynamic: yes X-Cloudmark-SP-Filtered: true X-Cloudmark-SP-Result: v=1.1 cv=Ax9Kd9og7W2htzcJF9odWb+87PP6pPoG1t8Q07ebhfo= c=1 sm=2 a=05oQ047nAAAA:8 a=yjlXrIyeW0nd9E4ujrsA:9 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2AJBwAJgVtV/9cAQFdcgxCBMrMACAECBQGBBJEAh1CBQzoTAQEBAQEBAYEKhH8jgRo3iDAB1AGGFoopHYQXBYZllwWXECNhZgGCMjwxgQMkgSABAQE From: Fabian Frederick To: linux-kernel@vger.kernel.org Cc: Fabian Frederick , Julia Lawall , Gilles Muller , Nicolas Palix , Michal Marek , cocci@systeme.lip6.fr Subject: [PATCH 1/1] scripts/coccinelle/misc: add swap.cocci Date: Tue, 19 May 2015 20:35:01 +0200 Message-Id: <1432060501-9463-1-git-send-email-fabf@skynet.be> X-Mailer: git-send-email 2.4.0 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2231 Lines: 100 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 --- 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 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/