2020-09-02 15:37:31

by Denis Efremov

[permalink] [raw]
Subject: [PATCH] coccinelle: misc: add excluded_middle.cocci script

Check for "!A || A && B" condition. It's equivalent to
"!A || B" condition.

Signed-off-by: Denis Efremov <[email protected]>
---
scripts/coccinelle/misc/excluded_middle.cocci | 40 +++++++++++++++++++
1 file changed, 40 insertions(+)
create mode 100644 scripts/coccinelle/misc/excluded_middle.cocci

diff --git a/scripts/coccinelle/misc/excluded_middle.cocci b/scripts/coccinelle/misc/excluded_middle.cocci
new file mode 100644
index 000000000000..1b8c20f13966
--- /dev/null
+++ b/scripts/coccinelle/misc/excluded_middle.cocci
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Condition "!A || A && B" is equalent to "!A || B".
+///
+// Confidence: High
+// Copyright: (C) 2020 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+
+virtual patch
+virtual context
+virtual org
+virtual report
+
+@r depends on !patch@
+expression A, B;
+position p;
+@@
+
+* !A || (A && B)@p
+
+@depends on patch@
+expression A, B;
+@@
+
+ !A ||
+- (A &&
+ B
+- )
+
+@script:python depends on report@
+p << r.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING condition !A || A && B is equivalent to !A || B")
+
+@script:python depends on org@
+p << r.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING condition !A || A && B is equivalent to !A || B")
--
2.26.2


2020-09-04 20:40:20

by Julia Lawall

[permalink] [raw]
Subject: Re: [PATCH] coccinelle: misc: add excluded_middle.cocci script



On Wed, 2 Sep 2020, Denis Efremov wrote:

> Check for "!A || A && B" condition. It's equivalent to
> "!A || B" condition.
>
> Signed-off-by: Denis Efremov <[email protected]>
> ---
> scripts/coccinelle/misc/excluded_middle.cocci | 40 +++++++++++++++++++
> 1 file changed, 40 insertions(+)
> create mode 100644 scripts/coccinelle/misc/excluded_middle.cocci
>
> diff --git a/scripts/coccinelle/misc/excluded_middle.cocci b/scripts/coccinelle/misc/excluded_middle.cocci
> new file mode 100644
> index 000000000000..1b8c20f13966
> --- /dev/null
> +++ b/scripts/coccinelle/misc/excluded_middle.cocci
> @@ -0,0 +1,40 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +///
> +/// Condition "!A || A && B" is equalent to "!A || B".

As was noted, there is a spelling mistake here.

> +///
> +// Confidence: High
> +// Copyright: (C) 2020 Denis Efremov ISPRAS
> +// Options: --no-includes --include-headers
> +
> +virtual patch
> +virtual context
> +virtual org
> +virtual report
> +
> +@r depends on !patch@
> +expression A, B;
> +position p;
> +@@
> +
> +* !A || (A && B)@p

In org mode, the token with the position variable gets highlighted in
color. It would be more meaningful to put the position variable on the &&
operator.

> +
> +@depends on patch@
> +expression A, B;
> +@@
> +
> + !A ||
> +- (A &&
> + B
> +- )

I found it better to say

- (A && B)
+ B

There are some cases where B should be moved one space to the left and by
removing B and adding it back again that will happen. I didn't see any
case where the result was less good.

> +@script:python depends on report@
> +p << r.p;
> +@@
> +
> +coccilib.report.print_report(p[0], "WARNING condition !A || A && B is equivalent to !A || B")
> +
> +@script:python depends on org@
> +p << r.p;
> +@@
> +
> +coccilib.org.print_todo(p[0], "WARNING condition !A || A && B is equivalent to !A || B")

I'm not sure that you need the word condition, but up to you.

julia

2020-09-21 15:33:22

by Denis Efremov

[permalink] [raw]
Subject: [PATCH v2] coccinelle: misc: add excluded_middle.cocci script

Check for !A || A && B condition. It's equivalent to !A || B.

Signed-off-by: Denis Efremov <[email protected]>
---
Changes in v2:
- spelling mistake fixed
- position variable moved on the && operator
- patch pattern changed to - (A && B)
- word "condition" removed from warning message

scripts/coccinelle/misc/excluded_middle.cocci | 39 +++++++++++++++++++
1 file changed, 39 insertions(+)
create mode 100644 scripts/coccinelle/misc/excluded_middle.cocci

diff --git a/scripts/coccinelle/misc/excluded_middle.cocci b/scripts/coccinelle/misc/excluded_middle.cocci
new file mode 100644
index 000000000000..ab28393e4843
--- /dev/null
+++ b/scripts/coccinelle/misc/excluded_middle.cocci
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Condition !A || A && B is equivalent to !A || B.
+///
+// Confidence: High
+// Copyright: (C) 2020 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+
+virtual patch
+virtual context
+virtual org
+virtual report
+
+@r depends on !patch@
+expression A, B;
+position p;
+@@
+
+* !A || (A &&@p B)
+
+@depends on patch@
+expression A, B;
+@@
+
+ !A ||
+- (A && B)
++ B
+
+@script:python depends on report@
+p << r.p;
+@@
+
+coccilib.report.print_report(p[0], "WARNING !A || A && B is equivalent to !A || B")
+
+@script:python depends on org@
+p << r.p;
+@@
+
+coccilib.org.print_todo(p[0], "WARNING !A || A && B is equivalent to !A || B")
--
2.26.2

2020-09-21 17:49:17

by Julia Lawall

[permalink] [raw]
Subject: Re: [PATCH v2] coccinelle: misc: add excluded_middle.cocci script



On Mon, 21 Sep 2020, Denis Efremov wrote:

> Check for !A || A && B condition. It's equivalent to !A || B.
>
> Signed-off-by: Denis Efremov <[email protected]>

Applied, thanks.

> ---
> Changes in v2:
> - spelling mistake fixed
> - position variable moved on the && operator
> - patch pattern changed to - (A && B)
> - word "condition" removed from warning message
>
> scripts/coccinelle/misc/excluded_middle.cocci | 39 +++++++++++++++++++
> 1 file changed, 39 insertions(+)
> create mode 100644 scripts/coccinelle/misc/excluded_middle.cocci
>
> diff --git a/scripts/coccinelle/misc/excluded_middle.cocci b/scripts/coccinelle/misc/excluded_middle.cocci
> new file mode 100644
> index 000000000000..ab28393e4843
> --- /dev/null
> +++ b/scripts/coccinelle/misc/excluded_middle.cocci
> @@ -0,0 +1,39 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +///
> +/// Condition !A || A && B is equivalent to !A || B.
> +///
> +// Confidence: High
> +// Copyright: (C) 2020 Denis Efremov ISPRAS
> +// Options: --no-includes --include-headers
> +
> +virtual patch
> +virtual context
> +virtual org
> +virtual report
> +
> +@r depends on !patch@
> +expression A, B;
> +position p;
> +@@
> +
> +* !A || (A &&@p B)
> +
> +@depends on patch@
> +expression A, B;
> +@@
> +
> + !A ||
> +- (A && B)
> ++ B
> +
> +@script:python depends on report@
> +p << r.p;
> +@@
> +
> +coccilib.report.print_report(p[0], "WARNING !A || A && B is equivalent to !A || B")
> +
> +@script:python depends on org@
> +p << r.p;
> +@@
> +
> +coccilib.org.print_todo(p[0], "WARNING !A || A && B is equivalent to !A || B")
> --
> 2.26.2
>
>