Re: Coccinelle
Julia Lawall <[email protected]> Mon, 31 Mar 2025 10:52:20 +0200 (CEST)
| Newsgroups | dev.linux.lists.outreachy |
|---|---|
| Message-ID | <[email protected]> |
On Mon, 31 Mar 2025, Samuel Abraham wrote: > On Sun, Mar 30, 2025 at 3:56 PM Julia Lawall <[email protected]> wrote: > > > > Hello, > > > > The actual internship project will involve using Coccinelle. Some warm up > > examplesin using Coccinelle are available at the following URL: > > > > https://who.paris.inria.fr/Julia.Lawall/outreachy25/ > > Hello Julia, > I have been studying the SmPL examples and some documentation. > But I have some questions as regards the flow of execution of the SmPL. > > Firstly, I found out you are the writer and maintainer of Coccinelle, and I just > want to say a job well done. Thanks :) > Looking at the first example of the SmPL, > > @initialize:ocaml@ => This defines an ocaml function > @@ > > let same_function p q = > (List.hd p).Coccilib.current_element = (List.hd > q).Coccilib.current_element. => This checks if both p and q belong to > the same function Actually, this defines any data that you want to be global to the semantic patch execution. It is also possible to define a finalize that is executed after all of the files have been handled. You can also use python. Then it would be eg initialize:python. > > @r@. => Defines a named rule 'r' Yes. You dont' have to name all your rules. But if you want to refer to the metavariables defined in this rule from a later rule, then this rule needs a name. > expression e; => matches an expression > position p; => Does this store the match's position like the > line number in the file? Yes. You can also get the function name and the starting and ending lines of the containing function. > symbol true,false; > @@ > > e =@p \(true\|false\) > This line above is where I am majorly confused. > Please clarify: Are we setting the value of the expression to true or > false and storing the position of the match? This rule doesn't say it is an ocaml or python script, so it's a pattern matching rule. This is matching an assignment where the right hand side is either true or false. The left hand side is any expression e. You can also write the true or false pattern as: ( true | false ) where the ( | ) are in column 0. Outside column 0, without \, they are interpreted as somethign to match in the code. > > @@ > expression r.e; => Here, we are referencing the expression in > rule 'r' to replace the expression we have matched? e is going to be an expression, but now it has to look the same as the one matched in rule r. It doesn't have to be at the same position, though. > position q : script:ocaml(r.p) { same_function p q }; => we are > setting position q if it the same as position p from the same_function r.p is the position that was noted in rule r. Here we want to note a position and see if that position is in the same function as r.p. The idea here is that we may have x = true and then we may have x = 1. But if the second x is declared in a different function than the first x, then it might be the case that the second one is really intended to be an integer. So we take x = true as evidence that x should be a boolean only when the two occurrences of x are in the same function. This is of course a heuristic. You can try something different, for example getting rid of q entirely, and see what the result is. > @@ > > e =@q > ( > - 1 > + true > | > - 0 > + false > ) > > Then this line above replaces the 1 with true or 0 with false in the > expression that had been set previously above. > But my question is, are we setting the value of e =@p \(true\|false\) No. Pattterns don't change anything. The only way to change the state is to write OCeml or python code (script:ocaml or script:python). > or it just a place holder that will be replaced by any expression > we match > Then we can now check the matched expression if it is 0 and replace > with true and if is 1 we replace with false? The two matches of e are intrinsically at different places in the code, since in one case there is true and in the other case there is 1 (or false or 0). > > @s@ > type T; > T e; > identifier fld; > symbol true,false; > @@ > > e.fld = \(true\|false\) ==> Same question applies here too Same answer. No assignment is executed here. We find a random expression of some type, and this expression has some random structure field named fld. The whole thing is found somewhere to be assigned to true or to false in the source code. I hope it's clearer now. You may want to try running it on some small example to see what it does. julia > > @@ > type s.T; > T e; > identifier s.fld; > @@ > > e.fld = > ( > - 1 > + true > | > - 0 > + false > ) > > Thanks > > Adekunle. >