cocci.inria.fr archive mirror
 help / color / mirror / 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, other threads:[~2019-07-31 11:27 UTC | newest]

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

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).