On 14.10.19 13:14, Julia Lawall wrote:


On Mon, 14 Oct 2019, Jürgen Groß wrote:

On 11.10.19 22:00, Julia Lawall wrote:


On Fri, 11 Oct 2019, Jürgen Groß wrote:

Hi,

I have a simple semantic patch:

    virtual patch

    @@
    expression buf, val;
    @@
    - snprintf(buf, PAGE_SIZE, "%d\n", val)
    + spgprintf_d(buf, val)

This works nearly always as expected, but not in some macros. The cases
where it is not working are those when "val" is using a parameter of the
macro, e.g.:

    #define MACRO(name)    snprintf(pg, PAGE_SIZE, %u\n", ptr->name)

Is that on purpose? If yes, how can this be avoided?

I don't think it should be on purpose.  Could you send some C code that
illustrates the problem?

I have attached a little C file and a semantic patch, which I had
located in a local directory.

I invoke spatch via: "spatch --cocci-file patch.cocci --patch . --dir ."

The result shows that the problem is a little bit different from my
first analysis: whether a pattern is recognized or not seems to depend
on macro parameter usage, which is not limited to the pattern itself.

Thanks for the examples.  The problem in these examples actually has
nothing to with parameter usage, but with the ability to parse the macro
definition.  I get:

  #define macro1a(par)            \
-        func(buf, 1, par)
+        func2(buf, par)

Tha above mentions a parameter but the code gets changed.  This case is no
problem because the body of the macro is an expression.


  #define macro1b(par)            \
          func(buf, 1, par)       \
          func(buf, 1, par)

Nothing happens here.  It is not possible in C to have one function call
after another like this.

Oh sorry, typo by me.



  #define macro2a(par)            \
          par++;                  \
          func(buf, 1, 17)

Nothing happens here either.  Because the final ; is omitted the body of
the macro is not valid C.  But it doesn't work if you add a semicolon at
the end either.  It seems that a sequence of statements is not supported
either.  OOn the other hand, if you transform it into a do while(0), even
with no trailing semicolon after the while (0) all is fine.

This is not always possible. See the new example as attached.

This is more like the original problem (stripped down a lot).


Juergen
#include <stdio.h>

#define macro1(prefix, name)                              \
static int func##prefix##_##name##_show(char *page)       \
{                                                         \
        return snprintf(page, 1, "%d\n", name);           \
}

#define macro2(name)                                      \
static int func##name##_show(char *page)                  \
{                                                         \
        int rb;                                           \
                                                          \
        rb = snprintf(page, 1, "%d\n", name);             \
                                                          \
        return rb;                                        \
}                                                         \
                                                          \
macro1(pre, name)
@@
expression buf, val;
@@
- snprintf(buf, 1, "%d\n", val)
+ spgprintf(buf, val)
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to