Re: [Cocci] changing of_get_mac_address() to pass a buffer
On Mon, 5 Apr 2021, Michael Walle wrote: > Hi Mansour, > > Am 2021-04-04 19:48, schrieb Mansour Moufid: > > On Thu, Apr 1, 2021 at 4:13 AM Michael Walle wrote: > > > > > > Hi, > > > > > > so first I need to say I've never used coccinelle before, > > > so please bear with me ;) > > > > > > To make of_get_mac_address() work with DSA ports (and a nvmem > > > provider) I'd need to change the semantics of of_get_mac_address(). > > > Right now it returns a pointer to "const char *", I'd need to change > > > that so a buffer will be passed as a parameter in which the MAC > > > address gets stored. > > > > > > (1) Usually the call is something like: > > > > > >const char *mac; > > >mac = of_get_mac_address(np); > > >if (!IS_ERR(mac)) > > > ether_addr_copy(ndev->dev_addr, mac); > > > > > > This would need to be changed to: > > > > > >of_get_mac_address(np, ndev->dev_addr); > > > > Here is one possible approach, doing the API change first then > > handling the conditionals. It seems to work. > > > > @a@ > > identifier x; > > expression y, z; > > @@ > > - x = of_get_mac_address(y); > > + x = of_get_mac_address(y, z); > > <... > > - ether_addr_copy(z, x); > > ...> > > > > @@ > > identifier a.x; > > @@ > > - if (<+... x ...+>) {} > > > > @@ > > identifier a.x; > > @@ > > if (<+... x ...+>) { > > ... > > } > > - else {} > > > > @@ > > identifier a.x; > > expression e; > > @@ > > - if (<+... x ...+>@e) > > - {} > > - else > > + if (!(e)) Maybe try the above line without the parentheses around e? > > {...} > > > > @@ > > expression x, y, z; > > @@ > > - x = of_get_mac_address(y, z); > > + of_get_mac_address(y, z); > > ... when != x > > Thanks a lot! > > See also > https://lore.kernel.org/netdev/20210405164643.21130-1-mich...@walle.cc/ > > There were some "if (!(!IS_ERR(x))", which I needed to simplify > manually. Didn't noticed that in my previous script. I'm just > curious, is that also something coccinelle can simplify on its > own? You can certainly write another rule for it: - !(!e) + e julia > > -michael > ___ > Cocci mailing list > Cocci@systeme.lip6.fr > https://systeme.lip6.fr/mailman/listinfo/cocci > ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] changing of_get_mac_address() to pass a buffer
Hi Mansour, Am 2021-04-04 19:48, schrieb Mansour Moufid: On Thu, Apr 1, 2021 at 4:13 AM Michael Walle wrote: Hi, so first I need to say I've never used coccinelle before, so please bear with me ;) To make of_get_mac_address() work with DSA ports (and a nvmem provider) I'd need to change the semantics of of_get_mac_address(). Right now it returns a pointer to "const char *", I'd need to change that so a buffer will be passed as a parameter in which the MAC address gets stored. (1) Usually the call is something like: const char *mac; mac = of_get_mac_address(np); if (!IS_ERR(mac)) ether_addr_copy(ndev->dev_addr, mac); This would need to be changed to: of_get_mac_address(np, ndev->dev_addr); Here is one possible approach, doing the API change first then handling the conditionals. It seems to work. @a@ identifier x; expression y, z; @@ - x = of_get_mac_address(y); + x = of_get_mac_address(y, z); <... - ether_addr_copy(z, x); ...> @@ identifier a.x; @@ - if (<+... x ...+>) {} @@ identifier a.x; @@ if (<+... x ...+>) { ... } - else {} @@ identifier a.x; expression e; @@ - if (<+... x ...+>@e) - {} - else + if (!(e)) {...} @@ expression x, y, z; @@ - x = of_get_mac_address(y, z); + of_get_mac_address(y, z); ... when != x Thanks a lot! See also https://lore.kernel.org/netdev/20210405164643.21130-1-mich...@walle.cc/ There were some "if (!(!IS_ERR(x))", which I needed to simplify manually. Didn't noticed that in my previous script. I'm just curious, is that also something coccinelle can simplify on its own? -michael ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Excluding quotes from strings of #define directives
>> I hoped that the specified constraint for the metavariable “e” would mean >> that expressions which contain a double quotation character should be >> excluded >> for my source code analysis approach. >> Would you like to check the observed software functionality once more? > > There is perhaps a problem, but it is surely not necessary to have all of > this python code around it to see the problem. I chose this test display so that it can be clearly seen which data were processes for the metavariable “e”. > Please make a minimal example. A rule with a match and a * in front of it > should be sufficient. Do you find constraints of the following SmPL script variant easier to clarify? @display@ identifier i =~ "^(?:[A-Z]+_){3,3}[A-Z]+"; expression e !~ "\""; @@ *#define i e Test result: https://gitlab.freedesktop.org/pipewire/pipewire/-/blob/97b01ed9b01bac7cba68ff11c6bf7ceabcae7f52/spa/include/spa/node/type-info.h#L38 elfring@Sonne:~/Projekte/PipeWire/lokal> spatch ~/Projekte/Coccinelle/janitor/show_define_usage4.cocci spa/include/spa/node/type-info.h … @@ -35,8 +35,6 @@ extern "C" { #include #include -#define SPA_TYPE_INFO_IO SPA_TYPE_INFO_ENUM_BASE "IO" -#define SPA_TYPE_INFO_IO_BASE SPA_TYPE_INFO_IO ":" static const struct spa_type_info spa_type_io[] = { { SPA_IO_Invalid, SPA_TYPE_Int, SPA_TYPE_INFO_IO_BASE "Invalid", NULL }, … Regards, Markus ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Checking support for compound expressions (according to #define directives)
>> I would like to avoid the repetition of parsing efforts as much as possible. >> Under which circumstances can replacement lists be taken better into account? > > Why does my suggestion involve a repetition of parsing effort? The selection of the applied programming interfaces has got significant influences on the run time behaviour. See also: https://github.com/coccinelle/coccinelle/issues/200#issuecomment-653775288 > You want to use a regexp. This view depends on some factors. I would prefer to search for string literals (and their exclusion) by higher level means. > I'm asking you to put the regexp in a python function. How do you think about to improve the following software situation besides the application of regular expressions? @initialize:python@ @@ import re @display@ identifier i =~ "^(?:[A-Z]+_){3,3}[A-Z]+"; expression e : script:python() { re.match('"', e) }; @@ *#define i e elfring@Sonne:~/Projekte/PipeWire/lokal> spatch ~/Projekte/Coccinelle/janitor/show_define_usage7.cocci spa/include/spa/node/type-info.h … File "", line 5 coccinelle.result = (re . match ( " , e )) ^ SyntaxError: EOL while scanning string literal Regards, Markus ___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Checking support for compound expressions (according to #define directives)
On Mon, 5 Apr 2021, Markus Elfring wrote: > >> I would like to avoid the repetition of parsing efforts as much as > >> possible. > >> Under which circumstances can replacement lists be taken better into > >> account? > > > > Why does my suggestion involve a repetition of parsing effort? > > The selection of the applied programming interfaces has got significant > influences > on the run time behaviour. > > See also: > https://github.com/coccinelle/coccinelle/issues/200#issuecomment-653775288 > > > > You want to use a regexp. > > This view depends on some factors. > I would prefer to search for string literals (and their exclusion) by higher > level means. > > > > I'm asking you to put the regexp in a python function. > > How do you think about to improve the following software situation > besides the application of regular expressions? > > @initialize:python@ > @@ > import re > > @display@ > identifier i =~ "^(?:[A-Z]+_){3,3}[A-Z]+"; > expression e : script:python() { re.match('"', e) }; > @@ > *#define i e > > > elfring@Sonne:~/Projekte/PipeWire/lokal> spatch > ~/Projekte/Coccinelle/janitor/show_define_usage7.cocci > spa/include/spa/node/type-info.h > … > File "", line 5 > coccinelle.result = (re . match ( " , e )) > ^ > SyntaxError: EOL while scanning string literal This looks like a problem. Thanks for the report. julia___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Checking support for compound expressions (according to #define directives)
On Mon, 5 Apr 2021, Markus Elfring wrote: > >> @display@ > >> identifier i =~ "^(?:[A-Z]+_){3,3}[A-Z]+", x; > >> constant c =~ "\""; > >> @@ > >> *#define i x c > >> > >> > >> elfring@Sonne:~/Projekte/Coccinelle/janitor> spatch --parse-cocci > >> show_define_usage6.cocci > >> … > >> minus: parse error: > >> File "show_define_usage6.cocci", line 5, column 13, charpos = 92 > >> around = 'c', > >> whole content = *#define i x c > > > > No. You have to match the expression and then check it using python. > > I find this view improvable. > > I would like to avoid the repetition of parsing efforts as much as possible. > Under which circumstances can replacement lists be taken better into account? Why does my suggestion involve a repetition of parsing effort? You want to use a regexp. I'm asking you to put the regexp in a python function. julia___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci
Re: [Cocci] Checking support for compound expressions (according to #define directives)
On Mon, 5 Apr 2021, Markus Elfring wrote: > > Thanks for the simpler examples. > > Would you like to support another search pattern by the means of > the semantic patch language? > > > @display@ > identifier i =~ "^(?:[A-Z]+_){3,3}[A-Z]+", x; > constant c =~ "\""; > @@ > *#define i x c > > > elfring@Sonne:~/Projekte/Coccinelle/janitor> spatch --parse-cocci > show_define_usage6.cocci > … > minus: parse error: > File "show_define_usage6.cocci", line 5, column 13, charpos = 92 > around = 'c', > whole content = *#define i x c No. You have to match the expression and then check it using python. YOu can write: @@ identifier i; expression e : script:python() { ... python code to ceck e }; @@ #define i e julia___ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci