Skip to contentSkip to conversation

coccinelle: boolconv: improve script to handle more cases

PATCH Superseded Aishwarya Pant 2 participants 2 messages Sep 23, 2017 Coccinelle/Semantic Patches (SmPL)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 coccinelle: boolconv: improve script to handle more cases

Given expressions A, B and a binary operator op, the current script checks for cases where
A op B ? true : false can be replaced by A op B

This can be extended to handle the other case where
A op B ? false : true can be replaced by !(A op B)

Moreover we can use the binary operator type to catch all desirable comparisons in one go instead of writing them down.

---

Okay I'm not sure if A op B ? false : true conversion to !(A op B) is even desirable because it might hinder code readability in certain cases. Just putting it out here because while I was running coccicheck in staging drivers a few cases like the above turned up.

---
 scripts/coccinelle/misc/boolconv.cocci | 43 ++++++++--------------------------
 1 file changed, 10 insertions(+), 33 deletions(-)
scripts/coccinelle/misc/boolconv.cocci +10 -33
index 33c464d..9b32b5f 100644
@@ -16,27 +16,19 @@ virtual report
 
 @depends on patch@
 expression A, B;
+binary operator op = {==,!=,>,<,>=,<=,&&,||};
 symbol true, false;
 @@
 
 (
-  A == B
-|
-  A != B
-|
-  A > B
-|
-  A < B
-|
-  A >= B
-|
-  A <= B
-|
-  A && B
+  A op B
+- ? true : false
 |
-  A || B
++ !(
+  A op B
++ )
+- ? false : true
 )
-- ? true : false
 
 //----------------------------------------------------------
 //  For context mode
@@ -44,28 +36,13 @@ symbol true, false;
 
 @r depends on !patch@
 expression A, B;
+binary operator op = {==,!=,>,<,>=,<=,&&,||};
 symbol true, false;
 position p;
 @@
 
-(
-  A == B
-|
-  A != B
-|
-  A > B
-|
-  A < B
-|
-  A >= B
-|
-  A <= B
-|
-  A && B
-|
-  A || B
-)
-* ? true : false@p
+  A op B
+* ? \(true\|false\) : \(false@p\|true@p\)
 
 //----------------------------------------------------------
 //  For org mode
Signature · 1 line
2.7.4
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Aishwarya Pant

Given expressions A, B and a binary operator op, the current script checks for cases where
A op B ? true : false can be replaced by A op B

This can be extended to handle the other case where
A op B ? false : true can be replaced by !(A op B)

Moreover we can use the binary operator type to catch all desirable comparisons in one go instead of writing them down.

Quoted 7 lines

---

Okay I'm not sure if A op B ? false : true conversion to !(A op B) is even desirable because it might hinder code readability in certain cases. Just putting it out here because while I was running coccicheck in staging drivers a few cases like the above turned up.

Maybe the user can decide what they want to do in this case.

Quoted 79 lines
---
 scripts/coccinelle/misc/boolconv.cocci | 43 ++++++++--------------------------
 1 file changed, 10 insertions(+), 33 deletions(-)
scripts/coccinelle/misc/boolconv.cocci +10 -34
index 33c464d..9b32b5f 100644
@@ -16,27 +16,19 @@ virtual report
 
 @depends on patch@
 expression A, B;
+binary operator op = {==,!=,>,<,>=,<=,&&,||};
 symbol true, false;
 @@
 
 (
-  A == B
-|
-  A != B
-|
-  A > B
-|
-  A < B
-|
-  A >= B
-|
-  A <= B
-|
-  A && B
+  A op B
+- ? true : false
 |
-  A || B
++ !(
+  A op B
++ )
+- ? false : true
 )
-- ? true : false
 
 //----------------------------------------------------------
 //  For context mode
@@ -44,28 +36,13 @@ symbol true, false;
 
 @r depends on !patch@
 expression A, B;
+binary operator op = {==,!=,>,<,>=,<=,&&,||};
 symbol true, false;
 position p;
 @@
 
-(
-  A == B
-|
-  A != B
-|
-  A > B
-|
-  A < B
-|
-  A >= B
-|
-  A <= B
-|
-  A && B
-|
-  A || B
-)
-* ? true : false@p
+  A op B
+* ? \(true\|false\) : \(false@p\|true@p\)
 
 //----------------------------------------------------------
 //  For org mode
--

2.7.4