[cocci] Question regarding Coccinelle
Pierrick Philippe <[email protected]> Fri, 22 May 2026 09:38:34 +0200
| Newsgroups | fr.inria.cocci |
|---|---|
| Message-ID | <[email protected]> |
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;
```
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 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