cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [Cocci] qualified function rule
@ 2021-01-27 20:23 James K. Lowden
  2021-01-27 20:35 ` Julia Lawall
  2021-01-30  3:53 ` Mansour Moufid
  0 siblings, 2 replies; 3+ messages in thread
From: James K. Lowden @ 2021-01-27 20:23 UTC (permalink / raw)
  To: cocci

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.  

In the kmalloc examples, I see things like 

- \(kmalloc|kcmalloc\)(...)
+ mumble something

but that forces me to enumerate all such function names. It seems
vaguely like positions would do the trick, but, well, vaguely.  

Is there a way?  

--jkl
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] qualified function rule
  2021-01-27 20:23 [Cocci] qualified function rule James K. Lowden
@ 2021-01-27 20:35 ` Julia Lawall
  2021-01-30  3:53 ` Mansour Moufid
  1 sibling, 0 replies; 3+ messages in thread
From: Julia Lawall @ 2021-01-27 20:35 UTC (permalink / raw)
  To: James K. Lowden; +Cc: cocci



On Wed, 27 Jan 2021, 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.
>
> In the kmalloc examples, I see things like
>
> - \(kmalloc|kcmalloc\)(...)
> + mumble something
>
> but that forces me to enumerate all such function names. It seems
> vaguely like positions would do the trick, but, well, vaguely.
>
> Is there a way?

In principle, you should be able to specify the type of F. But I'm not at
all sure that that is supported for function names.

Maybe it would suffice to do:

@fn@
identifier F,i;
parameter list[n] ps;
@@

F(ps,void *i,...) { ... }

@@
identifier fn.F;
expression list[fn.n] es;
type T;
expression *e;
@@

F(es,
- (T*)
  e, ...)

@ty@
identifier F,i;
parameter list[n] ps;
type t;
@@

t F(ps,void *i,...);

@@
identifier ty.F;
expression list[ty.n] es;
type T;
expression *e;
@@

F(es,
- (T*)
  e, ...)

Probably your function prototypes are not in the .c files, but rather in
things that they include.  So you would want to use an argument like
--all-includes (include locally mentioned header) or --recursive-includes
(include headers included in other headers).  You may want to give some -I
dir arguments to help it find the header files.

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] qualified function rule
  2021-01-27 20:23 [Cocci] qualified function rule James K. Lowden
  2021-01-27 20:35 ` Julia Lawall
@ 2021-01-30  3:53 ` Mansour Moufid
  1 sibling, 0 replies; 3+ messages in thread
From: Mansour Moufid @ 2021-01-30  3:53 UTC (permalink / raw)
  To: cocci

On Wed, Jan 27, 2021 at 3:23 PM James K. Lowden
<jklowden@schemamania.org> 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

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

end of thread, other threads:[~2021-01-30  3:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-27 20:23 [Cocci] qualified function rule James K. Lowden
2021-01-27 20:35 ` Julia Lawall
2021-01-30  3:53 ` Mansour Moufid

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).