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

Julia Lawall <[email protected]> Thu, 28 May 2026 12:49:51 +0200 (CEST)
Newsgroups fr.inria.cocci
Message-ID <[email protected]>
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--8323329-656495293-1779965391=:10822
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8BIT



On Thu, 28 May 2026, Pierrick Philippe wrote:

> On 5/28/26 10:21, Julia Lawall wrote:
> > On Thu, 28 May 2026, Pierrick Philippe wrote:
> >> On 5/28/26 09:48, Julia Lawall wrote:
> >> I have tried it, and spatch was complaining for some reason.
> >> Though it might be to my lack of experience with SmPL.
> >> I can provide the output if needed.
> > Please provide the semantic patch used and the resulting output.
>
> Sure, here is the output of `spatch --sp-file ./test-python.cocci
> ./test-struct.c -debug`:
>
> ```
> init_defs_builtins:
> /nix/store/40c99rw6w18kbf89qjqampp7m672a773-coccinelle-1.3.0/lib/coccinelle/standard.h
> -----------------------------------------------------------------------
> processing semantic patch file: ./test-python.cocci
> with isos from:
> /nix/store/40c99rw6w18kbf89qjqampp7m672a773-coccinelle-1.3.0/lib/coccinelle/standard.iso
> -----------------------------------------------------------------------
> @searching_struct_scalar@
> expression E1, E2;
> identifier F, X;
> initialiser I;
> type T;
> @@
> T F = I;
> ...
> (
> memcmp(&F.X, E1, E2)
> |
> memcmp(E1, &F.X, E2)
> )
>
> @script:python generation_struct_scalar@
> f << searching_struct_scalar.F;
> x << searching_struct_scalar.X;
> content;
> @@
> coccinelle.content = cocci.make_ident("__attribute__((myattr(\"" + x +
> "\"))) ")
>
> @replacement_struct_scalar@
> expression searching_struct_scalar.E1, searching_struct_scalar.E2;
> identifier searching_struct_scalar.F, searching_struct_scalar.X,
> generation_struct_scalar.content;
> initialiser searching_struct_scalar.I;
> type searching_struct_scalar.T;
> @@
> T
> +content
>  F = I;

You have declared content as an identifer and added it into a position
where an identifier is not allowed.  My suggestion was something like:

coccinelle.content = "\"" + x + "\""

Or whatever is appropriate in pything for putting the value of x in string
quotes.  Then in the replacement rule you can put the whole
__attribute((...)) construction.

julia

> ...
> (
> memcmp(&F.X, E1, E2)
> |
> memcmp(E1, &F.X, E2)
> )
>
> plus: parse error: 
>   File "./test-python.cocci", line 30, column 1, charpos = 629
>   around = 'F',
>   whole content =  F = I;
> ```
>
> Seems like the issue is in the rule `replacement_struct_scalar`.
>
> >
> > thanks,
> > julia
>
--8323329-656495293-1779965391=:10822--