Re: [cocci] [PATCH v3 2/6] coccinelle: misc: Add field_modify script

From: Luo Jie
Date: Mon May 19 2025 - 09:48:45 EST




On 4/24/2025 12:35 AM, Markus Elfring wrote:

-reg &= ~mask;
-reg |= FIELD_PREP
+       FIELD_MODIFY
                   (mask,
+                  &reg,
                    val
                   );

With this code variant, there is no space prior to &reg, here is the
example code changes generated by the SmPL code as below, is this expected?

+++ b/drivers/phy/starfive/phy-jh7110-dphy-tx.c
@@ -244,8 +244,7 @@ static int stf_dphy_configure(struct phy
        i = stf_dphy_get_config_index(bitrate);

        tmp = readl(dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(100));
-       tmp &= ~STF_DPHY_REFCLK_IN_SEL;
-       tmp |= FIELD_PREP(STF_DPHY_REFCLK_IN_SEL, STF_DPHY_REFCLK_12M);
+       FIELD_MODIFY(STF_DPHY_REFCLK_IN_SEL,&tmp, STF_DPHY_REFCLK_12M);
        writel(tmp, dphy->topsys + STF_DPHY_APBIFSAIF_SYSCFG(100));

The Coccinelle software is still evolving somehow.
Thus your test result can trigger further development considerations.
I hope that clarifications and corresponding improvements can be achieved
also according to such source code layout concerns.

Regards,
Markus

OK, understand. I will keep the original patch as below for now, as we
need to ensure the script generates code with the expected style.
+- reg &= ~mask;
+- reg |= FIELD_PREP(mask, val);
++ FIELD_MODIFY(mask, &reg, val);

In addition, below case is filed to request this improvement on the
coccinelle project.
https://github.com/coccinelle/coccinelle/issues/397

Thanks.