coccinelle: Add rules to find str_plural() replacements
authorKees Cook <keescook@chromium.org>
Thu, 15 Feb 2024 17:58:10 +0000 (09:58 -0800)
committerKees Cook <keescook@chromium.org>
Thu, 29 Feb 2024 21:38:01 +0000 (13:38 -0800)
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>
MAINTAINERS
scripts/coccinelle/api/string_choices.cocci [new file with mode: 0644]

index d0df728734c190c236d0b3e3b23c36be179ca153..216d02a3fed5b024e214972fe3f4ba5c6c823f59 100644 (file)
@@ -8979,6 +8979,7 @@ F:        lib/string.c
 F:     lib/string_helpers.c
 F:     lib/test-string_helpers.c
 F:     lib/test_string.c
+F:     scripts/coccinelle/api/string_choices.cocci
 
 GENERIC UIO DRIVER FOR PCI DEVICES
 M:     "Michael S. Tsirkin" <mst@redhat.com>
diff --git a/scripts/coccinelle/api/string_choices.cocci b/scripts/coccinelle/api/string_choices.cocci
new file mode 100644 (file)
index 0000000..a71966c
--- /dev/null
@@ -0,0 +1,41 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Find places to use string_choices.h's various helpers.
+//
+// Confidence: Medium
+// Options: --no-includes --include-headers
+virtual patch
+virtual context
+virtual report
+
+@str_plural depends on patch@
+expression E;
+@@
+(
+-      ((E == 1) ? "" : "s")
++      str_plural(E)
+|
+-      ((E != 1) ? "s" : "")
++      str_plural(E)
+|
+-      ((E > 1) ? "s" : "")
++      str_plural(E)
+)
+
+@str_plural_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 << str_plural_r.P;
+e << str_plural_r.E;
+@@
+
+coccilib.report.print_report(p[0], "opportunity for str_plural(%s)" % e)