All of lore.kernel.org
 help / color / mirror / Atom feed
* [cocci] Replace custom function multiple times
@ 2022-11-02 17:02 Sebastiano Miano
  2022-11-02 17:30 ` Julia Lawall
  2022-11-02 19:15 ` Markus Elfring
  0 siblings, 2 replies; 13+ messages in thread
From: Sebastiano Miano @ 2022-11-02 17:02 UTC (permalink / raw)
  To: cocci

Hello,

I am trying to use Coccinelle to replace a function that is called
multiple times in the code with a different version whose name is
taken from the string variable that is passed as the second parameter.

To explain better, my C code is the following:

#include <string.h>

static int process(__u64 off) {
  MERGE_FUNCTION_OPENED("file.c", "ping_pong", arg1, arg2, arg3, arg4, NULL);
  MERGE_FUNCTION_OPENED("file2.c", "ping_pong2", arg1, arg2, arg3, NULL);
  return 0;
}

I want my code to be transformed into the following version:

#include <string.h>
#include "file.h"

static int process(__u64 off) {
  ping_pong(arg1, arg2, arg3, arg4, NULL);
  ping_pong2(arg1, arg2, arg3, NULL);
  return 0;
}

where the string that is passed as the second parameter is used as the
function name, and all the other arguments are copied as they are.

I managed to write a working cocci script (listed below) that is doing the job.

@merge_annotation@
expression path, fn;
@@

MERGE_FUNCTION_OPENED(path, fn, ...);

@script:python merge_p@
path << merge_annotation.path;
fn << merge_annotation.fn;
new_fn;
include_name;
@@
print "Obtained path: %s and file name: %s" % (path,fn)
coccinelle.new_fn = cocci.make_expr(fn.strip('\"'))

import os
pre, ext = os.path.splitext(path.strip('\"'))
p = "%s.h" % (pre)

coccinelle.include_name = cocci.make_ident("\n#include \"%s\"" % (p))

@c@
expression merge_p.new_fn;
expression E1, E2;
expression list E3;
@@
- MERGE_FUNCTION_OPENED(E1, E2, E3);
+ new_fn(E3);

@add_include depends on c@
identifier merge_p.include_name;
@@

#include <...>
+ include_name

Unfortunately, this script works only when a single instance of the
MERGE_FUNCTION_OPENED(...) function is used. When I use both of them I
get a "c: already tagged token:" error.
Using "++" instead of "+" removes the error, but produces a code that
contains 2 calls to the ping_pong function, and 2 calls to the
ping_pong2 function.
Any ideas on how to solve this issue?

Thanks in advance,
Sebastiano

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

end of thread, other threads:[~2022-11-04  9:48 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-02 17:02 [cocci] Replace custom function multiple times Sebastiano Miano
2022-11-02 17:30 ` Julia Lawall
2022-11-02 17:56   ` Sebastiano Miano
2022-11-02 19:15 ` Markus Elfring
2022-11-03 10:51   ` Sebastiano Miano
2022-11-03 12:33     ` Julia Lawall
2022-11-03 13:26     ` Markus Elfring
2022-11-04  8:30     ` Markus Elfring
2022-11-04  9:31       ` Julia Lawall
2022-11-04  9:48         ` Markus Elfring
2022-11-03 13:07   ` Markus Elfring
2022-11-03 15:28     ` Julia Lawall
2022-11-03 15:50       ` Markus Elfring

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.