From: Sebastiano Miano <mianosebastiano@gmail.com>
To: cocci@inria.fr
Subject: [cocci] Replace custom function multiple times
Date: Wed, 2 Nov 2022 18:02:03 +0100 [thread overview]
Message-ID: <CAMENy5qK-KB1_D-zgh=UyLarKVaup-J0yRdZwTL-fkyn_gQhJg@mail.gmail.com> (raw)
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
next reply other threads:[~2022-11-02 17:11 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-02 17:02 Sebastiano Miano [this message]
2022-11-02 17:30 ` [cocci] Replace custom function multiple times 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAMENy5qK-KB1_D-zgh=UyLarKVaup-J0yRdZwTL-fkyn_gQhJg@mail.gmail.com' \
--to=mianosebastiano@gmail.com \
--cc=cocci@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).