Re: [cocci] Adjusting attributes for variables with SmPL?
Pierrick Philippe <[email protected]> Thu, 28 May 2026 07:51:06 +0200
| Newsgroups | fr.inria.cocci |
|---|---|
| Message-ID | <[email protected]> |
On 5/27/26 12:53, Julia Lawall wrote:
> On Wed, 27 May 2026, Pierrick Philippe wrote:
>> 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.
> I'm sorry, but I don't know what you want for the output. Could you
> provide an example? If you don't want quotes at all, then you don't need
> quotes at all.
I apologize to both of you for the confusion, I do want quote, but I do
not want escaped quote (I haven't tried it with escaped quote, but I'm
pretty sure it won't be a valid string for GCC).
I also apologize for the lack of precision here.
The attribute I'm working with is taking an optional parameter, which,
if present, should be a string (const char *) containing the field name
of interest.
So in my example, the field of interest is `x` from the memory object
`f` of type ` struct foo` (this instance specifically, not all instance
of the same type).
And the argument to the attribute would then be the string "x".
The requirement of being a string is due to restriction for parameter of
GNU attributes (outside internal attributes), which have to be constant
literals or a declared identifier.
In my case, without the quote, the parameter would be interpreted as an
undeclared identifier, hence leading to compilation error.
> julia
>> 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
> >