Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1128993pxb; Thu, 4 Mar 2021 04:11:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJzz08vTJyMtm0VRXpPw3OHhLbGGTEZ0d0yavbNDWb4aJyd/gMYV9M3j1P7oF/OKxOQph14R X-Received: by 2002:a05:6402:4314:: with SMTP id m20mr3851603edc.5.1614859894694; Thu, 04 Mar 2021 04:11:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614859894; cv=none; d=google.com; s=arc-20160816; b=oe32C+Nsc56spyWYZrmQmyk2uHU3RDH8ikELgHoMFHV7R3szpsZj53TQOIGA8vRSbX efpjFCiTdOqQGKYA+N1MVuTMBVx40tAOxzbS2ycnsbPf8Vg8NIS6YOYYNYZEOv4tavj7 QPR8t9pQpVcq2XqVmsxMsYpLpwrkFfkb9FtS0MPcqN2lrOUjo6Q9vGq/IH4PRrjAUM3J W1nRq5cvc954IAcB6m836QDZTg/NjYnZluzn/w/nSHmnjIT0dpbQhBX4Td11/Xt2WAZg cz/ZUVDs9cxFota8iph79kr6mofAP2jvxFmV6buDn1tswuvcMD6xNjyF7suRoXA7QMAj CIdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date; bh=Ulkwt1XTTuDbonmbfpjMToI6z2jlcyaiPMSZ6w1xs3U=; b=Vh/OME8hUrfzhYO/Tt3c7N6qOKlREEcA5vdHMgILcvyG6aNSBDiC/yU9of+CefG5AP +MOGPuRJlp65dUAUeYaamhYyGd072Te+BJ0cBzrR8IQSTDO5ZMs5tS7b2VWypYTulvnc E1z9WUz8CkHw72vRmTyWLIJ+oFFSl78zw826acf7XnSa4rro83/rzp+TQpAqz3D4oWaS qKHaihCwizc0F2TEEBL/GAvSCflAelwrGhYGVQWMLNaeZfxjrUuiDPI62RiZrfLqXzCl vsuYUn0GgvgGciG1MOnSYqcDWSOPqzmWofDKbNm1BKzYgwUcZbC9maTzQxZ/u3PXsZ+I u8Ug== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hr14si9300723ejc.394.2021.03.04.04.11.11; Thu, 04 Mar 2021 04:11:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1579642AbhCCSbp (ORCPT + 99 others); Wed, 3 Mar 2021 13:31:45 -0500 Received: from mail2-relais-roc.national.inria.fr ([192.134.164.83]:29916 "EHLO mail2-relais-roc.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346514AbhCCQil (ORCPT ); Wed, 3 Mar 2021 11:38:41 -0500 X-IronPort-AV: E=Sophos;i="5.81,220,1610406000"; d="scan'208";a="495951182" Received: from 173.121.68.85.rev.sfr.net (HELO hadrien) ([85.68.121.173]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Mar 2021 17:37:54 +0100 Date: Wed, 3 Mar 2021 17:37:54 +0100 (CET) From: Julia Lawall X-X-Sender: jll@hadrien To: Denis Efremov cc: cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] coccinelle: misc: add swap script In-Reply-To: <20210219092448.13760-1-efremov@linux.com> Message-ID: References: <20210216080133.455456-1-efremov@linux.com> <20210219092448.13760-1-efremov@linux.com> User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 19 Feb 2021, Denis Efremov wrote: > Check for opencoded swap() implementation. > > Signed-off-by: Denis Efremov > --- > Changes in v2: > - additional patch rule to drop excessive {} > - fix indentation in patch mode by anchoring ; > > scripts/coccinelle/misc/swap.cocci | 101 +++++++++++++++++++++++++++++ > 1 file changed, 101 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 000000000000..d5da9888c222 > --- /dev/null > +++ b/scripts/coccinelle/misc/swap.cocci > @@ -0,0 +1,101 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/// > +/// Check for opencoded swap() implementation. > +/// > +// Confidence: High > +// Copyright: (C) 2021 Denis Efremov ISPRAS > +// Options: --no-includes --include-headers > +// > +// Keywords: swap > +// > + > +virtual patch > +virtual org > +virtual report > +virtual context > + > +@r depends on !patch@ > +identifier tmp; > +expression a, b; > +type T; > +position p; > +@@ > + > +( > +* T tmp; > +| > +* T tmp = 0; > +| > +* T *tmp = NULL; > +) > +... when != tmp > +* tmp = a; > +* a = b;@p > +* b = tmp; > +... when != tmp > + > +@rpvar depends on patch@ > +identifier tmp; > +expression a, b; > +type T; > +@@ > + > +( > +- T tmp; > +| > +- T tmp = 0; > +| > +- T *tmp = NULL; > +) > +... when != tmp > +- tmp = a; > +- a = b; > +- b = tmp > ++ swap(a, b) > + ; > +... when != tmp > + > + > +@rp depends on patch@ > +identifier tmp; > +expression a, b; > +@@ > + > +- tmp = a; > +- a = b; > +- b = tmp > ++ swap(a, b) > + ; A rule like the above should also appear in the non-patch case. > + > +@depends on (rpvar || rp)@ This needs to be depends on patch && (rpvar || rp). It doesn't make much sense, because rpvar and rp both depend on patch, but at the moment that information isn't propagate everywhere that it is needed. thanks, julia > +@@ > + > +( > + for (...;...;...) > +- { > + swap(...); > +- } > +| > + while (...) > +- { > + swap(...); > +- } > +| > + if (...) > +- { > + swap(...); > +- } > +) > + > + > +@script:python depends on report@ > +p << r.p; > +@@ > + > +coccilib.report.print_report(p[0], "WARNING opportunity for swap()") > + > +@script:python depends on org@ > +p << r.p; > +@@ > + > +coccilib.org.print_todo(p[0], "WARNING opportunity for swap()") > -- > 2.26.2 > >