Coccinelle archive mirror
 help / color / mirror / Atom feed
From: Gaston Fernandez <gaston.fernandez@eclypsium.com>
To: cocci@inria.fr
Subject: [cocci] Help - Pattern with nested 'if' statements.
Date: Wed, 3 Sep 2025 11:04:40 -0300	[thread overview]
Message-ID: <CAE0_PArbd-jaTHr8NR-mdP7pkDPsfHVSAAw_Xe16vvexx+eVrA@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 986 bytes --]

Hi! I'm having some trouble with running a cocci pattern, and I would
like to know if it's a mistake related to my pattern, or a
coccinelle's limitation.

*### What the patch should do ###*
- The patch *[if_else_swap.cocci]* should take every instance of if/else
statements from the file *[test-nested.c]*, negate the condition and then
swap its bodies. (A double negative)

*### What I have tried ###*
- Declaring the if/else's bodies as `statements lists` variables, since I
need to store the whole content.

*### What I'd expect ###*
- Negate the if condition
- Swap the "if" body for the "else" body.

*### Result ###*
- Error: It tries to rewrite the variables multiple times. (Instead of
trying to solve from the highest/deepest?)
```
According to environment 2:
   rule.Sb -> x--;

   rule.Sa -> x++;

current modification:
MINUS
According to environment 2:

   rule.Sb -> if (x == 1) {x++;}else {x--;}

   rule.Sa -> if (x == 1) {x--;}else

   {x++;}
```

Thank you, very much!

[-- Attachment #1.2: Type: text/html, Size: 1710 bytes --]

[-- Attachment #2: if_else_swap.cocci --]
[-- Type: application/octet-stream, Size: 189 bytes --]

// This rule won't match nested if/else conditions.
@rule@
expression condition;
statement list Sa, Sb;
@@

- if (condition) 
+ if (!(condition))
{
-   Sa
+   Sb
}
else
{
-    Sb
+    Sa
}

[-- Attachment #3: test-nested.c --]
[-- Type: text/x-csrc, Size: 418 bytes --]

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int x = 0;
    int y = -1;


    if(y == 1)
    {
        if( x == 1)
        {
            x--;
        }
        else
        {
            x++;
        }
    }
    else
    {
        if( x == 1)
        {
            x++;
        }
        else
        {
            x--;
        }
    }
    return EXIT_SUCCESS;
}

             reply	other threads:[~2025-09-17 12:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-03 14:04 Gaston Fernandez [this message]
2025-09-17 13:07 ` [cocci] Reordering of if/else branches with SmPL? 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=CAE0_PArbd-jaTHr8NR-mdP7pkDPsfHVSAAw_Xe16vvexx+eVrA@mail.gmail.com \
    --to=gaston.fernandez@eclypsium.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).