Coccinelle archive on lore.kernel.org
 help / color / Atom feed
* [Cocci] Using cocci to mangle function arguments
@ 2020-05-17  0:16 Thomas Adam
  2020-05-17  8:07 ` Julia Lawall
  0 siblings, 1 reply; 2+ messages in thread
From: Thomas Adam @ 2020-05-17  0:16 UTC (permalink / raw)
  To: cocci

Hi all,

I have a situation where I'm trying to use Coccinelle to both rename one
function to another, and at the same time modify the number of arguments.

I have a function called:

    func_old(ERR, "func_name", "%s message", charstring);

Which is therefore variadic.  In terms of argument ordering func_old() takes:

  1.  log-level type
  2.  the calling function
  3.  A variadic string

I'm wanting to replace func_old() with func_new() such that it looks like
this:

    func_new("%s: %s message", __func__, charstring);

Hence, func_new() reduces the number of arguments to just one -- a format
string, and variadic arguments.

My question is how would I go about trying to get coccinelle to help me
translate this?  Is this even possible?  I have tried:

@@
expression O1, O2, O3
@@

- func_old(O1, O2, O3, ...);

But I don't know how to convert what would be O2 to '__func__'.  When calling
func_new(), there is no explicit O2 parameter from func_old(), it should be
part of the format string, hence:

+ func_new("%s: ...", __func__, O3);

I suspect I might be stretching coccinelle's abilities in trying to craft new
parameters, but I thought I'd ask.

Apologies if this is convoluted.  If I can help answer any additional
questions, or if something's not clear, let me know.

TIA!

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

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

* Re: [Cocci] Using cocci to mangle function arguments
  2020-05-17  0:16 [Cocci] Using cocci to mangle function arguments Thomas Adam
@ 2020-05-17  8:07 ` Julia Lawall
  0 siblings, 0 replies; 2+ messages in thread
From: Julia Lawall @ 2020-05-17  8:07 UTC (permalink / raw)
  To: Thomas Adam; +Cc: cocci



On Sun, 17 May 2020, Thomas Adam wrote:

> Hi all,
>
> I have a situation where I'm trying to use Coccinelle to both rename one
> function to another, and at the same time modify the number of arguments.
>
> I have a function called:
>
>     func_old(ERR, "func_name", "%s message", charstring);
>
> Which is therefore variadic.  In terms of argument ordering func_old() takes:
>
>   1.  log-level type
>   2.  the calling function
>   3.  A variadic string
>
> I'm wanting to replace func_old() with func_new() such that it looks like
> this:
>
>     func_new("%s: %s message", __func__, charstring);
>
> Hence, func_new() reduces the number of arguments to just one -- a format
> string, and variadic arguments.
>
> My question is how would I go about trying to get coccinelle to help me
> translate this?  Is this even possible?  I have tried:
>
> @@
> expression O1, O2, O3
> @@
>
> - func_old(O1, O2, O3, ...);
>
> But I don't know how to convert what would be O2 to '__func__'.  When calling
> func_new(), there is no explicit O2 parameter from func_old(), it should be
> part of the format string, hence:
>
> + func_new("%s: ...", __func__, O3);
>
> I suspect I might be stretching coccinelle's abilities in trying to craft new
> parameters, but I thought I'd ask.

I'm not sure to completely understand the problem.  Is it that O1 is
always an explicit string and you want to add %s: at the beginning of that
string?

If you require O1 to be an explicit string, it would be better to declare
it as:

constant char[] O1;

For creating the new string, you can use python.  Check
demos/pythontococci.cocci.  Basically, you need to play with the data you
are given in the python code to create the string that you want.
According to the example, when you use it in the final SmPL rule that
creates the new code, you will call it an identifier, even though it is a
string.  But it doesn't matter.

There is a more clean way to do this with some make_expr function, but
there doens't seem to be a convenient demo for it.

julia


>
> Apologies if this is convoluted.  If I can help answer any additional
> questions, or if something's not clear, let me know.
>
> TIA!
>
> Thomas
> _______________________________________________
> 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

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

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-17  0:16 [Cocci] Using cocci to mangle function arguments Thomas Adam
2020-05-17  8:07 ` Julia Lawall

Coccinelle archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/cocci/0 cocci/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 cocci cocci/ https://lore.kernel.org/cocci \
		cocci@systeme.lip6.fr
	public-inbox-index cocci

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/fr.lip6.systeme.cocci


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git