diff mbox series

cocci: Add rules to find str_plural() replacements

Message ID 20240215180156.work.548-kees@kernel.org (mailing list archive)
State Superseded
Headers show
Series cocci: Add rules to find str_plural() replacements | expand

Commit Message

Kees Cook Feb. 15, 2024, 6:02 p.m. UTC
Add rules for finding places where str_plural() can be used. This
currently finds:
 54 files changed, 62 insertions(+), 61 deletions(-)

Co-developed-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
Link: https://lore.kernel.org/all/fc1b25a8-6381-47c2-831c-ab6b8201a82b@intel.com/
Signed-off-by: Kees Cook <keescook@chromium.org>
---
Cc: Michal Wajdeczko <michal.wajdeczko@intel.com>
Cc: Jani Nikula <jani.nikula@intel.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Julia Lawall <Julia.Lawall@inria.fr>
Cc: Nicolas Palix <nicolas.palix@imag.fr>
Cc: cocci@inria.fr

Since ()s are deduplicated by Coccinelle I removed the duplicate rules,
wrapped the final rules in ()s to catch those cases, and added a "!= 1"
case.
---
 scripts/coccinelle/api/str_plural.cocci | 41 +++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 scripts/coccinelle/api/str_plural.cocci

Comments

Andy Shevchenko Feb. 15, 2024, 7:33 p.m. UTC | #1
On Thu, Feb 15, 2024 at 10:02:00AM -0800, Kees Cook wrote:
> Add rules for finding places where str_plural() can be used. This
> currently finds:
>  54 files changed, 62 insertions(+), 61 deletions(-)

Can we extend this to cover string_choices and call the script probably
str_choices.cocci ?
Andy Shevchenko Feb. 15, 2024, 7:34 p.m. UTC | #2
On Thu, Feb 15, 2024 at 09:33:31PM +0200, Andy Shevchenko wrote:
> On Thu, Feb 15, 2024 at 10:02:00AM -0800, Kees Cook wrote:
> > Add rules for finding places where str_plural() can be used. This
> > currently finds:
> >  54 files changed, 62 insertions(+), 61 deletions(-)
> 
> Can we extend this to cover string_choices and call the script probably
> str_choices.cocci ?

(And add it to MAINTAINERS?)
Kees Cook Feb. 15, 2024, 7:44 p.m. UTC | #3
On Thu, Feb 15, 2024 at 09:34:01PM +0200, Andy Shevchenko wrote:
> On Thu, Feb 15, 2024 at 09:33:31PM +0200, Andy Shevchenko wrote:
> > On Thu, Feb 15, 2024 at 10:02:00AM -0800, Kees Cook wrote:
> > > Add rules for finding places where str_plural() can be used. This
> > > currently finds:
> > >  54 files changed, 62 insertions(+), 61 deletions(-)
> > 
> > Can we extend this to cover string_choices and call the script probably
> > str_choices.cocci ?
> 
> (And add it to MAINTAINERS?)

Sure! I'll send a v2.
diff mbox series

Patch

diff --git a/scripts/coccinelle/api/str_plural.cocci b/scripts/coccinelle/api/str_plural.cocci
new file mode 100644
index 000000000000..c28cb6f7b803
--- /dev/null
+++ b/scripts/coccinelle/api/str_plural.cocci
@@ -0,0 +1,41 @@ 
+// SPDX-License-Identifier: GPL-2.0-only
+// Find places to use string_choices.h's str_plural() helper.
+//
+// Confidence: Medium
+virtual patch
+virtual context
+virtual report
+
+@depends on patch@
+expression E;
+@@
+(
+-	((E == 1) ? "" : "s")
++	str_plural(E)
+|
+-	((E != 1) ? "s" : "")
++	str_plural(E)
+|
+-	((E > 1) ? "s" : "")
++	str_plural(E)
+)
+
+@r depends on !patch exists@
+expression E;
+position P;
+@@
+(
+*	((E@P == 1) ? "" : "s")
+|
+*	((E@P != 1) ? "s" : "")
+|
+*	((E@P > 1) ? "s" : "")
+)
+
+@script:python depends on report@
+p << r.P;
+e << r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_plural(%s)" % e)
+