Re: [cocci] Adjusting attributes for variables with SmPL?

Julia Lawall <[email protected]> Wed, 27 May 2026 11:16:44 +0200 (CEST)
Newsgroups fr.inria.cocci
Message-ID <[email protected]>

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:
> >
> >> Hi Markus and Julia,
> >>
> >> I hope you are doing well.
> >>
> >> I am now trying to work with structures and basically here is the patch
> >> I'd like to generate:
> >>
> >> ```diff
> >> struct foo {
> >>     int x;
> >>     int y;
> >> };
> >> 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;
> >> }
> >> ```
> >>
> >> I used the following semantic patch which is successfully parsed, but
> >> not generating any diff:
> >>
> >> ```cocci
> >> @attribute_struct_scalar_type@
> >> expression E, E1, E2;
> >> type T;
> >> identifier F, X;
> >> @@
> >> -T F = E;
> >> +T __attribute__((myattr(("X")))) F = E;
> >> ...
> >> memcmp(&F.X, E1, E2)
> >> ```
> > 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 # "\"";

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

> I think I read something related to attribute not being parsed in `+`
> context in the documentation.
>
> >
> > Lots of other decompositions are possible.  For example you can also
> > write:
> >
> > T
> > + __attribute__((myattr(("X"))))
> >   F = { ... };
> >
> > julia
> >
> >> The idea is essentially the same than in my previous email, although
> >> here I'm working with field access.
> >> The semantic patch is clearly inspired from a previous version for
> >> integer or float which was successfully parsed and spatch was generating
> >> the expected diff.
> >>
> >> Also, from what I understand from the docs, metavariables of type
> >> `field` target field declaration (meaning within the structure type
> >> definition specifically).
> >> And I could not wrap my head around a way to use it to achieve my goal here.
> >> I also tried to used a quite simple semantic patch, which was still not
> >> generating any diff and raising a warning ("should f be a metavariable?").
> >> And I do not understand why this one is not working since there are
> >> basically no metavariables outside the three expressions.
> >> Here is the semantic patch in question:
> >>
> >> ```cocci
> >> @attribute_struct_scalar_type_dummy@
> >> expression E, E1, E2;
> >> @@
> >> -struct foo f = E;
> >> +struct foo __attribute__((myattr(("x")))) f = E;
> >> ...
> >> memcmp(&f.x, E1, E2)
> >> ```
> >>
> >> In any case, thank you for your time and your previous answers.
> >>
> >> Pierrick
> >>
> > >
>