Re: [cocci] Question regarding Coccinelle
Pierrick Philippe <[email protected]> Tue, 26 May 2026 10:55:04 +0200
| Newsgroups | fr.inria.cocci |
|---|---|
| Message-ID | <[email protected]> |
Hi Julia,
First of all thanks for your answer.
On 5/26/26 10:23, Julia Lawall wrote:
> On Fri, 22 May 2026, Pierrick Philippe wrote:
>> Hi everyone,
>>
>> I am currently trying to assess if Coccinelle could be used to match my
>> use case.
>> What I am trying to do is to modify the declaration of a specific
>> variable by adding an attribute to it, if and only if it is passed to a
>> specific function (memcmp in this case).
>> i am using Coccinelle version 1.3.0 (through nix-shell:
>> https://search.nixos.org/packages?channel=25.11&query=coccinelle#show=coccinelle
>> <https://search.nixos.org/packages?channel=25.11&query=coccinelle#show=coccinelle> ).
>>
>> My input test program is the following:
>>
>> ```c
>> #include <string.h>
>> int main(void) {
>> int a = 42; // This is the variable of interest
>> int b = 43;
>> int z = memcmp(&a, &b, sizeof(int));
>> return z;
>> }
>> ```
>>
>> Which should be modify to:
>>
>> ```c
>> #include <string.h>
>> int main(void) {
>> int __attribute__((myattr)) a = 42; // This is the variable of interest
>> int b = 43;
>> int z = memcmp(&a, &b, sizeof(int));
>> return z;
>> }
>> ```
>>
>> In order to do this, I played around with Coccinelle, but I keep having
>> weird behavior from spatch (at least from where I stand).
>> Here are two attempt to cocci rules:
>>
>> ```cocci
>> @first@
>> type T;
>> T VAR;
>> expression E;
>> @@
>> -VAR = E;
>> +__attribute__((myattr)) VAR = E; // Parse error here
>> ...
>> memcmp(&VAR, ...);
>> ```
>>
>> But for some reason, I got the following parsing error for the first
>> attempt and I couldn't wrap my head around it:
>>
>> ```
>> plus: parse error:
>> File "test.cocci", line 7, column 25, charpos = 76
>> around = 'VAR',
>> whole content = +__attribute__((myattr)) VAR = E;
>> ```
> Attributes are only allowed in variable declarations, which declare
> identifiers, not arbitrary expressions.
>
> In int x = 3;
>
> x somehow doesn't yet have type int. It's just a name.
>
> Also, Coccinelle requires you specify a complete term, not just the
> subpart of the term that is convenient for you.
I think I understand, also I do have a question regarding the
`declaration` keyword of the grammar.
Is there any way to access inner information of the declaration? Such as
the type or the presence of an initializer or not?
For example in a python or a ocaml script?
>> My second attempt is successfully parsed, but leave the input program
>> untouched:
>>
>> ```cocci
>> @second@
>> type T;
>> identifier VAR;
>> expression E;
>> @@
>> -T VAR = E;
>> +T __attribute__((myattr)) VAR = E;
>> ...
>> memcmp(&VAR, ...);
> This again fails because you have only specified part of the memcmp
> statement. If you want to put a complete statement, you have to put the
> whole thing. I think you can also just put a memcmp call, without the
> semicolon.
It worked with the whole `memcmp` call statement indeed, I probably
misunderstood the `...` operator then.
Pierrick
> julia
>> ```
>>
>> This is probably an easy use case, but I am completely new to Coccinelle
>> and I honestly can't understand what is going wrong here.
>> To be honest, I also do not understand the difference between the two
>> rules, and why one is successfully parsed and not the other one.
>> Although this is probably due to a lack of experience with Coccinelle
>> and of its internals.
>>
>> Cheers,
>>
>> Pierrick
>>
> >