Skip to contentSkip to conversation

cocci: Add rules to find str_plural() replacements

PATCH Superseded Kees Cook 2 participants 4 messages Feb 15, 2024 Coccinelle/Semantic Patches (SmPL)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 cocci: Add rules to find str_plural() replacements

Add rules for finding places where str_plural() can be used. This currently finds:

 54 files changed, 62 insertions(+), 61 deletions(-)

---
Cc: Michal Wajdeczko <[email protected]>
Cc: Jani Nikula <[email protected]>
Cc: Andy Shevchenko <[email protected]>
Cc: Julia Lawall <[email protected]>
Cc: Nicolas Palix <[email protected]>
Cc: [email protected]

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
scripts/coccinelle/api/str_plural.cocci new file +41 -0
new file mode 100644
index 000000000000..c28cb6f7b803
@@ -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)
+
Signature · 1 line
2.34.1
#1 lore raw
Quoted 3 lines · Kees Cook

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 ?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Kees Cook

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?)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#3 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Kees Cook

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.

Signature · 1 line
Kees Cook