On Sat, 29 Aug 2020, Joe Perches wrote:

> Is it me not understanding cocci grammar again?

The problem is the loop.  You are trying to change something in the body
of a loop and the body of a for loop might not be executed.  ... means
that the thing must be found on every execution path.

Do you want to make the change in the function header even if there are
not changes in the body?  If so, <... ...> is what you are looking for.
If you want to be sure there is a change to make in the function body then
you can use <+... ...+> but it will be more expensive.

julia

>
> Given these input and cocci script files:
>
> Why isn't the show_test1 function transformed?
> Why is only the show_test2 function transformed?
>
> The only difference between the files is some
> commented out lines with a for loop and if test.
>
> $ cat test.c
> static ssize_t test1_show(struct device *d,
>               struct device_attribute *a,
>               char *buffer)
> {
>       ssize_t rc = 0;
>
>       for (cnt = 0; cnt < s_attr->size; cnt++) {
>               if (cnt && !(cnt % 16)) {
>                       if (PAGE_SIZE - rc)
>                               buffer[rc++] = '\n';
>               }
>
>               rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ",
>                               ((unsigned char *)s_attr->data)[cnt]);
>       }
>       return rc;
> }
>
> static ssize_t test2_show(struct device *d,
>               struct device_attribute *a,
>               char *buffer)
> {
>       ssize_t rc = 0;
>
> //    for (cnt = 0; cnt < s_attr->size; cnt++) {
> //            if (cnt && !(cnt % 16)) {
>                       if (PAGE_SIZE - rc)
>                               buffer[rc++] = '\n';
> //            }
>               rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ",
>                               ((unsigned char *)s_attr->data)[cnt]);
> //    }
>       return rc;
> }
> $ cat sysfs_emit_rename.cocci
> @@
> identifier d_show =~ "^.*show.*$";
> identifier arg1, arg2, arg3;
> @@
> ssize_t d_show(struct device *
> -     arg1
> +     dev
>       , struct device_attribute *
> -     arg2
> +     attr
>       , char *
> -     arg3
> +     buf
>       )
> {
>       ...
> (
> -     arg1
> +     dev
> |
> -     arg2
> +     attr
> |
> -     arg3
> +     buf
> )
>       ... when any
> }
> $ spatch -sp-file sysfs_emit_rename.cocci test.c
> init_defs_builtins: /usr/local/bin/../lib/coccinelle/standard.h
> HANDLING: test.c
> diff =
> --- test.c
> +++ /tmp/cocci-output-68270-4c9b1f-test.c
> @@ -16,18 +16,18 @@ static ssize_t test1_show(struct device
>       return rc;
>  }
>
> -static ssize_t test2_show(struct device *d,
> -             struct device_attribute *a,
> -             char *buffer)
> +static ssize_t test2_show(struct device *dev,
> +                       struct device_attribute *attr,
> +                       char *buf)
>  {
>       ssize_t rc = 0;
>
>  //   for (cnt = 0; cnt < s_attr->size; cnt++) {
>  //           if (cnt && !(cnt % 16)) {
>                       if (PAGE_SIZE - rc)
> -                             buffer[rc++] = '\n';
> +                             buf[rc++] = '\n';
>  //           }
> -             rc += scnprintf(buffer + rc, PAGE_SIZE - rc, "%02x ",
> +             rc += scnprintf(buf + rc, PAGE_SIZE - rc, "%02x ",
>                               ((unsigned char *)s_attr->data)[cnt]);
>  //   }
>       return rc;
> $
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to