[Cocci] Change function invocations argument

Eliseo Martínez eliseomarmol at gmail.com
Sun Dec 21 11:43:43 CET 2014


Ok. Finally I have it working. There’s still a gotcha, though.
The first version of my spatch contained these rules:

```
@@ typedef long_u, uintmax_t; expression e1, e2, e3; @@

-   put_bytes(e1, (long_u)(e2), e3)
+   put_bytes(e1, (uintmax_t)e2, e3)

@@ expression e1, e2, e3; @@

-   put_bytes(e1, (long_u)e2, e3)
+   put_bytes(e1, (uintmax_t)e2, e3)

@@ expression e1, e2, e3; @@

-   put_bytes(e1, e2, (int)(e3))
+   put_bytes(e1, e2, (unsigned int)e3)

@@ expression e1, e2, e3; @@

-   put_bytes(e1, e2, (int)e3)
+   put_bytes(e1, e2, (unsigned int)e3)

```

Those work ok.
Then, I tried a second version, trying to compact those rules into a single, more expressive one:

```
@@ typedef long_u, uintmax_t; expression e1, e2, e3; @@

    put_bytes(
        e1,
(
-       (long_u)(e2)
|
-       (long_u)e2
)
+       (uintmax_t)e2
        ,
(
-       (int)(e3)
|
-       (int)e3
)
+       (unsigned int)e3
    )

```

That parses ok but just does nothing. So, it seems it doesn’t match anything, but I don’t understand why.
Could you please explain?

> On 20 Dec 2014, at 21:51, Julia Lawall <julia.lawall at lip6.fr> wrote:
> 
> 
> 
> On Sat, 20 Dec 2014, Eliseo Martínez wrote:
> 
>> Great. That worked. Just one more thing, please.
>> Some of the replacements done by the rule before leave literals of the form “(uintmax_t)34”.
>> I would like to use an unsigned constant in those cases. This is, something like “34u”. 
>> So I add the following rule:
>> 
>> ```
>> @@ expression e1, e3; constant c2; @@
>> 30 
>> 31 - put_bytes(e1, (uintmax_t)c2, e3)
>> 32 + put_bytes(e1, c2, e3)
>> ```
>> 
>> That does the work, except adding the “u”. How could I do that?
>> Is it possible adding something literally in the output?
>> Is it possible to somehow say in line 32 “render c2 but with u suffix”?
> 
> It is possible, but somewhat awkward.  Here is a simpler example that you 
> can adapt to your case:
> 
> @r@
> typedef uintmax_t;
> constant c;
> @@
> 
> (uintmax_t)c
> 
> @script:python s@
> c << r.c;
> cu;
> @@
> 
> coccinelle.cu = "%su" % (c)
> 
> @@
> constant r.c;
> identifier s.cu;
> @@
> 
> - (uintmax_t)c
> + cu
> 
> julia



More information about the Cocci mailing list