From: Julia Lawall <julia.lawall@inria.fr>
To: Lorenz Bauer <lorenz.bauer@isovalent.com>
Cc: cocci@inria.fr
Subject: Re: [cocci] Refactoring C preprocessor using cocci?
Date: Fri, 2 Feb 2024 18:22:01 +0100 (CET) [thread overview]
Message-ID: <bc9490c0-4998-c1d8-4971-7093902e3756@inria.fr> (raw)
In-Reply-To: <CAN+4W8jQeU70dRrEokYxomCBdM3cFmbxBe2mXnsTg4nijTJ3Sg@mail.gmail.com>
On Fri, 2 Feb 2024, Lorenz Bauer wrote:
> Hi,
>
> I work on a codebase which has a fair amount of preprocessor usage
> (https://github.com/cilium/cilium). I'm trying to figure out whether
> we could use coccinelle to automate refactoring of said code. For
> example:
>
> #ifdef ENABLE_IPV4
> static __always_inline bool identity_from_ipcache_ok(void)
> {
> return SECCTX_FROM_IPCACHE == SECCTX_FROM_IPCACHE_OK;
> }
> #endif
>
> Here I'd like to strip the ifdef and add an attribute __maybe_unused
> to the function definition.
>
> A more complicated example:
>
> void foo() {
> #ifdef ENABLE_SRV6
> if (!from_host) {
> if (is_srv6_packet(ip6) && srv6_lookup_sid(&ip6->daddr)) {
> /* This packet is destined to an SID so we need to decapsulate it
> * and forward it.
> */
> ep_tail_call(ctx, CILIUM_CALL_SRV6_DECAP);
> return DROP_MISSED_TAIL_CALL;
> }
> }
> #endif /* ENABLE_SRV6 */
> }
>
> Here I'd like to remove the ifdef and transform the wrapped "natural"
> if to something like
>
> if (is_defined(ENABLE_SRV6) && !from_host) ...
>
> Are such transformations possible with coccinelle?
I don't think so, sorry. Coccinelle currently only allows adding #ifdefs,
but not removing them.
>
> A colleague has pointed me at --ifdef-to-if. From the description of
> the flag it sounds like I wouldn't be able to distinguish a real if
> from an ifdef?
That's only useful for #ifdefs inside a function, to get the proper
control flow (actually, Coccinelle also tries to get the proper control
flow without this option, so I'm not sure what this option does
additionally).
julia
> Thanks for your work on coccinelle!
> Lorenz
>
prev parent reply other threads:[~2024-02-02 17:22 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-02 11:25 [cocci] Refactoring C preprocessor using cocci? Lorenz Bauer
2024-02-02 12:27 ` [cocci] Refactoring C preprocessor code with SmPL? Markus Elfring
2024-02-02 17:22 ` Julia Lawall [this message]
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=bc9490c0-4998-c1d8-4971-7093902e3756@inria.fr \
--to=julia.lawall@inria.fr \
--cc=cocci@inria.fr \
--cc=lorenz.bauer@isovalent.com \
/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).