[U-Boot] [PATCH] treewide: compress lines for immediate return

Masahiro Yamada yamada.masahiro at socionext.com
Sun Sep 4 01:38:20 CEST 2016


2016-09-03 2:15 GMT+09:00 Stephen Warren <swarren at wwwdotorg.org>:
> On 09/02/2016 04:36 AM, Masahiro Yamada wrote:
>>
>>   -ret = expression;
>>   -if (ret)
>>   -        return ret;
>>   -return 0;
>>   +return expression;
>
>
> I disagree with this change if applied blindly; I think both coding styles
> have their merit depending on the semantic context.
>
> In the case of a simple wrapper function that just calls another with
> different arguments, directly returning the result from the called function
> make sense. For example:
>
>> int device_bind(struct udevice *parent, const struct driver *drv,
>>                 const char *name, void *platdata, int of_offset,
>>                 struct udevice **devp)
>> {
>>         return device_bind_common(parent, drv, name, platdata, 0,
>> of_offset, 0,
>>                                   devp);
>> }
>
>
> However, where the top-level function is more complex, especially where it
> calls multiple functions in its body, I think it's best to use the exact
> same style to call all functions in the top-level body, and just "return 0"
> separately at the end. For example:
>
>> static int tegra186_bpmp_bind(struct udevice *dev)
>> {
>>         int ret;
>>         struct udevice *child;
>>
>>         debug("%s(dev=%p)\n", __func__, dev);
>>
>>         ret = device_bind_driver_to_node(dev, "tegra186_clk",
>> "tegra186_clk",
>>                                          dev->of_offset, &child);
>>         if (ret)
>>                 return ret;
>>
>>         ret = device_bind_driver_to_node(dev, "tegra186_reset",
>>                                          "tegra186_reset", dev->of_offset,
>>                                          &child);
>>         if (ret)
>>                 return ret;
>>
>>         ret = device_bind_driver_to_node(dev, "tegra186_power_domain",
>>                                          "tegra186_power_domain",
>>                                          dev->of_offset, &child);
>>         if (ret)
>>                 return ret;
>>
>>         ret = dm_scan_fdt_dev(dev);
>>         if (ret)
>>                 return ret;
>>
>>         return 0;
>> }
>
>
> All child function calls are structured the same, so someone reading the
> code will always see the same structure irrespective of where in a function
> a child function is called. This gives uniformity. This also yields a few
> maintenance advantages below, and helps keep all code uniform even if any of
> the maintenance operations below have been applied to some functions and
> aren't needed in others.





Did you think I ran a semantic patch with Coccinelle
and then sent it blindly?

No, this patch passed my eyes' check, at least.


Please notice this patch did not transform
the following function in arch/arm/cpu/armv7/am33xx/clk_synthesizer.c


int setup_clock_synthesizer(struct clk_synth *data)
{
        int rc;
        uint8_t val;

        rc =  i2c_probe(CLK_SYNTHESIZER_I2C_ADDR);
        if (rc) {
                printf("i2c probe failed at address 0x%x\n",
                       CLK_SYNTHESIZER_I2C_ADDR);
                return rc;
        }

        rc = clk_synthesizer_reg_read(CLK_SYNTHESIZER_ID_REG, &val);
        if (val != data->id)
                return rc;

        /* Crystal Load capacitor selection */
        rc = clk_synthesizer_reg_write(CLK_SYNTHESIZER_XCSEL, data->capacitor);
        if (rc)
                return rc;
        rc = clk_synthesizer_reg_write(CLK_SYNTHESIZER_MUX_REG, data->mux);
        if (rc)
                return rc;
        rc = clk_synthesizer_reg_write(CLK_SYNTHESIZER_PDIV2_REG, data->pdiv2);
        if (rc)
                return rc;
        rc = clk_synthesizer_reg_write(CLK_SYNTHESIZER_PDIV3_REG, data->pdiv3);
        if (rc)
                return rc;

        return 0;
}



We can transform this function if we like
but it would badly break the uniformity.
So, I did not.

So, I agree your statement to some extent.


I guess the matter is, where we should draw a line.
What is uniform and what is not uniform?



If you want to stick to the safest side,
a semantic patch like follows would do the job.

 @@
 type t;
 expression e;
 identifier ret;
 @@
 -   t ret;
 -   ret = e;
 -   if (ret)
 -       return ret;
 -   return 0;
 +
 +   return e;



> 1)
>
> If tegra186_bpmp_bind() were modified to call an additional function in its
> body, the diff for that addition would look identical no matter whether the
> new call was added at the start/middle/end of the body. We wouldn't ever
> have to do something like:
>
> -       return dm_scan_fdt_dev(dev);
> +       ret = dm_scan_fdt_dev(dev);
> +       if (ret)
> +               return ret;
>
> ... which is an edit to a piece of code that's unrelated to the code being
> added, and thus makes the patch more confusing.
>
> 2)
>
> There's always an obvious place to put any debug()/error() invocations, or
> translate return values; inside the if (ret) body. There's only one way for
> the code to look so it doesn't change based on what exactly we do with the
> return value.
>
> 3)
>
> If we add the need for cleanup logic in the failure case, we can just
> blindly change "return ret" to "goto fail" without re-structuring code.


I am not sure if 2) and 3) are realistic.

If we start to consider things that may happen or may not happen,
we end up with adding redundancy all the time.



Are you positive or negative for the following hunk?


> diff --git a/drivers/mtd/nand/fsl_elbc_nand.c b/drivers/mtd/nand/fsl_elbc_nand.c
> index f621f14..b27a6af 100644
> --- a/drivers/mtd/nand/fsl_elbc_nand.c
> +++ b/drivers/mtd/nand/fsl_elbc_nand.c
> @@ -788,11 +788,7 @@ static int fsl_elbc_chip_init(int devnum, u8 *addr)
>         if (ret)
>                 return ret;
>
> -       ret = nand_register(devnum, mtd);
> -       if (ret)
> -               return ret;
> -
> -       return 0;
> +       return nand_register(devnum, mtd);
>  }

I think probe/init function can return a value
of register function directly, from my best common sense.

This change will lose 2)
in case fsl_elbc_chip_init() fails to do nand_register, though.



-- 
Best Regards
Masahiro Yamada


More information about the U-Boot mailing list