Coccinelle archive on lore.kernel.org
 help / color / Atom feed
* [Cocci] How to match all conditions in if-else if blocks?
@ 2019-07-30 14:43 Chuhong Yuan
  2019-07-31 11:27 ` Julia Lawall
  0 siblings, 1 reply; 2+ messages in thread
From: Chuhong Yuan @ 2019-07-30 14:43 UTC (permalink / raw)
  To: cocci

Hi all,
I want to write a script to match a pattern of conditional statement.
However, I find that I can only match the first "if" in the block.
The script is like this:
fn(...) {
...
- f(arg1, arg2, arg3)
...
}
where f returns the condition check result.

But when I apply to source code like this:
if (f(a1, a2, a3)) {
...
} else if (f(a4, a5, a6)) {
...
}
I find it can only match f(a1, a2, a3).
So how to fix this problem?

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

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

* Re: [Cocci] How to match all conditions in if-else if blocks?
  2019-07-30 14:43 [Cocci] How to match all conditions in if-else if blocks? Chuhong Yuan
@ 2019-07-31 11:27 ` Julia Lawall
  0 siblings, 0 replies; 2+ messages in thread
From: Julia Lawall @ 2019-07-31 11:27 UTC (permalink / raw)
  To: Chuhong Yuan; +Cc: cocci



On Tue, 30 Jul 2019, Chuhong Yuan wrote:

> Hi all,
> I want to write a script to match a pattern of conditional statement.
> However, I find that I can only match the first "if" in the block.
> The script is like this:
> fn(...) {
> ...
> - f(arg1, arg2, arg3)
> ...
> }
> where f returns the condition check result.

... means that what is before and after should not appear in the code
matched by ...  This is useful for patterns like

A
...
B

when you want the closest A and B.

You can remove this restriction using

... when any

However that will not completely solve your problem, because when
transformation is used with ..., the patterns have to appear on every
control-flow path.

To eliminate this constraint, you can use

fn(...) {
<...
- f(...)
...> }

The <... ...> allows the pattern to appear 0 or more times in the matched
region.  If you want to be sure that it appears one or more times, you can
use <+... ...+>.  But the latter is more expensive and may not be
necessary.

On the other hand, if the name of the function is not very important, it
would be much more efficient to just match the function call that you are
interested in directly, without putting the function definition pattern
around it.

julia


>
> But when I apply to source code like this:
> if (f(a1, a2, a3)) {
> ...
> } else if (f(a4, a5, a6)) {
> ...
> }
> I find it can only match f(a1, a2, a3).
> So how to fix this problem?
>
> Thanks,
> Chuhong
> _______________________________________________
> 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] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-30 14:43 [Cocci] How to match all conditions in if-else if blocks? Chuhong Yuan
2019-07-31 11:27 ` Julia Lawall

Coccinelle archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/cocci/0 cocci/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 cocci cocci/ https://lore.kernel.org/cocci \
		cocci@systeme.lip6.fr cocci@archiver.kernel.org
	public-inbox-index cocci


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/fr.lip6.systeme.cocci


AGPL code for this site: git clone https://public-inbox.org/ public-inbox