Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp7185097pxb; Thu, 18 Feb 2021 03:55:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJwgLJa2C+AsZnnsXbdUSF0QA8w4QiMcifnvUCUrvaYOJdbFn54v3etfTvaI2rIUK1BWdPZD X-Received: by 2002:aa7:cd51:: with SMTP id v17mr3919241edw.194.1613649328529; Thu, 18 Feb 2021 03:55:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613649328; cv=none; d=google.com; s=arc-20160816; b=kMec0Zv/kmlrrdpsmc9WjFak6DR+dolL8f6wD8/TRUvKja/9svb9KG4D4kJhiK3thW epzy3OIUSzfmee2ZkIydogmHuYnAHOG+Lx0XbFBIzLzsi6g+Llhqe5P53zPXgInH9gdM wWvCxPAWc3BKIeKnRtlmShqUR8d5a+M7TpnL3Es5WuwzEc5sJnq+ziVJDFoJYTq1366x v+bKzyDsdOum8ctzpKloUNmFYtKJaeJmmig50VJVE/kw+HdBfQzkwrO7tYubt55B2f7o 7NXDhkZuayjAVekJApaoo8y2IxEHCo1DWMgKgrRrhC+fAkjeQFj2h0mogIK4GGdRxNPs aLBQ== 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=+o2VfQtYhwkA9Ua0Vt1xHRwsGFLO8pLBlm1JsdERVfs=; b=UQBoU9L1jEo+VqUu4xRe5zHtIIY1x8K+VPHsxuHoNih2oP9SOaQaEC/xJbfnEy2B0G H/lBmZRDuIXiNwSYaViGweuDngfMi3HpGkBA+HQjz/y6AM5r7VqpD7Cl7A0oE38HL+/u 928nviH0sicf88ZHvBxcYHRtJpafq2sVEdfz2n6mUxVD74NUJdBtnI5Uc2BJ8nLzISer hGhr6wHvf8Euz32HwDJi5PnxqTeVxoxJyzrmzW3I14+k1kmljY8Uc6vWLZLW/mJ4vkAn VcvU907l3MJwVn0hM7aZR4+Z7Vy2OF/A1Pa9qLRtk6V+kow4Rw/lMgINMVlL089QYLUM au8w== 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 l20si3667717eje.712.2021.02.18.03.55.04; Thu, 18 Feb 2021 03:55:28 -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 S231340AbhBRLxO (ORCPT + 99 others); Thu, 18 Feb 2021 06:53:14 -0500 Received: from mail3-relais-sop.national.inria.fr ([192.134.164.104]:29880 "EHLO mail3-relais-sop.national.inria.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232715AbhBRKRs (ORCPT ); Thu, 18 Feb 2021 05:17:48 -0500 X-IronPort-AV: E=Sophos;i="5.81,186,1610406000"; d="scan'208";a="373421670" Received: from 173.121.68.85.rev.sfr.net (HELO hadrien) ([85.68.121.173]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Feb 2021 11:17:02 +0100 Date: Thu, 18 Feb 2021 11:17:02 +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] coccinelle: misc: add swap script In-Reply-To: Message-ID: References: <20210216080133.455456-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 Thu, 18 Feb 2021, Denis Efremov wrote: > > > On 2/18/21 12:31 AM, Julia Lawall wrote: > >> +@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 > > > > In this rule and the next one, if you remove the final ; from the b = tmp > > line and from the swap line, and put it into context code afterwards, them > > the generated code looks better on cases like fs/xfs/xfs_inode.c in the > > function xfs_lock_two_inodes where two successive swap calls are > > generated. > > > > There are also some cases such as drivers/net/wireless/ath/ath5k/phy.c in > > the function ath5k_hw_get_median_noise_floor where the swap code makes up > > a whole if branch. > > > In this cases it would be good to remove the {}. > > How this can be handled? > > If I use this pattern: > @depends on patch@ > identifier tmp; > expression a, b; > @@ > > ( > if (...) > - { > - tmp = a; > - a = b; > - b = tmp > + swap(a, b) > ; > - } > | > - tmp = a; > - a = b; > - b = tmp > + swap(a, b) > ; > ) > > The tool fails with error: > > EXN: Failure("rule starting on line 58: already tagged token:\nC code > context\nFile \"drivers/net/wireless/ath/ath5k/phy.c\", line 1574, > column 4, charpos = 41650\n around = 'sort',\n whole content = > \t\t\t\tsort[j - 1] = tmp;") in drivers/net/wireless/ath/ath5k/phy.c A disjunction basically says "at this node in the cfg, can I match the first patter, or can I match the second pattern, etc." Unfortunately in this case the two branches start matching at different nodes, so the short circuit aspect of a disjunction isn't used, and it matches both patterns. The solution is to just make two rules. The first for the if case and the second for everything else. julia