Reducing the scope for a selected variable with SmPL?
Hello,
I took another look also at the implementation of the function “pci_bridge_release_resources”.
https://elixir.bootlin.com/linux/v6.17-rc2/source/drivers/pci/setup-bus.c#L1711-L1767
I got the impression then that it would become helpful to reduce the scope for a variable there.
Another SmPL script example:
@adjustment@
identifier xflags, type;
@@
-unsigned int xflags;
... when != xflags
-type =
+unsigned int
xflags = r->flags & PCI_RES_TYPE_MASK;
+type = xflags;
Questionable test result (by the software combination “Coccinelle 1.3.0” for example):
Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> /usr/bin/spatch --no-loops …/Projekte/Coccinelle/janitor/reduce_scope_for_old_flags2.cocci drivers/pci/setup-bus.c …
[no diff output]
How will clarification interests evolve further for this issue?
Regards,
Markus
Quoted 1 line
Another SmPL script example:
@adjustment@
identifier xflags, var;
@@
-unsigned int xflags;
... when != xflags
-var =
+unsigned int
xflags = r->flags & PCI_RES_TYPE_MASK;
+var = xflags;
Unfortunately, an expected diff output is also not generated then.
Another SmPL script example:
@reduced_adjustment@
identifier xflags, var;
@@
-var =
+unsigned int
xflags = r->flags & PCI_RES_TYPE_MASK;
+var = xflags;
An interesting difference display can be generated then. Do such script variants indicate data processing difficulties for the handling of SmPL exclusion specifications in combination with a selected variable declaration?
Regards,
Markus
Quoted 4 lines
An interesting difference display can be generated then. Do such script variants indicate data processing difficulties for the handling of SmPL exclusion specifications in combination with a selected variable declaration?
I guess that another SmPL script example will influence corresponding development ideas.
// See also:
// pci_bridge_release_resources()
// https://elixir.bootlin.com/linux/v6.17-rc2/source/drivers/pci/setup-bus.c#L1711-L1767
@adjustment@
identifier var;
@@
-unsigned int old_flags;
... when exists
when != old_flags
+unsigned int old_flags;
var = old_flags = r->flags & PCI_RES_TYPE_MASK;
Regards,
Markus
The when exists is what you were missing. How you arrange the rest is up to you. Sent from my iPhone
Quoted 26 lines · Markus Elfring
Quoted 5 lines
An interesting difference display can be generated then. Do such script variants indicate data processing difficulties for the handling of SmPL exclusion specifications in combination with a selected variable declaration?
I guess that another SmPL script example will influence corresponding development ideas.
// See also:
//pci_bridge_release_resources()
// https://elixir.bootlin.com/linux/v6.17-rc2/source/drivers/pci/setup-bus.c#L1711-L1767
@adjustment@
identifier var;
@@
-unsigned intold_flags;
... when existswhen != old_flags+unsigned int
old_flags;
var =old_flags=r->flags&PCI_RES_TYPE_MASK;Regards,
Markus
Quoted 1 line
The when exists is what you were missing.
I would appreciate if the application of such source code search pattern details would become clearer somehow.
Quoted 1 line
How you arrange the rest is up to you.
I see opportunities to improve related transformation approaches further.
By the way:
[PATCH] PCI: Reduce the scope for a variable in pci_bridge_release_resources()
https://lore.kernel.org/linux-pci/[email protected]/
https://lkml.org/lkml/2025/8/23/136
Regards,
Markus
Quoted 10 lines · Markus Elfring
Quoted 2 lines
The when exists is what you were missing.
I would appreciate if the application of such source code search pattern details would become clearer somehow.
Think about it. The code you are interested in at the end of the pattern is under an if in the C file. It is not the case that all paths from the top of the function reach that point. But there does exist a pattern that reaches there.
Quoted 14 lines
Quoted 3 lines
How you arrange the rest is up to you.
I see opportunities to improve related transformation approaches further.
By the way:
[PATCH] PCI: Reduce the scope for a variable inpci_bridge_release_resources()
https://lore.kernel.org/linux-pci/[email protected]/
https://lkml.org/lkml/2025/8/23/136Regards,
Markus
Quoted 1 line
I guess that another SmPL script example will influence corresponding development ideas.
// See also:
// [PATCH v6 1/7] ALSA: usb-audio: Add initial driver for TASCAM US-144MKII
// https://lore.kernel.org/linux-sound/[email protected]/
//
// tascam_probe()
//
// https://wiki.sei.cmu.edu/confluence/display/c/DCL19-C.+Minimize+the+scope+of+variables+and+functions
@adjustment@
attribute name __free;
identifier i;
type t;
@@
-t *handshake_buf __free(i) = NULL;
... when != handshake_buf
when exists
+t *
handshake_buf
+__free(i)
= kmalloc(...);
Regards,
Markus
Quoted 14 lines · Markus Elfring
Quoted 1 line
I guess that another SmPL script example will influence corresponding development ideas.
// See also:
// [PATCH v6 1/7] ALSA: usb-audio: Add initial driver for TASCAM US-144MKII
// https://lore.kernel.org/linux-sound/[email protected]/
//
//tascam_probe()
//
// https://wiki.sei.cmu.edu/confluence/display/c/DCL19-C.+Minimize+the+scope+of+variables+and+functions
@adjustment@
attribute name__free;
identifier i;
type t;
@@
Review
Quoted 7 lines
(quoted diff) +2
-1-t *handshake_buf __free(i) = NULL; ... when != handshake_buf when exists +t * handshake_buf +__free(i) = kmalloc(...);
This is not correct. There may exist some path where there is no reference to handshake_buf, but that doesn't mean that that is the property of all paths.
julia
Quoted 8 lines
Quoted 8 lines
@adjustment@
attribute name__free;
identifier i;
type t;
@@
-t*handshake_buf__free(i)= NULL;
... when !=handshake_buf
when exists
Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +2
-0+t * handshake_buf +__free(i) = kmalloc(...);This is not correct. There may exist some path where there is no reference to
handshake_buf, but that doesn't mean that that is the property of all paths.
Do you propose to generalise such a source code transformation approach any further?
Regards,
Markus
Quoted 18 lines · Markus Elfring
Quoted 8 lines
Quoted 8 lines
@adjustment@
attribute name__free;
identifier i;
type t;
@@
-t*handshake_buf__free(i)= NULL;
... when !=handshake_buf
when existsQuoted 8 lines
Review
Quoted 4 lines
(quoted diff) +2
-0+t * handshake_buf +__free(i) = kmalloc(...);This is not correct. There may exist some path where there is no reference to
handshake_buf, but that doesn't mean that that is the property of all paths.
Quoted 2 lines
Do you propose to generalise such a source code transformation approach any further?
I don't propose to do anything. If you want to do something like this, you have to check that handshake_buf doesn't occur on other paths.
julia
…>>>> ... when != handshake_buf
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
when exists
…>> Do you propose to generalise such a source code transformation approach
Quoted 4 lines
Quoted 1 line
any further?
I don't propose to do anything. If you want to do something like this, you have to check that
handshake_bufdoesn't occur on other paths.
Do you find the mentioned SmPL code exclusion specification insufficient so far?
Regards,
Markus