All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [Cocci] Find expressions of given type
@ 2015-09-14  8:29 Jan Moskyto Matejka
  2015-09-14  9:38 ` [Cocci] Checking lock usage SF Markus Elfring
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jan Moskyto Matejka @ 2015-09-14  8:29 UTC (permalink / raw)
  To: cocci

Hello,

I'm trying to match all expressions of a given type. I want to check
whether all accesses to any part of some struct are locked.

In the hypothetical case (which doesn't happen), every access to that
struct would create a local variable of the wanted type and I could then
match it with something like this:

@@
identifier	   i;
@@

...
my_specific_type  *var;
...
var->i
...

Unfortunately, this structure may be also referenced from another
structure, like this:

struct A { int foo; int bar; };
struct B { int foo; struct A *bar };

struct B *b;
...
b->bar->foo;

I would like to write something like:

@@
expression struct A* E;
@@

...
+E
...

and match every occurence of not only variables of the given type, but
also an expression.

I was first trying to find some hint in the doc, found 
expression struct *E;
which is almost what I want but still too broad.

Does there exist some way to
A) achieve the behaviour I want by config
or
B) implement it?

If A, could you please give me a hint?
If B, I wanted to implement it but got totally lost in the code. Could
you please give me a pointer, where to begin? I'd like to contribute the
result then.

Best regards and thanks
MQ

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Cocci] Checking lock usage
  2015-09-14  8:29 [Cocci] Find expressions of given type Jan Moskyto Matejka
@ 2015-09-14  9:38 ` SF Markus Elfring
  2015-09-14 14:16 ` [Cocci] Find expressions of given type Peter Senna Tschudin
  2015-09-14 19:46 ` Julia Lawall
  2 siblings, 0 replies; 4+ messages in thread
From: SF Markus Elfring @ 2015-09-14  9:38 UTC (permalink / raw)
  To: cocci

> I want to check whether all accesses to any part of some struct are locked.

I imagine that you will need to perform data-flow analysis for such a task.
I guess that there are some software development challenges to achieve
it with the semantic patch language.


> struct B *b;
> ...
> b->bar->foo;

Are you eventually concerned about the handling of such pointer access
chains?


> If B, I wanted to implement it but got totally lost in the code.

Would you like to increase your efforts for the software development
with the programming language "OCaml"?

Regards,
Markus

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Cocci] Find expressions of given type
  2015-09-14  8:29 [Cocci] Find expressions of given type Jan Moskyto Matejka
  2015-09-14  9:38 ` [Cocci] Checking lock usage SF Markus Elfring
@ 2015-09-14 14:16 ` Peter Senna Tschudin
  2015-09-14 19:46 ` Julia Lawall
  2 siblings, 0 replies; 4+ messages in thread
From: Peter Senna Tschudin @ 2015-09-14 14:16 UTC (permalink / raw)
  To: cocci

Hello Jan,

I'm not sure to understand your problem, but here is a nice feature I
use to get expression types. I think this example doesn't do what you
want but I guess it shows how to get types from expressions:

@r1@
expression ep;
identifier f, fld;
type t;
t e;
@@
ep at e.fld = f

@script:python@
ep << r1.ep;
fld << r1.fld;
t << r1.t;
@@
print t, ep, fld

There are two important parts:

@r1@
...
type t;
t e;
@@

which just tells coccinelle that you are interested in e of type t,
but you don't want to match the declaration. Then the '@e' on the
line:

ep at e.fld = f

makes e match the expression that comes before '.fld', and as the type
of that expression is t, you can use t on your rules (to print, import
it in other rules, ...).

Hope it helps...


On Mon, Sep 14, 2015 at 10:29 AM, Jan Moskyto Matejka
<mq+coccinelle@ucw.cz> wrote:
> Hello,
>
> I'm trying to match all expressions of a given type. I want to check
> whether all accesses to any part of some struct are locked.
>
> In the hypothetical case (which doesn't happen), every access to that
> struct would create a local variable of the wanted type and I could then
> match it with something like this:
>
> @@
> identifier         i;
> @@
>
> ...
> my_specific_type  *var;
> ...
> var->i
> ...
>
> Unfortunately, this structure may be also referenced from another
> structure, like this:
>
> struct A { int foo; int bar; };
> struct B { int foo; struct A *bar };
>
> struct B *b;
> ...
> b->bar->foo;
>
> I would like to write something like:
>
> @@
> expression struct A* E;
> @@
>
> ...
> +E
> ...
>
> and match every occurence of not only variables of the given type, but
> also an expression.
>
> I was first trying to find some hint in the doc, found
> expression struct *E;
> which is almost what I want but still too broad.
>
> Does there exist some way to
> A) achieve the behaviour I want by config
> or
> B) implement it?
>
> If A, could you please give me a hint?
> If B, I wanted to implement it but got totally lost in the code. Could
> you please give me a pointer, where to begin? I'd like to contribute the
> result then.
>
> Best regards and thanks
> MQ
> _______________________________________________
> Cocci mailing list
> Cocci at systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci



-- 
Peter

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Cocci] Find expressions of given type
  2015-09-14  8:29 [Cocci] Find expressions of given type Jan Moskyto Matejka
  2015-09-14  9:38 ` [Cocci] Checking lock usage SF Markus Elfring
  2015-09-14 14:16 ` [Cocci] Find expressions of given type Peter Senna Tschudin
@ 2015-09-14 19:46 ` Julia Lawall
  2 siblings, 0 replies; 4+ messages in thread
From: Julia Lawall @ 2015-09-14 19:46 UTC (permalink / raw)
  To: cocci

I think Peter gave some good suggestions, so I will just add a few hints.

> I would like to write something like:
> 
> @@
> expression struct A* E;
> @@
> 
> ...
> +E
> ...

If you want to match an expression E, don't put ... ... around it.  Just 
put the expression E itself.  The way you have the ...s it means since the 
beginning of the function (for the first one) and up to the end of the 
fnuction (for the second one).  ... also has a semantics of "shortest 
path", meaning that there is an implicit constraint that the ... doesn't 
match E.  So you would only get functions that have only one occurrence of 
E.  One way to get around this is to put when any on the ...  But in your 
case, and even easier solution is to just drop them completely.

> and match every occurence of not only variables of the given type, but
> also an expression.
> 
> I was first trying to find some hint in the doc, found 
> expression struct *E;

Yeah, this was intended as a special case, because struct * is not a real 
type.

> which is almost what I want but still too broad.
> 
> Does there exist some way to
> A) achieve the behaviour I want by config
> or
> B) implement it?
> 
> If A, could you please give me a hint?
> If B, I wanted to implement it but got totally lost in the code. Could
> you please give me a pointer, where to begin? I'd like to contribute the
> result then.

Not sure what you mean by config, but you can declare a metavariable as

t E;

for any type t.  No need for "expression" in this case.

The next problem, though, is where it is going to get the type information 
from.  No problem for local variables, but it can be a problem for 
structure fields.  By default, Coccinelle only includes header files that 
have th same name as the .c file, ie processing of foo.c will include 
foo.h.  If you need more, you can use the command line option 
--all-includes, which will include all of the header files mentioned 
explicitly in the .c file, or even --recursive-includes, which does the 
same but proceeds recursively.  The more header files you include, though, 
the more parsing work will have to be done, and the more time it will 
take.  So you are better off avoiding --recursive-includes if possible.

julia

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-09-14 19:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-14  8:29 [Cocci] Find expressions of given type Jan Moskyto Matejka
2015-09-14  9:38 ` [Cocci] Checking lock usage SF Markus Elfring
2015-09-14 14:16 ` [Cocci] Find expressions of given type Peter Senna Tschudin
2015-09-14 19:46 ` Julia Lawall

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.