Skip to contentSkip to conversation

Reconsidering condition check repetitions with help of SmPL?

cocci Markus Elfring 1 participant 3 messages Oct 8, 2025

lore
Share & tools
lore
#0 lore raw

Hello,

I inspected some source code.

Example:
smb2_compound_op()
https://elixir.bootlin.com/linux/v6.17.1/source/fs/smb/client/smb2inode.c#L697-L748

I noticed then a special implementation detail.

Now I would appreciate if another source code transformation can be achieved also by the means of the semantic patch language.
Thus I tried the following SmPL script variant out.

@display@
expression e, x;
statement is, es;
@@
(
*if (e)
is
else
es
<+... when != e = x
*if (e && ...)
is
else
es
...+>
|
*if (e && ...)
is
else
es
<+... when != e = x
*if (e)
is
else
es
...+>
)

Questionable test result (according to the software combination “Coccinelle 1.3.0”):

Markus_Elfring@Sonne:…/Projekte/Coccinelle/janitor> time /usr/bin/spatch --no-loops show_questionable_condition_check_repetitions.cocci …/Projekte/Linux/next-analyses/fs/smb/client/smb2inode.c …
[no diff output]
Note: processing took 72.8s: …/Projekte/Linux/next-analyses/fs/smb/client/smb2inode.c

real    1m12,835s
user    1m12,532s
sys     0m0,184s

Under which circumstances will such data processing become better supported?

Another SmPL script example for further clarifications:
@display2@
expression x;
statement is, es;
@@
(
*if (rc == 0)
is
else
es
<+... when != rc = x
*if (rc == 0 && ...)
is
else
es
...+>
|
*if (rc == 0 && ...)
is
else
es
<+... when != rc = x
*if (rc == 0)
is
else
es
...+>
)

Markus_Elfring@Sonne:…/Projekte/Coccinelle/janitor> time /usr/bin/spatch --no-loops show_questionable_condition_check_repetitions2.cocci …/Projekte/Linux/next-analyses/fs/smb/client/smb2inode.c …
warning: line 9: should rc be a metavariable?
[no diff output]

real    0m5,863s
user    0m5,728s
sys     0m0,122s

Regards,
Markus

#1 lore raw
Quoted 1 line

Another SmPL script example for further clarifications:

The SmPL code can be reduced for further development considerations.

@display3@
@@
*if (rc == 0 && ...)

    { ... }
 else
    { ... }
 <+... when != rc
*if (rc == 0)
    { ... }
 else
    { ... }

...+>

Markus_Elfring@Sonne:…/Projekte/Coccinelle/janitor> /usr/bin/spatch --parse-cocci show_questionable_condition_check_repetitions3.cocci …
@display3@
statement _S_7;
…
statement _S_0;
@@
…
warning: …: should rc be a metavariable?
…
warning: iso drop_else does not match the code below on …
…
Grep query
rc

It seems that algorithmic constraints would need corresponding improvements.

* Some code is converted to applications of unique metavariables.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/9e8fb87bd681a6f5ee9ae43df6322d2c5ee7887b/standard.iso#L492-527

* Is there a need for marking the usage of these unique metavariables in the way that found source code should not be stored further?

* May the mentioned statement variables match similar source code?

* Should the amount of captured technical information be refined?

Regards,
Markus

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

Thus I tried the following SmPL script variant out.

Another SmPL script example for further development considerations:
// See also:
// smb2_compound_op()
// https://elixir.bootlin.com/linux/v6.17.1/source/fs/smb/client/smb2inode.c#L697-L748
@display@
expression e;
statement is, is2, es, es2;
@@
*if (e && ...)
is
else
es
<+... when != e
*if (e)

    is2
 else
    es2

...+>

It looks promising on the first glance.

Markus_Elfring@Sonne:…/Projekte/Coccinelle/janitor> /usr/bin/spatch --parse-cocci show_questionable_condition_check_repetitions4.cocci …
No grep query

Unfortunately, the software run time characteristics are evolving into undesirable directions so far.

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --no-loops fs/smb/client/smb2inode.c …/Projekte/Coccinelle/janitor/show_questionable_condition_check_repetitions4.cocci …
timeout (we abort)
Fatal error: exception Coccinelle_modules.Common.Timeout

real    3m20,475s
user    3m20,006s
sys     0m0,331s

Regards,
Markus