Skip to contentSkip to conversation

Detecting statements from the last if branch (with SmPL)?

cocci Markus Elfring 2 participants 19 messages Nov 22, 2023 – Nov 23, 2023

lore
Share & tools
lore
#0 lore raw

Hello,

Some statements in if branches correspond to exception handling approaches. Would you become interested to detect a few statements from the last if branch in a generic way (with the means of the semantic patch language)?

Regards,
Markus

#1 lore raw
Quoted 5 lines · Markus Elfring

Hello,

Some statements in if branches correspond to exception handling approaches. Would you become interested to detect a few statements from the last if branch in a generic way (with the means of the semantic patch language)?

Normally that would be an if without an else that ends in a return. So

if (...) {
...
return ...;
}

The ... on the second line will be allowed to go over gotos.

julia

Quoted 4 lines

Regards,
Markus

#2 lore raw
Quoted 10 lines
Quoted 3 lines

Some statements in if branches correspond to exception handling approaches. Would you become interested to detect a few statements from the last if branch in a generic way (with the means of the semantic patch language)?

Normally that would be an if without an else that ends in a return. So

if (...) {
...
return ...;
}

I would interpret such a simple source code search approach in the way that some if branches can be found.
But it would be uncertain then if this code block would be really the last one within a selected control flow.

Quoted 1 line

The ... on the second line will be allowed to go over gotos.

A goto statement can occasionally also occur instead of the suggested return statement.

Regards,
Markus

#3 lore raw
Quoted 11 lines · Markus Elfring
Quoted 10 lines
Quoted 3 lines

Some statements in if branches correspond to exception handling approaches. Would you become interested to detect a few statements from the last if branch in a generic way (with the means of the semantic patch language)?

Normally that would be an if without an else that ends in a return. So

if (...) {
...
return ...;
}

Quoted 4 lines

I would interpret such a simple source code search approach in the way that some if branches can be found.
But it would be uncertain then if this code block would be really the last one within a selected control flow.

It would seem that it would be the last one, because there is no else. But one could have a different optinion abou what "last one" means. If you don't want any subsequent ifs, then you can put ... after th } and put when != if (...) S1 else S2

Quoted 5 lines
Quoted 2 lines

The ... on the second line will be allowed to go over gotos.

A goto statement can occasionally also occur instead of the suggested return statement.

As I said, the ... will go over the goto onward to the return.

julia

#4 lore raw
Quoted 4 lines
Quoted 2 lines

But it would be uncertain then if this code block would be really the last one within a selected control flow.

It would seem that it would be the last one, because there is no else.

Such if statements can be distributed within function implementations.

Quoted 1 line

But one could have a different optinion abou what "last one" means.

A code review can point implementation details out for further considerations.

Additional context data can be provided around and/or within an if statement so that it would be clear that such a code block belongs only to the last if branch.

But I got the impression that the control flow analysis is more challenging for the general case.

Quoted 2 lines

If you don't want any subsequent ifs, then you can put ... after th } and put when != if (...) S1 else S2

How do you think about to count involved if statements?

Regards,
Markus

#5 lore raw
Quoted 4 lines
Quoted 2 lines

If you don't want any subsequent ifs, then you can put ... after th } and put when != if (...) S1 else S2

How do you think about to count involved if statements?

You can put a position variable on a matched if (if@p) and then use python code to add the length of p to some variable.

julia

#6 lore raw
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

If you don't want any subsequent ifs, then you can put ... after th } and put when != if (...) S1 else S2

How do you think about to count involved if statements?

You can put a position variable on a matched if (if@p) and then use python code to add the length of p to some variable.

Would you like to explain your development idea a bit more for the usage of “the length” from a position variable?

Regards,
Markus

#7 lore raw
Quoted 10 lines · Markus Elfring
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

If you don't want any subsequent ifs, then you can put ... after th } and put when != if (...) S1 else S2

How do you think about to count involved if statements?

You can put a position variable on a matched if (if@p) and then use python code to add the length of p to some variable.

Would you like to explain your development idea a bit more for the usage of “the length” from a position variable?

A position variable is an array. So something like count = count + length(p), but I don't know the proper way to tak the length of an array in python.

julia

#8 lore raw
Quoted 4 lines
Quoted 2 lines

Would you like to explain your development idea a bit more for the usage of “the length” from a position variable?

A position variable is an array.

Can such information trigger any adjustments for the software documentation?

Regards,
Markus

#9 lore raw
In reply to Julia Lawall
Quoted 4 lines
Quoted 1 line

How do you think about to count involved if statements?

You can put a position variable on a matched if (if@p) and then use python code to add the length of p to some variable.

There are programming interfaces supported to some degree. Scripting code can be used to construct customised metavariables which can be applied in subsequent SmPL rules.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/8d66511d00b27de44e5c21b60d41938d32321b1a/docs/manual/cocci_syntax.tex#L655

A position variable needs probably be applied to mark a source code place as unique for desirable adjustments.
A function like “make_position” is available for such a purpose.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/8d66511d00b27de44e5c21b60d41938d32321b1a/docs/Coccilib.3cocci#L226

How do you think about to provide a function variant which can copy relevant data from a previous position object directly?

Regards,
Markus

#10 lore raw
Quoted 10 lines · Markus Elfring
Quoted 4 lines
Quoted 1 line

How do you think about to count involved if statements?

You can put a position variable on a matched if (if@p) and then use python code to add the length of p to some variable.

There are programming interfaces supported to some degree. Scripting code can be used to construct customised metavariables which can be applied in subsequent SmPL rules.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/8d66511d00b27de44e5c21b60d41938d32321b1a/docs/manual/cocci_syntax.tex#L655

Quoted 7 lines

A position variable needs probably be applied to mark a source code place as unique for desirable adjustments.
A function like “make_position” is available for such a purpose.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/8d66511d00b27de44e5c21b60d41938d32321b1a/docs/Coccilib.3cocci#L226

How do you think about to provide a function variant which can copy relevant data from a previous position object directly?

I don't see why it would be useful. But if it is, you can define such a function.

julia

#11 lore raw
Quoted 6 lines
Quoted 4 lines

https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/8d66511d00b27de44e5c21b60d41938d32321b1a/docs/Coccilib.3cocci#L226

How do you think about to provide a function variant which can copy relevant data from a previous position object directly?

I don't see why it would be useful.

Would you find any other code variant more reasonable than an approach like the following?

@script:python selection@
places << find.pos;
marker;
@@
place = places[-1]
coccinelle.marker = cocci.make_position(place.file,
place.current_element, int(place.line), int(place.column), int(place.line_end), int(place.column_end))

Regards,
Markus

#12 lore raw
Quoted 10 lines · Markus Elfring
Quoted 6 lines
Quoted 4 lines

https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/8d66511d00b27de44e5c21b60d41938d32321b1a/docs/Coccilib.3cocci#L226

How do you think about to provide a function variant which can copy relevant data from a previous position object directly?

I don't see why it would be useful.

Would you find any other code variant more reasonable than an approach like the following?

Quoted 11 lines

@script:python selection@
places << find.pos;
marker;
@@
place = places[-1]
coccinelle.marker = cocci.make_position(place.file,
place.current_element, int(place.line), int(place.column), int(place.line_end), int(place.column_end))

I don't know python. It could be ok.

julia

#13 lore raw
Quoted 22 lines
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines

https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/8d66511d00b27de44e5c21b60d41938d32321b1a/docs/Coccilib.3cocci#L226

How do you think about to provide a function variant which can copy relevant data from a previous position object directly?

I don't see why it would be useful.

Would you find any other code variant more reasonable than an approach like the following?

Quoted 11 lines

@script:python selection@
places << find.pos;
marker;
@@
place = places[-1]
coccinelle.marker = cocci.make_position(place.file,
place.current_element, int(place.line), int(place.column), int(place.line_end), int(place.column_end))

Quoted 1 line

I don't know python. It could be ok.

Would any other developers get into the mood to add more constructive comments according to shown implementation details?

Regards,
Markus

#14 lore raw
In reply to Julia Lawall
Quoted 2 lines

You can put a position variable on a matched if (if@p) and then use python code to add the length of p to some variable.

I would like to point another challenge out for the development of the discussed control flow analysis algorithm.
I got the impression that script rules are evaluated as often as some source code matches according to a previous SmPL rule.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/8d66511d00b27de44e5c21b60d41938d32321b1a/docs/manual/cocci_syntax.tex#L655

I imagine then that it would occasionally be more helpful to execute a special script rule only once while it got complete position information. How will ideas evolve further here?

Regards,
Markus

#15 lore raw
Quoted 9 lines · Markus Elfring
Quoted 2 lines

You can put a position variable on a matched if (if@p) and then use python code to add the length of p to some variable.

I would like to point another challenge out for the development of the discussed control flow analysis algorithm.
I got the impression that script rules are evaluated as often as some source code matches according to a previous SmPL rule.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/8d66511d00b27de44e5c21b60d41938d32321b1a/docs/manual/cocci_syntax.tex#L655

Quoted 3 lines

I imagine then that it would occasionally be more helpful to execute a special script rule only once while it got complete position information. How will ideas evolve further here?

Script rules are executed once per unique match. Nothing will change about that.

julia

#16 lore raw
Quoted 1 line

Script rules are executed once per unique match.

Does such an information mean also that each script rule can not know that it would be executed several times because of repeatedly matched source code?

Regards,
Markus

#17 lore raw
Quoted 4 lines · Markus Elfring
Quoted 1 line

Script rules are executed once per unique match.

Does such an information mean also that each script rule can not know that it would be executed several times because of repeatedly matched source code?

I said once per unique match. If all of the values of the metavariables imported by the scrip are repeated by a second match, the script code is not executed again.

julia

#18 lore raw
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Script rules are executed once per unique match.

Does such an information mean also that each script rule can not know that it would be executed several times because of repeatedly matched source code?

I said once per unique match. If all of the values of the metavariables imported by the scrip are repeated by a second match, the script code is not executed again.

It seems that script rules can not determine the repetition incidence for matched source code by themselves.

Regards,
Markus