Skip to contentSkip to conversation

coccinelle: misc: add excluded_middle.cocci script

PATCH Superseded Denis Efremov 3 participants 5 messages Sep 2, 2020 – Sep 21, 2020 Coccinelle/Semantic Patches (SmPL)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 coccinelle: misc: add excluded_middle.cocci script

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

---
 scripts/coccinelle/misc/excluded_middle.cocci | 40 +++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 scripts/coccinelle/misc/excluded_middle.cocci
scripts/coccinelle/misc/excluded_middle.cocci new file +40 -0
new file mode 100644
index 000000000000..1b8c20f13966
@@ -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")
Signature · 6 lines
2.26.2

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
#1 lore raw
Quoted 16 lines · Denis Efremov

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

---
 scripts/coccinelle/misc/excluded_middle.cocci | 40 +++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 scripts/coccinelle/misc/excluded_middle.cocci
scripts/coccinelle/misc/excluded_middle.cocci new file +0 -0
new file mode 100644
index 000000000000..1b8c20f13966

scripts/coccinelle/misc/excluded_middle.cocci line 3

Quoted 3 lines
scripts/coccinelle/misc/excluded_middle.cocci new file +3 -0
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-only
+///
+/// Condition "!A || A && B" is equalent to "!A || B".

s/equalent/equivalent/

Quoted 44 lines
scripts/coccinelle/misc/excluded_middle.cocci new file +37 -1
+///
+// 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

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

Signature · 5 lines
Jim
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
maintainer
#2 lore raw
In reply to Denis Efremov
Quoted 15 lines · Denis Efremov

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

---
 scripts/coccinelle/misc/excluded_middle.cocci | 40 +++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 scripts/coccinelle/misc/excluded_middle.cocci
scripts/coccinelle/misc/excluded_middle.cocci new file +0 -0
new file mode 100644
index 000000000000..1b8c20f13966

scripts/coccinelle/misc/excluded_middle.cocci line 3

Quoted 3 lines
scripts/coccinelle/misc/excluded_middle.cocci new file +3 -0
@@ -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.

Quoted 6 lines
scripts/coccinelle/misc/excluded_middle.cocci new file +6 -0
+///
+// Confidence: High
+// Copyright: (C) 2020 Denis Efremov ISPRAS
+// Options: --no-includes --include-headers
+
+virtual patch

scripts/coccinelle/misc/excluded_middle.cocci line 19

Quoted 10 lines
scripts/coccinelle/misc/excluded_middle.cocci new file +10 -0
+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.

scripts/coccinelle/misc/excluded_middle.cocci line 28

Quoted 9 lines
scripts/coccinelle/misc/excluded_middle.cocci new file +9 -0
+
+@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.

scripts/coccinelle/misc/excluded_middle.cocci line 40

Quoted 11 lines
scripts/coccinelle/misc/excluded_middle.cocci new file +11 -0
+@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

#3 lore raw

PATCH v2 1/1 coccinelle: misc: add excluded_middle.cocci script

In reply to Denis Efremov

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

---
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
scripts/coccinelle/misc/excluded_middle.cocci new file +39 -0
new file mode 100644
index 000000000000..ab28393e4843
@@ -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")
Signature · 1 line
2.26.2
maintainer Applied
#4 lore raw
Quoted 3 lines · Denis Efremov

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

Applied, thanks.

Quoted 60 lines

---
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
scripts/coccinelle/misc/excluded_middle.cocci new file +39 -1
new file mode 100644
index 000000000000..ab28393e4843
@@ -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