All of lore.kernel.org
 help / color / mirror / Atom feed
* [Cocci] Checking if a line of code *does not* exist
@ 2019-05-24 18:13 Timur Tabi
  2019-05-24 19:48 ` Julia Lawall
  0 siblings, 1 reply; 9+ messages in thread
From: Timur Tabi @ 2019-05-24 18:13 UTC (permalink / raw)
  To: cocci

I'm working on a new script that needs to add a NULL-pointer check if
one does not already exist.  For example:

x = (MYTYPE)MACRO(params);

should become

x = (MYTYPE)MACRO(params);
if (!x) {
    do_something;
}

My problem is that I want coccinelle to add the if-check only if it
doesn't already exist.  So currently I have this:

@@
type T;
expression x;
@@
x = (T)MACRO(...);
+if (x == NULL)
+{
+    status = ERROR;
+    goto exit;
+}

How do I make sure that these lines are not added if they already
exist?  I need to check for "if (x == NULL)" and "if (!x)" variants.
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Checking if a line of code *does not* exist
  2019-05-24 18:13 [Cocci] Checking if a line of code *does not* exist Timur Tabi
@ 2019-05-24 19:48 ` Julia Lawall
  2019-05-24 20:07   ` Timur Tabi
  0 siblings, 1 reply; 9+ messages in thread
From: Julia Lawall @ 2019-05-24 19:48 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Fri, 24 May 2019, Timur Tabi wrote:

> I'm working on a new script that needs to add a NULL-pointer check if
> one does not already exist.  For example:
>
> x = (MYTYPE)MACRO(params);
>
> should become
>
> x = (MYTYPE)MACRO(params);
> if (!x) {
>     do_something;
> }
>
> My problem is that I want coccinelle to add the if-check only if it
> doesn't already exist.  So currently I have this:
>
> @@
> type T;
> expression x;
> @@
> x = (T)MACRO(...);
> +if (x == NULL)
> +{
> +    status = ERROR;
> +    goto exit;
> +}
>
> How do I make sure that these lines are not added if they already
> exist?  I need to check for "if (x == NULL)" and "if (!x)" variants.

(
A
B
|
A
+B
)

Just put the == NULL variant.  An isomorphism will take care of the !x
case.

julia




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

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

* Re: [Cocci] Checking if a line of code *does not* exist
  2019-05-24 19:48 ` Julia Lawall
@ 2019-05-24 20:07   ` Timur Tabi
  2019-05-24 20:12     ` Julia Lawall
  0 siblings, 1 reply; 9+ messages in thread
From: Timur Tabi @ 2019-05-24 20:07 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci, Timur Tabi

On Fri, May 24, 2019 at 2:48 PM Julia Lawall <julia.lawall@lip6.fr> wrote:
>
>
>
> On Fri, 24 May 2019, Timur Tabi wrote:
>
> > I'm working on a new script that needs to add a NULL-pointer check if
> > one does not already exist.  For example:
> >
> > x = (MYTYPE)MACRO(params);
> >
> > should become
> >
> > x = (MYTYPE)MACRO(params);
> > if (!x) {
> >     do_something;
> > }
> >
> > My problem is that I want coccinelle to add the if-check only if it
> > doesn't already exist.  So currently I have this:
> >
> > @@
> > type T;
> > expression x;
> > @@
> > x = (T)MACRO(...);
> > +if (x == NULL)
> > +{
> > +    status = ERROR;
> > +    goto exit;
> > +}
> >
> > How do I make sure that these lines are not added if they already
> > exist?  I need to check for "if (x == NULL)" and "if (!x)" variants.
>
> (
> A
> B
> |
> A
> +B
> )

I can't quite get this to work:

func(...) {
<+...
 x = (T)MACRO(...);
(
 if (x == NULL)
|
+if (x == NULL)
+{
+    goto label;
+}
)
...+>
}

This gives me:
minus: parse error:
  File "/home/ttabi/boardobj.cocci", line 12, column 0, charpos = 157
  around = '|',
  whole content = |

Line 12 is the |



>
> Just put the == NULL variant.  An isomorphism will take care of the !x
> case.
>
> julia
>
>
>
>
> > _______________________________________________
> > Cocci mailing list
> > Cocci@systeme.lip6.fr
> > https://systeme.lip6.fr/mailman/listinfo/cocci
> >
> _______________________________________________
> Cocci mailing list
> Cocci@systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Checking if a line of code *does not* exist
  2019-05-24 20:07   ` Timur Tabi
@ 2019-05-24 20:12     ` Julia Lawall
  2019-05-29 19:33       ` Timur Tabi
  0 siblings, 1 reply; 9+ messages in thread
From: Julia Lawall @ 2019-05-24 20:12 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Fri, 24 May 2019, Timur Tabi wrote:

> On Fri, May 24, 2019 at 2:48 PM Julia Lawall <julia.lawall@lip6.fr> wrote:
> >
> >
> >
> > On Fri, 24 May 2019, Timur Tabi wrote:
> >
> > > I'm working on a new script that needs to add a NULL-pointer check if
> > > one does not already exist.  For example:
> > >
> > > x = (MYTYPE)MACRO(params);
> > >
> > > should become
> > >
> > > x = (MYTYPE)MACRO(params);
> > > if (!x) {
> > >     do_something;
> > > }
> > >
> > > My problem is that I want coccinelle to add the if-check only if it
> > > doesn't already exist.  So currently I have this:
> > >
> > > @@
> > > type T;
> > > expression x;
> > > @@
> > > x = (T)MACRO(...);
> > > +if (x == NULL)
> > > +{
> > > +    status = ERROR;
> > > +    goto exit;
> > > +}
> > >
> > > How do I make sure that these lines are not added if they already
> > > exist?  I need to check for "if (x == NULL)" and "if (!x)" variants.
> >
> > (
> > A
> > B
> > |
> > A
> > +B
> > )
>
> I can't quite get this to work:
>
> func(...) {
> <+...
>  x = (T)MACRO(...);
> (
>  if (x == NULL)

You can't just put the header of an if.  You have to put the whole thing:

if (x == NULL) S1 else S2

julia

> |
> +if (x == NULL)
> +{
> +    goto label;
> +}
> )
> ...+>
> }
>
> This gives me:
> minus: parse error:
>   File "/home/ttabi/boardobj.cocci", line 12, column 0, charpos = 157
>   around = '|',
>   whole content = |
>
> Line 12 is the |
>
>
>
> >
> > Just put the == NULL variant.  An isomorphism will take care of the !x
> > case.
> >
> > julia
> >
> >
> >
> >
> > > _______________________________________________
> > > Cocci mailing list
> > > Cocci@systeme.lip6.fr
> > > https://systeme.lip6.fr/mailman/listinfo/cocci
> > >
> > _______________________________________________
> > Cocci mailing list
> > Cocci@systeme.lip6.fr
> > https://systeme.lip6.fr/mailman/listinfo/cocci
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Checking if a line of code *does not* exist
  2019-05-24 20:12     ` Julia Lawall
@ 2019-05-29 19:33       ` Timur Tabi
  2019-05-29 20:26         ` Timur Tabi
  0 siblings, 1 reply; 9+ messages in thread
From: Timur Tabi @ 2019-05-29 19:33 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci, Timur Tabi

On Fri, May 24, 2019 at 3:12 PM Julia Lawall <julia.lawall@lip6.fr> wrote:
> You can't just put the header of an if.  You have to put the whole thing:
>
> if (x == NULL) S1 else S2

Ok that works.

Can the (|) expressions be nested?  I'm trying to match multiple
possibilities, and I can't really get them to work.  I need to match
multiple macro names (MACRO1, MACRO2, etc), and the typecast before
the macro call is optional (but should be preserved), and it should
ignore lines that already have a null-pointer check after them.

I came up with this, but it doesn't work:

@@
identifier func;
fresh identifier label = func ## "_exit";
type T;
expression x;
statement S;
@@
func(...) {
<+...
(
 x =
 (
  (T)
 |
 )
 (MACRO1|MACRO2|MACRO3)
 (...);
 if (x == NULL) S
|
 x =
 (
  (T)
 |
 )
 (MACRO1|MACRO2|MACRO3)
(...);
+if (x == NULL)
+{
+    status = ERROR;
+    goto label;
+}
)
...+>
}

I get:

minus: parse error:
  File "/home/ttabi/boardobj.cocci", line 14, column 1, charpos = 135
  around = '|',
  whole content =  |
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Checking if a line of code *does not* exist
  2019-05-29 19:33       ` Timur Tabi
@ 2019-05-29 20:26         ` Timur Tabi
  2019-05-29 20:31           ` Julia Lawall
  0 siblings, 1 reply; 9+ messages in thread
From: Timur Tabi @ 2019-05-29 20:26 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci

On Wed, May 29, 2019 at 2:33 PM Timur Tabi <timur@kernel.org> wrote:
> minus: parse error:
>   File "/home/ttabi/boardobj.cocci", line 14, column 1, charpos = 135
>   around = '|',
>   whole content =  |

I did some reading of the documentation and came up with this.  I
think it's in improvement, but it still doesn't work.

@@
identifier func;
fresh identifier label = func ## "_exit";
type T;
expression x;
statement S;
@@
func(...) {
<+...
 x =
(
 (T)
 \(MACRO1\|MACRO2\|MACRO3\)
|
 \(MACRO1\|MACRO2\|MACRO3\)
)
 (...);
(
 if (x == NULL) S
|
+if (x == NULL)
+{
+    status = ERROR;
+    goto label;
+}
)
...+>
}

This gives me:
21: no available token to attach to

So it looks like the "\(MACRO1\|MACRO2\|MACRO3\)" isn't correct, but I
don't see what's wrong with it.
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Checking if a line of code *does not* exist
  2019-05-29 20:26         ` Timur Tabi
@ 2019-05-29 20:31           ` Julia Lawall
  2019-05-29 20:59             ` Timur Tabi
  0 siblings, 1 reply; 9+ messages in thread
From: Julia Lawall @ 2019-05-29 20:31 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci

Le 29.05.2019 22:26, Timur Tabi a écrit :
> On Wed, May 29, 2019 at 2:33 PM Timur Tabi <timur@kernel.org> wrote:
>> minus: parse error:
>>   File "/home/ttabi/boardobj.cocci", line 14, column 1, charpos = 135
>>   around = '|',
>>   whole content =  |
> 
> I did some reading of the documentation and came up with this.  I
> think it's in improvement, but it still doesn't work.
> 
> @@
> identifier func;
> fresh identifier label = func ## "_exit";
> type T;
> expression x;
> statement S;
> @@
> func(...) {
> <+...
>  x =
> (
>  (T)
>  \(MACRO1\|MACRO2\|MACRO3\)
> |
>  \(MACRO1\|MACRO2\|MACRO3\)
> )
>  (...);
> (
>  if (x == NULL) S
> |
> +if (x == NULL)
> +{
> +    status = ERROR;
> +    goto label;
> +}
> )
> ...+>
> }
> 
> This gives me:
> 21: no available token to attach to
> 
> So it looks like the "\(MACRO1\|MACRO2\|MACRO3\)" isn't correct, but I
> don't see what's wrong with it.


The + code has to be adjacent to some - or context code.  So you may 
need to duplicate some   code in the different branches of the 
disjunction.

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

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

* Re: [Cocci] Checking if a line of code *does not* exist
  2019-05-29 20:31           ` Julia Lawall
@ 2019-05-29 20:59             ` Timur Tabi
  2019-05-30  0:24               ` Julia Lawall
  0 siblings, 1 reply; 9+ messages in thread
From: Timur Tabi @ 2019-05-29 20:59 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci, Timur Tabi

On Wed, May 29, 2019 at 3:31 PM Julia Lawall <Julia.Lawall@lip6.fr> wrote:
> The + code has to be adjacent to some - or context code.  So you may
> need to duplicate some   code in the different branches of the
> disjunction.

So you're saying that the context code can't be a disjunction?

I broke my script up into two rules, and it works now.  It's not
really that elegant, but it works.  Thanks.
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

* Re: [Cocci] Checking if a line of code *does not* exist
  2019-05-29 20:59             ` Timur Tabi
@ 2019-05-30  0:24               ` Julia Lawall
  0 siblings, 0 replies; 9+ messages in thread
From: Julia Lawall @ 2019-05-30  0:24 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci



On Wed, 29 May 2019, Timur Tabi wrote:

> On Wed, May 29, 2019 at 3:31 PM Julia Lawall <Julia.Lawall@lip6.fr> wrote:
> > The + code has to be adjacent to some - or context code.  So you may
> > need to duplicate some   code in the different branches of the
> > disjunction.
>
> So you're saying that the context code can't be a disjunction?

Sort of.  If the context code is something that you want to attach added
code to, then no it cannot be a disjunction.

julia

>
> I broke my script up into two rules, and it works now.  It's not
> really that elegant, but it works.  Thanks.
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

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

end of thread, other threads:[~2019-05-30  0:24 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-24 18:13 [Cocci] Checking if a line of code *does not* exist Timur Tabi
2019-05-24 19:48 ` Julia Lawall
2019-05-24 20:07   ` Timur Tabi
2019-05-24 20:12     ` Julia Lawall
2019-05-29 19:33       ` Timur Tabi
2019-05-29 20:26         ` Timur Tabi
2019-05-29 20:31           ` Julia Lawall
2019-05-29 20:59             ` Timur Tabi
2019-05-30  0:24               ` Julia Lawall

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.