Re: [Cocci] how to make substitutions at the end of the function, vs. the end of the block ?

2021-07-31 Thread Mansour Moufid
On Fri, Jul 30, 2021 at 7:00 AM Andrew  Yourtchenko
 wrote:
>
> However, when I run it, the "done: " label, etc. gets inserted twice:

Every return statement is the end of a function. ;)

I don't know of a way to match only the "last" return statement in a
function, maybe with scripting...
___
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

2021-04-04 Thread 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
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] Replacing a struct member with a function call

2021-03-15 Thread Mansour Moufid
On Sun, Mar 14, 2021, 20:43 Thomas Adam  wrote:

> Hello,
>
> I can see I was as clear as mud with my explanation -- apologies for
> that, so let me try again.
>
> In my original example:
>
> struct monitor {
> struct {
> int width;
> int height
> } virtual;
> };
>
> ... the members width and height aren't required any more, as they're
> actually computable generically, and don't belong in that struct.
> Instead, I have separate functions which can provide those values.
>
> So where I have in code, statements such as:
>
> struct monitor *m = this_monitor();
> int foo = m->virutal.width;
>
> I want to be able to substitute "m->virtual.width" with a function
> call "get_width()" -- which does not involve "struct monitor" at all.
> Indeed, the semantic patch I'm trying to apply now looks like this:
>
> @@
> struct monitor *m;
> @@
>
> - m->virtual.width;
> + get_width();
>
> ... and although spatch doesn't tell me of any errors, when I run it
> over my codebase, no modifications are made.  So clearly I'm still
> doing something wrong.


Remove the semi-colons. ;)
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] linux-kernel janitorial RFP: Mark static arrays as const

2021-03-03 Thread Mansour Moufid
On Tue, Mar 2, 2021 at 4:22 PM Joe Perches  wrote:
>
> Here is a possible opportunity to reduce data usage in the kernel.
>
> $ git grep -P -n '^static\s+(?!const|struct)(?:\w+\s+){1,3}\w+\s*\[\s*\]' 
> drivers/ | \
>   grep -v __initdata | \
>   wc -l
> 3250
>
> Meaning there are ~3000 declarations of arrays with what appears to be
> file static const content that are not marked const.
>
> So there are many static arrays that could be marked const to move the
> compiled object code from data to text minimizing the total amount of
> exposed r/w data.
>
> However, I do not know of a mechanism using coccinelle to determine
> whether or not any of these static declarations are ever modified.

I thought it would be a fun exercise but it got tedious quick.

I don't know how to ignore an attribute like __initdata.

Feel free to refine it:


@@
type t;
identifier x;
@@
(
static const struct { ... } x[];
|
static
+   const
struct { ... } x[];
|
static const struct s *x[];
|
static
+   const
struct s *x[];
|
static const struct s x[];
|
static
+   const
struct s x[];
|
static const t *x[];
|
static
+   const
t *x[];
|
static const t x[];
|
static
+   const
t x[];
)

@@
type t;
identifier s, x, y, z;
assignment operator xx;
@@
(
static const struct { ... } x[] = { ... };
|
static
+   const
struct { ... } x[] = { ... };
|
static const struct s *x[] = { ... };
|
static
+   const
struct s *x[] = { ... };
|
static const struct s x[] = { ... };
|
static
+   const
struct s x[] = { ... };
|
static const t *x[] = { ... };
|
static
+   const
t *x[] = { ... };
|
static const t x[] = { ... };
|
static
+   const
t x[] = { ... };
)
... when != x.y xx ...
when != x[...] xx ...
when != z = x
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] qualified function rule

2021-01-29 Thread Mansour Moufid
On Wed, Jan 27, 2021 at 3:23 PM James K. Lowden
 wrote:
>
> I don't understand how, if it's possible, to qualify a function in a
> rule.  I want the class of all functions having a parameter of a
> particular type.
>
> The code I'm working with has hundreds of unnecessary casts.  Many
> functions take a void* parameter, but are nonetheless called by casting
> the parameter.  For example, the parameters to memcpy(3) often
> have casts applied.
>
> I imagine writing a rule like
>
> @@
> type T, D;
> identifier F(void*);
> identifier D * data;
> @@
>
> - F((T*)data)
> + F(data)
>
> but that doesn't work, and I haven't found anything that does.

Try:

@@
void *x;
@@
- (void *)(x)
+ x

or, to catch them all,

@@
type t;
t *x;
@@
- (t *)(x)
+ x

but this only works on function arguments when Coccinelle knows about
the function prototype from a header file (see the options
--include-headers and -I).
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] Adjusting replacement lists with SmPL?

2020-10-24 Thread Mansour Moufid
On Fri, Oct 23, 2020 at 11:00 AM Markus Elfring  wrote:
>
> > I'd like to add a statement after another within a preprocessor expression,
>
> How do you think about to refer to a “#define directive”?

I mean, not specifically in the #define, but anywhere.

(I don't try to patch preprocessor directives with spatch, I use sed for that.)

> > #define X(a) x(a);
> >
> > (I know the above is not technically correct but it's super common.)
>
> I stumble on understanding difficulties for this information.
> Would you like to clarify the knowledge about correctness a bit more?

A trailing colon in a #define leads to code like this:

{
X(1)
X(2)
...
}

... which of course does not parse.  It would be better to use a do {
... } while (0), or better yet an inline function.  But unfortunately
the above style is very common.

On Fri, Oct 23, 2020 at 11:11 AM Julia Lawall  wrote:

> I don't think he is asking that.  He means, if the call to x happens to be
> in a macro definition, how can he ensure that the transformed code treats
> newlines in the right way.

Yes, exactly. :)
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


[Cocci] Newline escape in preprocessor

2020-10-22 Thread Mansour Moufid
Hi,

I'd like to add a statement after another within a preprocessor
expression, but spatch adds the line without an escape (backslash).

x.c:

#define X(a) x(a);

(I know the above is not technically correct but it's super common.)

x.cocci:

@@
expression e;
@@
x(e);
+   y(e);

output:

$ spatch --sp-file x.cocci x.c
HANDLING: x.c
diff =
--- x.c
+++ /tmp/cocci-output-80658-7f90b1-x.c
@@ -1 +1,2 @@
 #define X(a) x(a);
+y(a);

I can think of two solutions, if an expression is inside a
preprocessor statement: add a backslash before every newline, or skip
the newline.
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


[Cocci] how to ignore a list of identifiers

2017-07-14 Thread Mansour Moufid
Hi everyone,

I'm trying to add the const qualifier to variables of a certain type,
except for a few.

For example, I'd like to modify this program:

void foo(void) {
int a = 1;
int b = 2;
int c = 3;
int d = 4;
}

to:

void foo(void) {
const int a = 1;
int b = 2;
const int c = 3;
int d = 4;
}

So far I've tried this semantic patch:

@@
identifier x != {b, d};
@@
+ const
  int x = ...;

This works but only if all the identifiers in the list are in the source.

If the list is {b, z} for example, it doesn't work, and spatch says:

(ONCE) Expected tokens z b

Is there an easy way to do this?

Thanks for your time.
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] Red Hat Women in open source award

2016-02-21 Thread Mansour Moufid
Voted. Best of luck, I hope you win!
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci