All of lore.kernel.org
 help / color / mirror / Atom feed
From: oleg@redhat.com (Oleg Nesterov)
To: cocci@systeme.lip6.fr
Subject: [Cocci] Q: question on disjunction of sub-expression patterns
Date: Sat, 22 Apr 2017 18:02:02 +0200	[thread overview]
Message-ID: <20170422160202.GA23532@redhat.com> (raw)

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.

             reply	other threads:[~2017-04-22 16:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-22 16:02 Oleg Nesterov [this message]
2017-04-22 16:45 ` [Cocci] Q: question on disjunction of sub-expression patterns Julia Lawall
2017-04-23 15:08   ` Oleg Nesterov
2017-04-23 16:03     ` 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=20170422160202.GA23532@redhat.com \
    --to=oleg@redhat.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 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.