All of lore.kernel.org
 help / color / mirror / Atom feed
* [Cocci] Q: question on disjunction of sub-expression patterns
@ 2017-04-22 16:02 Oleg Nesterov
  2017-04-22 16:45 ` Julia Lawall
  0 siblings, 1 reply; 4+ messages in thread
From: Oleg Nesterov @ 2017-04-22 16:02 UTC (permalink / raw)
  To: cocci

Hello,

I am very new to spatch and I don't even know how to formulate my question,
let me try to explain it by example.

Suppose that I want to track the usage of "+" or "-" operators, this simple
script

	@e@
	expression l, r;
	binary operator o = { -, + };
	position p;
	@@

	l o at p r

	@script:python@
	l << e.l;
	r << e.r;
	p << e.p;
	@@

	print(p[0].line,p[0].column, ':', '(%s) %s (%s)' % (l, 'OP', r))

works as expected. For example, with the following C code

	void func(void)
	{
		1 + 2 - 3;
		4 - 5 + 6;
	}

I get

	3 3 : (1) OP (2)
	3 7 : (1 + 2) OP (3)
	4 3 : (4) OP (5)
	4 7 : (4 - 5) OP (6)
	
Now. If I change the rule "e" above

	@e@
	expression l, r;
	position p;
	@@

	(
		l - at p r		// PAT1
	|
		l + at p r		// PAT2
	)

I only get

	3 7 : (1 + 2) OP (3)
	4 3 : (4) OP (5)

I could probably understand why the output for line 3 doesn't report "(1) OP (2)",
PAT1 matches and "eats" the "1 + 2" part, so PAT2 doesn't match. Not sure.

But. Could someone explain why I don't get

	4 3 : (4) OP (5)
	4 7 : (4 - 5) OP (6)

for "4 - 5 + 6" statement@line 4? IOW, why PAT2 can not match _after_ PAT1?

It looks as if only one pattern from disjunction can match in the same statement.
IOW, (PAT1 | PAT2) actually means (PAT1* | PAT2*), not (PAT1 | PAT2)*. Say,

	1 + 2 - 3 + 4 - 5 + 6 - 7;	

results in

	3 7 : (1 + 2) OP (3)
	3 15 : (1 + 2 - 3 + 4) OP (5)
	3 23 : (1 + 2 - 3 + 4 - 5 + 6) OP (7)

, only PAT1 matches. While in the case of

	1 - 2 + 3 - 4 + 5 - 6 + 7;

PAT2 "wins" and blocks PAT2 till the end of the statement:

	3 3 : (1) OP (2)
	3 11 : (1 - 2 + 3) OP (4)
	3 19 : (1 - 2 + 3 - 4 + 5) OP (6)
	
Is this all correct?

Thanks,

Oleg.

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

* [Cocci] Q: question on disjunction of sub-expression patterns
  2017-04-22 16:02 [Cocci] Q: question on disjunction of sub-expression patterns Oleg Nesterov
@ 2017-04-22 16:45 ` Julia Lawall
  2017-04-23 15:08   ` Oleg Nesterov
  0 siblings, 1 reply; 4+ messages in thread
From: Julia Lawall @ 2017-04-22 16:45 UTC (permalink / raw)
  To: cocci



On Sat, 22 Apr 2017, Oleg Nesterov wrote:

> Hello,
>
> I am very new to spatch and I don't even know how to formulate my question,
> let me try to explain it by example.
>
> Suppose that I want to track the usage of "+" or "-" operators, this simple
> script
>
> 	@e@
> 	expression l, r;
> 	binary operator o = { -, + };
> 	position p;
> 	@@
>
> 	l o at p r
>
> 	@script:python@
> 	l << e.l;
> 	r << e.r;
> 	p << e.p;
> 	@@
>
> 	print(p[0].line,p[0].column, ':', '(%s) %s (%s)' % (l, 'OP', r))
>
> works as expected. For example, with the following C code
>
> 	void func(void)
> 	{
> 		1 + 2 - 3;
> 		4 - 5 + 6;
> 	}
>
> I get
>
> 	3 3 : (1) OP (2)
> 	3 7 : (1 + 2) OP (3)
> 	4 3 : (4) OP (5)
> 	4 7 : (4 - 5) OP (6)
>
> Now. If I change the rule "e" above
>
> 	@e@
> 	expression l, r;
> 	position p;
> 	@@
>
> 	(
> 		l - at p r		// PAT1
> 	|
> 		l + at p r		// PAT2
> 	)
>
> I only get
>
> 	3 7 : (1 + 2) OP (3)
> 	4 3 : (4) OP (5)
>
> I could probably understand why the output for line 3 doesn't report "(1) OP (2)",
> PAT1 matches and "eats" the "1 + 2" part, so PAT2 doesn't match. Not sure.
>
> But. Could someone explain why I don't get
>
> 	4 3 : (4) OP (5)
> 	4 7 : (4 - 5) OP (6)
>
> for "4 - 5 + 6" statement at line 4? IOW, why PAT2 can not match _after_ PAT1?
>
> It looks as if only one pattern from disjunction can match in the same statement.
> IOW, (PAT1 | PAT2) actually means (PAT1* | PAT2*), not (PAT1 | PAT2)*. Say,
>
> 	1 + 2 - 3 + 4 - 5 + 6 - 7;
>
> results in
>
> 	3 7 : (1 + 2) OP (3)
> 	3 15 : (1 + 2 - 3 + 4) OP (5)
> 	3 23 : (1 + 2 - 3 + 4 - 5 + 6) OP (7)
>
> , only PAT1 matches. While in the case of
>
> 	1 - 2 + 3 - 4 + 5 - 6 + 7;
>
> PAT2 "wins" and blocks PAT2 till the end of the statement:
>
> 	3 3 : (1) OP (2)
> 	3 11 : (1 - 2 + 3) OP (4)
> 	3 19 : (1 - 2 + 3 - 4 + 5) OP (6)
>
> Is this all correct?

The idea with a disjunction is that if the first rule matches, then that
one wins.  Actually, ( A | B ) is encoded as A v (not A & B).

julia

>
> Thanks,
>
> Oleg.
>
> _______________________________________________
> Cocci mailing list
> Cocci at systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>

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

* [Cocci] Q: question on disjunction of sub-expression patterns
  2017-04-22 16:45 ` Julia Lawall
@ 2017-04-23 15:08   ` Oleg Nesterov
  2017-04-23 16:03     ` Julia Lawall
  0 siblings, 1 reply; 4+ messages in thread
From: Oleg Nesterov @ 2017-04-23 15:08 UTC (permalink / raw)
  To: cocci

On 04/22, Julia Lawall wrote:
>
> On Sat, 22 Apr 2017, Oleg Nesterov wrote:
>
> > It looks as if only one pattern from disjunction can match in the same statement.
> > IOW, (PAT1 | PAT2) actually means (PAT1* | PAT2*), not (PAT1 | PAT2)*. Say,
> >
> The idea with a disjunction is that if the first rule matches, then that
> one wins.  Actually, ( A | B ) is encoded as A v (not A & B).

OK, thanks a lot Julia!

Does this mean that I have to write 2 separate rules if I want to track the member
dereferences? One for "->" and another for ".", because I can't use the "operator"
metadecl in this case.

Oleg.

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

* [Cocci] Q: question on disjunction of sub-expression patterns
  2017-04-23 15:08   ` Oleg Nesterov
@ 2017-04-23 16:03     ` Julia Lawall
  0 siblings, 0 replies; 4+ messages in thread
From: Julia Lawall @ 2017-04-23 16:03 UTC (permalink / raw)
  To: cocci



On Sun, 23 Apr 2017, Oleg Nesterov wrote:

> On 04/22, Julia Lawall wrote:
> >
> > On Sat, 22 Apr 2017, Oleg Nesterov wrote:
> >
> > > It looks as if only one pattern from disjunction can match in the same statement.
> > > IOW, (PAT1 | PAT2) actually means (PAT1* | PAT2*), not (PAT1 | PAT2)*. Say,
> > >
> > The idea with a disjunction is that if the first rule matches, then that
> > one wins.  Actually, ( A | B ) is encoded as A v (not A & B).
>
> OK, thanks a lot Julia!
>
> Does this mean that I have to write 2 separate rules if I want to track the member
> dereferences? One for "->" and another for ".", because I can't use the "operator"
> metadecl in this case.

This would be safer in any case.

julia

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

end of thread, other threads:[~2017-04-23 16:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-22 16:02 [Cocci] Q: question on disjunction of sub-expression patterns Oleg Nesterov
2017-04-22 16:45 ` Julia Lawall
2017-04-23 15:08   ` Oleg Nesterov
2017-04-23 16:03     ` 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.