cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
From: Akos Pasztory <akos.pasztory@gmail.com>
To: cocci@systeme.lip6.fr
Subject: [Cocci] getting rid of implicit boolean expressions
Date: Wed, 21 Apr 2021 21:55:34 +0300	[thread overview]
Message-ID: <CAJwHcF6jc_NNGeXpPh0z7upKLXSOuprS=SPmiR-x-QdYxZiEyw@mail.gmail.com> (raw)


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

Hi,

I'm trying do the following kind of transformations:

 int x, y;
 char *p;
 bool b, c;

-b = x || !y;
+b = (x != 0) || (y == 0);

-c = !p;
+c = (p == NULL);

-if (x & 3)
+if ((x & 3) != 0)
 f();
// etc

That is: trying to eliminate implicit boolean-ness (and add parentheses as
well).

I was thinking along the lines of first finding expressions
that are in "boolean context" (part of a || or && expression,
or an if/for/while condition, maybe something else too?).
Then find sub-expressions of those that are not of the form 'E op F'
where 'op' is a comparison operator (==, !=, <=, ...).
And finally depending on whether they are pointer or integer and
whether they are negated, replace them with the above constructs (x != 0,
etc.)

Is this the right way to think about this?  Meaning does it fit the mental
model
of Coccinelle, or some other approach is needed? (E.g. it crossed my mind to
maybe match all expressions and try to filter out "unwanted" ones via
position p != { ... } constraints but that seemed infeasible.)

Thanks!

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

[-- Attachment #2: Type: text/plain, Size: 136 bytes --]

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

             reply	other threads:[~2021-04-21 19:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-21 18:55 Akos Pasztory [this message]
2021-04-21 19:16 ` [Cocci] getting rid of implicit boolean expressions Julia Lawall

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='CAJwHcF6jc_NNGeXpPh0z7upKLXSOuprS=SPmiR-x-QdYxZiEyw@mail.gmail.com' \
    --to=akos.pasztory@gmail.com \
    --cc=cocci@systeme.lip6.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 NNTP newsgroup(s).