Skip to contentSkip to conversation

Coccinelle: Add misc/boolconv.cocci

PATCH Superseded Andrew F. Davis 3 participants 4 messages Jun 23, 2016 – Jun 26, 2016 Coccinelle/Semantic Patches (SmPL)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Coccinelle: Add misc/boolconv.cocci

Add a script to check for unneeded conversions to bool.

---
 scripts/coccinelle/misc/boolconv.cocci | 90 ++++++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
 create mode 100644 scripts/coccinelle/misc/boolconv.cocci
scripts/coccinelle/misc/boolconv.cocci new file +90 -0
new file mode 100644
index 0000000..33c464d
@@ -0,0 +1,90 @@
+/// Remove unneeded conversion to bool
+///
+//# Relational and logical operators evaluate to bool,
+//# explicit conversion is overly verbose and unneeded.
+//
+// Copyright: (C) 2016 Andrew F. Davis <[email protected]> GPLv2.
+
+virtual patch
+virtual context
+virtual org
+virtual report
+
+//----------------------------------------------------------
+//  For patch mode
+//----------------------------------------------------------
+
+ at depends on patch@
+expression A, B;
+symbol true, false;
+@@
+
+(
+  A == B
+|
+  A != B
+|
+  A > B
+|
+  A < B
+|
+  A >= B
+|
+  A <= B
+|
+  A && B
+|
+  A || B
+)
+- ? true : false
+
+//----------------------------------------------------------
+//  For context mode
+//----------------------------------------------------------
+
+@r depends on !patch@
+expression A, B;
+symbol true, false;
+position p;
+@@
+
+(
+  A == B
+|
+  A != B
+|
+  A > B
+|
+  A < B
+|
+  A >= B
+|
+  A <= B
+|
+  A && B
+|
+  A || B
+)
+* ? true : false at p
+
+//----------------------------------------------------------
+//  For org mode
+//----------------------------------------------------------
+
+ at script:python depends on r&&org@
+p << r.p;
+@@
+
+msg = "WARNING: conversion to bool not needed here"
+coccilib.org.print_todo(p[0], msg)
+
+//----------------------------------------------------------
+//  For report mode
+//----------------------------------------------------------
+
+@script:python depends on r&&report@
+p << r.p;
+@@
+
+msg = "WARNING: conversion to bool not needed here"
+coccilib.report.print_report(p[0], msg)
Signature · 1 line
2.9.0
#1 lore raw
Quoted 1 line · Andrew F. Davis

Add a script to check for unneeded conversions to bool.

this could also be extended for bool functions that:

	return !!expression;
and
	return expression != 0;
#2 lore raw
Quoted 9 lines · Joe Perches
Quoted 1 line · Andrew F. Davis

Add a script to check for unneeded conversions to bool.

this could also be extended for bool functions that:

	return !!expression;
and
	return expression != 0;

Agreed, it's in the works, I was going to post this current set with some patches to the kernel that is detects, then add additional checks and their kernel fixes incrementally.

For the second suggestion I think we could have additions to boolinit.cocci that include the results of comparison/logical operations.

Thanks,
Andrew

Acked-by on v1 1/1
#3 lore raw
In reply to Andrew F. Davis
Quoted 3 lines · Andrew F. Davis

Add a script to check for unneeded conversions to bool.

Quoted 105 lines
---
 scripts/coccinelle/misc/boolconv.cocci | 90 ++++++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
 create mode 100644 scripts/coccinelle/misc/boolconv.cocci
scripts/coccinelle/misc/boolconv.cocci new file +90 -0
new file mode 100644
index 0000000..33c464d
@@ -0,0 +1,90 @@
+/// Remove unneeded conversion to bool
+///
+//# Relational and logical operators evaluate to bool,
+//# explicit conversion is overly verbose and unneeded.
+//
+// Copyright: (C) 2016 Andrew F. Davis <[email protected]> GPLv2.
+
+virtual patch
+virtual context
+virtual org
+virtual report
+
+//----------------------------------------------------------
+//  For patch mode
+//----------------------------------------------------------
+
+ at depends on patch@
+expression A, B;
+symbol true, false;
+@@
+
+(
+  A == B
+|
+  A != B
+|
+  A > B
+|
+  A < B
+|
+  A >= B
+|
+  A <= B
+|
+  A && B
+|
+  A || B
+)
+- ? true : false
+
+//----------------------------------------------------------
+//  For context mode
+//----------------------------------------------------------
+
+ at r depends on !patch@
+expression A, B;
+symbol true, false;
+position p;
+@@
+
+(
+  A == B
+|
+  A != B
+|
+  A > B
+|
+  A < B
+|
+  A >= B
+|
+  A <= B
+|
+  A && B
+|
+  A || B
+)
+* ? true : false at p
+
+//----------------------------------------------------------
+//  For org mode
+//----------------------------------------------------------
+
+ at script:python depends on r&&org@
+p << r.p;
+@@
+
+msg = "WARNING: conversion to bool not needed here"
+coccilib.org.print_todo(p[0], msg)
+
+//----------------------------------------------------------
+//  For report mode
+//----------------------------------------------------------
+
+ at script:python depends on r&&report@
+p << r.p;
+@@
+
+msg = "WARNING: conversion to bool not needed here"
+coccilib.report.print_report(p[0], msg)
Signature · 1 line
2.9.0