Find and suggest conversions of opencoded field modify patterns with
the wrapper FIELD_MODIFY() API defined in include/linux/bitfield.h
for catching the possible parameter type error in the compile time.
Signed-off-by: Luo Jie <quic_luoj@quicinc.com>
---
scripts/coccinelle/misc/field_modify.cocci | 61 ++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+)
diff --git a/scripts/coccinelle/misc/field_modify.cocci b/scripts/coccinelle/misc/field_modify.cocci
new file mode 100644
index 000000000000..48b00194a265
--- /dev/null
+++ b/scripts/coccinelle/misc/field_modify.cocci
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Replace below code with the wrapper FIELD_MODIFY(MASK, ®, val)
+/// - reg &= ~MASK;
+/// - reg |= FIELD_PREP(MASK, val);
+//
+// Confidence: High
+// Author: Luo Jie <quic_luoj@quicinc.com>
+// Copyright: (C) 2025 Qualcomm Innovation Center, Inc.
+// Keywords: FIELD_PREP, FIELD_MODIFY
+// Options: --include-headers
+
+virtual context
+virtual patch
+virtual org
+virtual report
+
+@ depends on context && !patch && !org && !report@
+identifier reg, val;
+constant mask;
+symbol FIELD_PREP;
+@@
+
+* reg &= ~mask;
+* reg |= FIELD_PREP(mask, val);
+
+@ depends on !context && patch && !org && !report @
+identifier reg, val;
+constant mask;
+symbol FIELD_PREP, FIELD_MODIFY;
+@@
+
+-reg &= ~mask;
+-reg |= FIELD_PREP(mask, val);
++FIELD_MODIFY(mask, ®, val);
+
+@r depends on !context && !patch && (org || report)@
+identifier reg, val;
+constant mask;
+symbol FIELD_PREP;
+position p;
+@@
+
+ reg &= ~mask;
+ reg |= FIELD_PREP@p(mask, val);
+
+@script:python depends on report@
+p << r.p;
+x << r.reg;
+@@
+
+msg="WARNING: Consider using FIELD_MODIFY helper on %s" % (x)
+coccilib.report.print_report(p[0], msg)
+
+@script:python depends on org@
+p << r.p;
+x << r.reg;
+@@
+
+msg="WARNING: Consider using FIELD_MODIFY helper on %s" % (x)
+msg_safe=msg.replace("[","@(").replace("]",")")
+coccilib.org.print_todo(p[0], msg_safe)
--
2.34.1
… > --- > scripts/coccinelle/misc/field_modify.cocci | 61 ++++++++++++++++++++++++++++++ … Did you overlook the addition of patch version descriptions? https://lore.kernel.org/all/?q=%22This+looks+like+a+new+version+of+a+previously+submitted+patch%22 https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.15#n310 … > +@ depends on context && !patch && !org && !report@ … I imagine that the condition selections can be simplified. … > +@script:python depends on report@ > +p << r.p; > +x << r.reg; > +@@ > + > +msg="WARNING: Consider using FIELD_MODIFY helper on %s" % (x) > +coccilib.report.print_report(p[0], msg) Do you know that a string construction can also be directly passed to such a function call? Regards, Markus
On 6/13/2025 12:48 AM, Markus Elfring wrote: > … >> --- >> scripts/coccinelle/misc/field_modify.cocci | 61 ++++++++++++++++++++++++++++++ > … > > Did you overlook the addition of patch version descriptions? > https://lore.kernel.org/all/?q=%22This+looks+like+a+new+version+of+a+previously+submitted+patch%22 > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.15#n310 Thank you for highlighting this, and for the references. A brief description of the differences in this script patch compared to the previous (v3) version were included in the cover letter, so a changelog was not added to the individual patch's commit message. Hope this approach is acceptable. I will ensure to include the reviewer in the CC list for future submissions, as recommended in the documentation. > > > … >> +@ depends on context && !patch && !org && !report@ > … > > I imagine that the condition selections can be simplified. > I agree that the condition selections can be simplified, I will update it to "@ depends on context@". Hope it is fine. > > … >> +@script:python depends on report@ >> +p << r.p; >> +x << r.reg; >> +@@ >> + >> +msg="WARNING: Consider using FIELD_MODIFY helper on %s" % (x) >> +coccilib.report.print_report(p[0], msg) > Do you know that a string construction can also be directly passed > to such a function call? I appreciate the tip. I'll update the patch to pass the formatted string directly to the function call in the next revision. > > Regards, > Markus
© 2016 - 2025 Red Hat, Inc.