Skip to contentSkip to conversation

Searching function implementations with more than a single return?

cocci Markus Elfring 2 participants 23 messages Jul 1, 2025 – Jul 3, 2025

lore
Share & tools
lore
#0 lore raw

Hello,

May I expect that a function like usb_ss_max_streams() can be found with the help of the following source code search pattern?
https://elixir.bootlin.com/linux/v6.16-rc3/source/include/uapi/linux/usb/ch9.h#L721-L737

Another SmPL script example:
@display@
statement list sl;
identifier i;
type t;
@@
(return ...;
|
?t i;
*sl
)

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --no-loops …/Projekte/Coccinelle/janitor/show_functions_with_more_than_single_return.cocci include/uapi/linux/usb/ch9.h …

real    0m0,245s
user    0m0,214s
sys     0m0,028s

Regards,
Markus

#1 lore raw

Re: [cocci] Searching function implementations with variable declarations and statements?

Quoted 2 lines

May I expect that a function like usb_ss_max_streams() can be found with the help of the following source code search pattern?

Another SmPL script example:
@display2@
statement list sl;
identifier i;
type t;
@@
<+...
*t i;
...+>
sl

Should any diff output be produced accordingly?

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --no-loops …/show_variable_declarations_before_statements.cocci include/uapi/linux/usb/ch9.h …

real    0m0,138s
user    0m0,101s
sys     0m0,035s

Regards,
Markus

#2 lore raw

Re: [cocci] Searching function implementations with variable declarations and statements?

Quoted 15 lines · Markus Elfring
Quoted 2 lines

May I expect that a function like usb_ss_max_streams() can be found with the help of the following source code search pattern?

Another SmPL script example:
@display2@
statement list sl;
identifier i;
type t;
@@
<+...
*t i;
...+>
sl

Quoted 1 line

Should any diff output be produced accordingly?

Probably not. Statement list metavariables only match the list of statements that occur at top level within {}.

julia

Quoted 11 lines

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --no-loops …/show_variable_declarations_before_statements.cocci include/uapi/linux/usb/ch9.h …

real    0m0,138s
user    0m0,101s
sys     0m0,035s

Regards,
Markus

#3 lore raw

Re: [cocci] Searching function implementations with variable declarations and statements?

Quoted 3 lines
Quoted 1 line

Should any diff output be produced accordingly?

Probably not. Statement list metavariables only match the list of statements that occur at top level within {}.

Do you occasionally prefer a source code search approach like the following?

Another SmPL script example:
@display2@
statement s;
identifier i;
type t;
@@
<+...
*t i;
...+>
s

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --no-loops …/Projekte/Coccinelle/janitor/show_variable_declarations_before_statements2.cocci include/… @@ -666,7 +666,6 @@ static inline int usb_endpoint_maxp(cons
…
@@ -721,7 +720,6 @@ struct usb_ss_ep_comp_descriptor {
…

real    0m0,250s
user    0m0,202s
sys     0m0,048s

Regards,
Markus

#4 lore raw

Re: [cocci] Searching function implementations with variable declarations and statements?

Quoted 6 lines · Markus Elfring
Quoted 3 lines
Quoted 1 line

Should any diff output be produced accordingly?

Probably not. Statement list metavariables only match the list of statements that occur at top level within {}.

Do you occasionally prefer a source code search approach like the following?

Quoted 10 lines

Another SmPL script example:
@display2@
statement s;
identifier i;
type t;
@@
<+...
*t i;
...+>
s

This seems more promising.

julia

Quoted 15 lines

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --no-loops …/Projekte/Coccinelle/janitor/show_variable_declarations_before_statements2.cocci include/…

(quoted diff) +0 -0
@@ -666,7 +666,6 @@ static inline int usb_endpoint_maxp(cons
…
@@ -721,7 +720,6 @@ struct usb_ss_ep_comp_descriptor {
…
real    0m0,250s
user    0m0,202s
sys     0m0,048s

Regards,
Markus

#5 lore raw

Re: [cocci] Searching function implementations with variable declarations and statements?

In reply to Markus Elfring
Quoted 2 lines

May I expect that a function like usb_ss_max_streams() can be found with the help of the following source code search pattern?

Another SmPL script example:
@display3@
statement list sl;
identifier i;
type t;
@@
<+...
t i;
...+>
*sl

Should any diff output be produced accordingly?

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --no-loops …/Projekte/Coccinelle/janitor/show_statements_after_variable_declaration.cocci include/uapi/linux/usb/ch9.h …

real    0m0,118s
user    0m0,094s
sys     0m0,025s

Regards,
Markus

#6 lore raw

Re: [cocci] Searching function implementations with variable declarations and statements?

Quoted 1 line

Should any diff output be produced accordingly?

Would you occasionally really find a source code search approach like the following more appropriate?

Another SmPL script example:
@display3@
statement s;
identifier i;
type t;
@@
t i;
<+...
*s
...+>

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --no-loops …/Projekte/Coccinelle/janitor/show_statements_after_variable_declaration2.cocci include/ …
@@ -668,7 +668,6 @@ usb_endpoint_maxp_mult(const struct usb_
…
@@ -723,17 +722,10 @@ usb_ss_max_streams(const struct usb_ss_e
…

real    0m0,243s
user    0m0,199s
sys     0m0,042s

Is there a need to reconsider the handling of curly brackets (for compound statements)?

Regards,
Markus

#7 lore raw

Re: [cocci] Searching function implementations with variable declarations and statements?

Quoted 5 lines · Markus Elfring
Quoted 1 line

Should any diff output be produced accordingly?

Would you occasionally really find a source code search approach like the following more appropriate?

Quoted 10 lines

Another SmPL script example:
@display3@
statement s;
identifier i;
type t;
@@
t i;
<+...
*s
...+>

You don't need the +

julia

Quoted 18 lines

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --no-loops …/Projekte/Coccinelle/janitor/show_statements_after_variable_declaration2.cocci include/ …

(quoted diff) +0 -0
@@ -668,7 +668,6 @@ usb_endpoint_maxp_mult(const struct usb_
…
@@ -723,17 +722,10 @@ usb_ss_max_streams(const struct usb_ss_e
…
real    0m0,243s
user    0m0,199s
sys     0m0,042s

Is there a need to reconsider the handling of curly brackets (for compound statements)?

Regards,
Markus

#8 lore raw

Re: [cocci] Searching function implementations with variable declarations and statements?

Quoted 11 lines
Quoted 10 lines

Another SmPL script example:
@display3@
statement s;
identifier i;
type t;
@@
t i;
<+...
*s
...+>

You don't need the +

Would we occasionally like to show a “list” of statements (detailed control flow)?

Regards,
Markus

#9 lore raw

Re: [cocci] Searching function implementations with variable declarations and statements?

Quoted 10 lines · Markus Elfring
Quoted 10 lines
Quoted 10 lines

Another SmPL script example:
@display3@
statement s;
identifier i;
type t;
@@
t i;
<+...
*s
...+>

Quoted 3 lines
Quoted 1 line

You don't need the +

Would we occasionally like to show a “list” of statements (detailed control flow)?

What you like doesn't really matter in this case. The only thing that is supported is when the list of statements is precisely the body of a {}. There is no unit in the cfg that a metavariable can be mapped to in the other cases.

julia

#10 lore raw
In reply to Markus Elfring
Quoted 1 line

Another SmPL script example:

for further development considerations?

@display@
identifier i;
statement s;
statement list sl;
type t;
@@
(return ...;
|t i;
(
<+...
*s
...+>
|
*sl
)
)

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --no-loops …/Projekte/Coccinelle/janitor/show_functions_with_more_than_single_return2.cocci include/uapi/linux/usb/ch9.h …
@@ -668,7 +668,6 @@ usb_endpoint_maxp_mult(const struct usb_
…
@@ -723,17 +722,10 @@ usb_ss_max_streams(const struct usb_ss_e
…

real    0m0,250s
user    0m0,217s
sys     0m0,030s

Regards,
Markus

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

Another SmPL script example:

for further development considerations?

Quoted 16 lines

@display@
identifier i;
statement s;
statement list sl;
type t;
@@
(return ...;
|t i;
(
<+...
*s
...+>
|
*sl
)
)

This one makes no sense, because return and t i; can't match the same thing. So the disjunction has no impact.

julia

Quoted 16 lines

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --no-loops …/Projekte/Coccinelle/janitor/show_functions_with_more_than_single_return2.cocci include/uapi/linux/usb/ch9.h …

(quoted diff) +0 -0
@@ -668,7 +668,6 @@ usb_endpoint_maxp_mult(const struct usb_
…
@@ -723,17 +722,10 @@ usb_ss_max_streams(const struct usb_ss_e
…
real    0m0,250s
user    0m0,217s
sys     0m0,030s

Regards,
Markus

#12 lore raw
Quoted 20 lines
Quoted 19 lines
Quoted 1 line

Another SmPL script example:

for further development considerations?

@display@
identifier i;
statement s;
statement list sl;
type t;
@@
(return ...;
|t i;
(
<+...
*s
...+>
|
*sl
)
)

Quoted 2 lines

This one makes no sense, because return and t i; can't match the same thing. So the disjunction has no impact.

Would you like to suggest the exclusion of a single return statement by a more appropriate source code search approach?

Regards,
Markus

#13 lore raw
Quoted 20 lines · Markus Elfring
Quoted 20 lines
Quoted 19 lines
Quoted 1 line

Another SmPL script example:

for further development considerations?

@display@
identifier i;
statement s;
statement list sl;
type t;
@@
(return ...;
|t i;
(
<+...
*s
...+>
|
*sl
)
)

Quoted 4 lines
Quoted 2 lines

This one makes no sense, because return and t i; can't match the same thing. So the disjunction has no impact.

Would you like to suggest the exclusion of a single return statement by a more appropriate source code search approach?

Requiring the presence of a declaration already excludes the case of a single return. If you want to exclude a single return after a declaration, then the return should be put in the disjunctions with the *s. The *sl will again never match anything, because it's not coming immediately after a {}. It comes immediately after a declaration.

julia

#14 lore raw
Quoted 4 lines
Quoted 2 lines

Would you like to suggest the exclusion of a single return statement by a more appropriate source code search approach?

Requiring the presence of a declaration already excludes the case of a single return.

Variable declarations (and definition) can also be specified after a return statement, can't they?

Quoted 4 lines

If you want to exclude a single return after a
declaration, then the return should be put in the disjunctions with the *s. The *sl will again never match anything, because it's not coming immediately after a {}. It comes immediately after a declaration.

Is the handling of curly brackets (for compound statements) still unclear so far?

Regards,
Markus

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

Would you like to suggest the exclusion of a single return statement by a more appropriate source code search approach?

Requiring the presence of a declaration already excludes the case of a single return.

Variable declarations (and definition) can also be specified after a return statement, can't they?

In terms of execution, nothing happens after a return statesment.

And anyway, after the return is not the issue here. You have a pattern that at each node in the CFG searches for either a return or a variable declaration. In the case of a return it does nothing. Since no node can match both the disjunction doesn't have any useful effect.

Quoted 9 lines
Quoted 6 lines

If you want to exclude a single return after a
declaration, then the return should be put in the disjunctions with the *s. The *sl will again never match anything, because it's not coming immediately after a {}. It comes immediately after a declaration.

Is the handling of curly brackets (for compound statements) still unclear so far?

I see nothing unclear. You can write { sl }. You can't write S1 sl S2. There exists no representation of a list of statements that sl can bind to.

julia

#16 lore raw
Quoted 2 lines

I see nothing unclear. You can write { sl }. You can't write S1 sl S2. There exists no representation of a list of statements that sl can bind to.

It seems that such bits of information point opportunities out for further contributors to improve also the software documentation accordingly.

Regards,
Markus

#17 lore raw

Re: [cocci] Search challenges for “statement lists”?

In reply to Julia Lawall
Quoted 1 line

You can write { sl }.

Such a source code search pattern can occasionally be helpful.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/b00c5adb1a485321c88450bdaa9e0d9ff93123dc/docs/manual/cocci_syntax.tex#L348-350

Quoted 1 line

You can't write S1 sl S2.

Which software constraints would hinder the application of such a metavariable combination so far?

Quoted 1 line

There exists no representation of a list of statements that sl can bind to.

Is there a need to clarify further possible software extensions?

Regards,
Markus

#18 lore raw

Re: [cocci] Using statement list combinations?

In reply to Julia Lawall
Quoted 1 line

I see nothing unclear. You can write { sl }. You can't write S1 sl S2.

Would you find an SmPL code variant (like the following) more reasonable?

@display@
statement header, footer;
statement list sl;
@@
*header { sl } footer

Regards,
Markus

#19 lore raw

Re: [cocci] Using statement list combinations?

Quoted 8 lines · Markus Elfring
Quoted 1 line

I see nothing unclear. You can write { sl }. You can't write S1 sl S2.

Would you find an SmPL code variant (like the following) more reasonable?

@display@
statement header, footer;
statement list sl;
@@
*header { sl } footer

I don't know what you expect it to match.

julia

Quoted 5 lines

Regards,
Markus

#20 lore raw

Re: [cocci] Using statement list combinations?

Quoted 9 lines
Quoted 7 lines

Would you find an SmPL code variant (like the following) more reasonable?

@display@
statement header, footer;
statement list sl;
@@
*header { sl } footer

I don't know what you expect it to match.

Such a source code search approach is very generic.
I am curious how often this code pattern could be found finally.

Regards,
Markus

#21 lore raw

Re: [cocci] Using statement list combinations?

Quoted 8 lines · Markus Elfring
Quoted 8 lines
Quoted 7 lines

Would you find an SmPL code variant (like the following) more reasonable?

@display@
statement header, footer;
statement list sl;
@@
*header { sl } footer

Quoted 4 lines
Quoted 1 line

I don't know what you expect it to match.

Such a source code search approach is very generic.
I am curious how often this code pattern could be found finally.

Probably never. People don't often make a {} between two other statements.

julia

#22 lore raw

Re: [cocci] Using statement list combinations?

Quoted 2 lines
Quoted 1 line

I am curious how often this code pattern could be found finally.

Probably never. People don't often make a {} between two other statements.

I imagine that source code analyses would occasionally care more for the implementation detail if curly brackets should enclose extra compound statements (or not).
Is there a need to mark data processing scopes better anyhow?

Regards,
Markus