Re: [cocci] Adjusting attributes for variables with SmPL?
Pierrick Philippe <[email protected]> Wed, 27 May 2026 11:44:34 +0200
| Newsgroups | fr.inria.cocci |
|---|---|
| Message-ID | <[email protected]> |
On 5/27/26 11:16, Julia Lawall wrote:
> On Wed, 27 May 2026, Pierrick Philippe wrote:
>> On 5/27/26 10:59, Julia Lawall wrote:
>>> On Wed, 27 May 2026, Pierrick Philippe wrote:
>>> I think that the problem is that { ... } is not actually an expression in
>>> C. It's a strange thing that can only exist as part of a declaration.
>>>
>>> But you don't want to change it, so you don't need a metavariable for it.
>>>
>>> Just write:
>>>
>>> - T F =
>>> + T __attribute__((myattr(("X")))) F =
>>> { ... };
>> It does work indeed, thanks a lot!
>> Although the metavariable X is not interpreted in the `+` context, could
>> it be due to the attribute or unrelated?
> It's due to the string quotes.
>
> It nmight be possible to get around this with a fresh identifier:
>
> fresh identifier x = "\"" # X # "\"";
So this work, although the output still have the escaped quote, probably
due to the same "issue" regarding quote.
But there also is a possible issue with the parsing/code generation
here, generated diff and output of spatch:
```
@attribute_struct_scalar_type_resolve@
expression E1, E2;
type T;
identifier F, X;
fresh identifier param = "\"" ## X ## "\"";
@@
T
+__attribute__((myattr((param))))
F = { ... };
...
memcmp(&F.X, E1, E2)
warning: attribute_struct_scalar_type_resolve: metavariable param not
used in the + code
HANDLING: ./test.c
-----------------------------------------------------------------------
let's go
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-----------------------------------------------------------------------
attribute_struct_scalar_type_resolve =
-----------------------------------------------------------------------
dependencies for rule attribute_struct_scalar_type_resolve satisfied:
binding in = []
binding relevant in = []
transformation info returned:
transform state: 7
with rule_elem: T
>>> __attribute__ ((myattr((param))))
F = {...
,
};
with binding: [attribute_struct_scalar_type_resolve.param
--> id \"x\";
attribute_struct_scalar_type_resolve.X -->
id x]
binding out = []
transform one node: 7
parse error
= error in /tmp/cocci_small_output-112399-acda44-test.c; set
verbose_parsing for more info
badcount: 2
bad:
bad:
bad: int main(void) {
-----------------------------------------------------------------------
Finished
-----------------------------------------------------------------------
parse error
= error in /tmp/cocci-output-112399-6b7713-test.c; set verbose_parsing
for more info
badcount: 3
bad: };
bad:
bad: int main(void) {
BAD:!!!!! struct foo __attribute__((myattr((\"x\")))) f = { 42, 42 };
diff =
--- ./test.c
+++ /tmp/cocci-output-112399-6b7713-test.c
@@ -6,9 +6,9 @@ struct foo {
};
int main(void) {
- struct foo f = { 42, 42 };
+ struct foo __attribute__((myattr((\"x\")))) f = { 42, 42 };
int b;
int *c = &b;
int z = memcmp(&f.x, c, sizeof(int));
return z;
-}
+}"
Check duplication for 1 files
```
It seems like a quote is added at the end of the main function itself.
> I haven't tried this, though. Another more complex solution involves
> passing through python code. You can get inspiration from
> demos/pythontococci.cocci. You would want to set eg coccinelle.z to a
> string that additionally contains the desired string quotes.
>
> julia