Skip to content
/ linux Public
forked from torvalds/linux

Commit

Permalink
coccinelle: Add rules to find str_plural() replacements
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Michal Wajdeczko <[email protected]>
Link: https://lore.kernel.org/all/[email protected]/
Signed-off-by: Kees Cook <[email protected]>
  • Loading branch information
kees committed Feb 29, 2024
1 parent 9ca5fac commit 1d02f25
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -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" <[email protected]>
Expand Down
41 changes: 41 additions & 0 deletions scripts/coccinelle/api/string_choices.cocci
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 1d02f25

Please sign in to comment.