Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7598C433DB for ; Tue, 16 Feb 2021 16:03:55 +0000 (UTC) Received: from isis.lip6.fr (isis.lip6.fr [132.227.60.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2E068614A5 for ; Tue, 16 Feb 2021 16:03:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2E068614A5 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=cocci-bounces@systeme.lip6.fr Received: from systeme.lip6.fr (systeme.lip6.fr [132.227.104.7]) by isis.lip6.fr (8.15.2/8.15.2) with ESMTP id 11GG3TVB029371; Tue, 16 Feb 2021 17:03:30 +0100 (CET) Received: from systeme.lip6.fr (systeme.lip6.fr [127.0.0.1]) by systeme.lip6.fr (Postfix) with ESMTP id AEC4377C0; Tue, 16 Feb 2021 17:03:29 +0100 (CET) Received: from isis.lip6.fr (isis.lip6.fr [132.227.60.2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by systeme.lip6.fr (Postfix) with ESMTPS id F35CA39D7 for ; Tue, 16 Feb 2021 17:03:27 +0100 (CET) Received: from mail-lj1-f169.google.com (mail-lj1-f169.google.com [209.85.208.169]) by isis.lip6.fr (8.15.2/8.15.2) with ESMTP id 11GG3QuD012334 for ; Tue, 16 Feb 2021 17:03:26 +0100 (CET) Received: by mail-lj1-f169.google.com with SMTP id b16so12432467lji.13 for ; Tue, 16 Feb 2021 08:03:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=o4/al9HbxUJ6dIQSP/cYBFZuT9Xf6OYChnIzkZS7oOg=; b=GJ7NZM55UwlKhf6+g9NP+yd9iUT54Td/TZE4lBs/zpmP4cs9kXaX0+shqFrVYVHXFQ tROpBbthwHc741N1kBYXHf1ijDi1zbR17z+P1mGNr/N4lW1IaZv8Gm3JpK6sdbI2v1eQ H2bCtCtQli7VxfjMfJ5T2B/KdVjCwLZAh0cGFuKxaRzS/eGgd/4SFRXzic4TL0vYpVLJ Qt6FMs5H9t11KlPREV5Nn4Xh58LoS3kKS2fFRtAcZjM5prJowNAImVgk4lFD9PzfQ1bC ag7oDvXi5EOGKUtJSIw17dgsp/err35+R+2vZkh94ciqSufDhWVLluJC2UkVKk71sAP4 3s5A== X-Gm-Message-State: AOAM532xvu5U8GjTaGNs+EpjqcaI9KEJzQfCegF4h9R9ykEHOFYABs0d DKQgR1Bcd7gW2snPxqriTvA= X-Google-Smtp-Source: ABdhPJx8IlxD7Zgl9Uoi0VptdlQbdJH7lD+YsyE/rrdvhDOak+7SeCbwS4RJJ2wi87BXJ2RueZYGRA== X-Received: by 2002:a05:651c:2125:: with SMTP id a37mr12855346ljq.19.1613491406542; Tue, 16 Feb 2021 08:03:26 -0800 (PST) Received: from localhost.localdomain (broadband-188-32-236-56.ip.moscow.rt.ru. [188.32.236.56]) by smtp.googlemail.com with ESMTPSA id y19sm992465lji.31.2021.02.16.08.03.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Feb 2021 08:03:25 -0800 (PST) From: Denis Efremov To: Julia Lawall Date: Tue, 16 Feb 2021 19:03:26 +0300 Message-Id: <20210216160326.1341741-1-efremov@linux.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 X-Greylist: Sender IP whitelisted, Sender e-mail whitelisted, not delayed by milter-greylist-4.4.3 (isis.lip6.fr [132.227.60.2]); Tue, 16 Feb 2021 17:03:31 +0100 (CET) X-Greylist: Sender DNS name whitelisted, not delayed by milter-greylist-4.4.3 (isis.lip6.fr [132.227.60.2]); Tue, 16 Feb 2021 17:03:26 +0100 (CET) X-Scanned-By: MIMEDefang 2.78 on 132.227.60.2 X-Scanned-By: MIMEDefang 2.78 on 132.227.60.2 Cc: cocci@systeme.lip6.fr, linux-kernel@vger.kernel.org Subject: [Cocci] [PATCH] coccinelle: misc: add minmax script X-BeenThere: cocci@systeme.lip6.fr X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: cocci-bounces@systeme.lip6.fr Errors-To: cocci-bounces@systeme.lip6.fr Check for opencoded min(), max() implementations. Signed-off-by: Denis Efremov --- scripts/coccinelle/misc/minmax.cocci | 198 +++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 scripts/coccinelle/misc/minmax.cocci diff --git a/scripts/coccinelle/misc/minmax.cocci b/scripts/coccinelle/misc/minmax.cocci new file mode 100644 index 000000000000..9ae689bb14fb --- /dev/null +++ b/scripts/coccinelle/misc/minmax.cocci @@ -0,0 +1,198 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// +/// Check for opencoded min(), max() implementations. +/// Generated patches sometimes require adding a cast to fix compile warning. +/// Warnings/patches scope intentionally limited to a function body. +/// +// Confidence: Medium +// Copyright: (C) 2021 Denis Efremov ISPRAS +// Options: --no-includes --include-headers +// +// Keywords: min, max +// + + +virtual report +virtual org +virtual context +virtual patch + +@rmax depends on !patch@ +identifier func; +expression x, y; +binary operator cmp = {>, >=}; +position p; +@@ + +func(...) +{ + ... when any +* (x cmp y) ?@p x : y + ... when any +} + +@rmaxif depends on !patch@ +identifier func; +expression x, y; +expression max_val; +binary operator cmp = {>, >=}; +position p; +@@ + +func(...) +{ + ... when any +* if (x cmp@p y) { +* max_val = x; +* } else { +* max_val = y; +* } + ... when any +} + +@rmin depends on !patch@ +identifier func; +expression x, y; +binary operator cmp = {<, <=}; +position p; +@@ + +func(...) +{ + ... when any +* (x cmp y) ?@p x : y + ... when any +} + +@rminif depends on !patch@ +identifier func; +expression x, y; +expression min_val; +binary operator cmp = {<, <=}; +position p; +@@ + +func(...) +{ + ... when any +* if (x cmp@p y) { +* min_val = x; +* } else { +* min_val = y; +* } + ... when any +} + +@depends on patch@ +identifier func; +expression x, y; +binary operator cmp = {>=, >}; +@@ + +func(...) +{ + ... when any +- (x cmp y) ? x : y ++ max(x, y) + ... when any +} + +@depends on patch@ +identifier func; +expression x, y; +expression max_val; +binary operator cmp = {>=, >}; +@@ + +func(...) +{ + ... when any +- if (x cmp y) { +- max_val = x; +- } else { +- max_val = y; +- } ++ max_val = max(x, y); + ... when any +} + +@depends on patch@ +identifier func; +expression x, y; +binary operator cmp = {<=, <}; +@@ + +func(...) +{ + ... when any +- (x cmp y) ? x : y ++ min(x, y) + ... when any +} + +@depends on patch@ +identifier func; +expression x, y; +expression min_val; +binary operator cmp = {<=, <}; +@@ + +func(...) +{ + ... when any +- if (x cmp y) { +- min_val = x; +- } else { +- min_val = y; +- } ++ min_val = min(x, y); + ... when any +} + +@script:python depends on report@ +p << rmax.p; +@@ + +coccilib.report.print_report(p[0], "WARNING opportunity for max()") + +@script:python depends on org@ +p << rmax.p; +@@ + +coccilib.report.print_todo(p[0], "WARNING opportunity for max()") + +@script:python depends on report@ +p << rmaxif.p; +@@ + +coccilib.report.print_report(p[0], "WARNING opportunity for max()") + +@script:python depends on org@ +p << rmaxif.p; +@@ + +coccilib.report.print_todo(p[0], "WARNING opportunity for max()") + +@script:python depends on report@ +p << rmin.p; +@@ + +coccilib.report.print_report(p[0], "WARNING opportunity for min()") + +@script:python depends on org@ +p << rmin.p; +@@ + +coccilib.report.print_todo(p[0], "WARNING opportunity for min()") + +@script:python depends on report@ +p << rminif.p; +@@ + +coccilib.report.print_report(p[0], "WARNING opportunity for min()") + +@script:python depends on org@ +p << rminif.p; +@@ + +coccilib.report.print_todo(p[0], "WARNING opportunity for min()") -- 2.26.2 _______________________________________________ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci