>> @@
>> -rc = x;
>
> Replace by - lrc@rc = x;

I have constructed further test scripts for the semantic patch language.

@display1@
expression x;
identifier rc;
local idexpression lrc;
@@
(
 if (...)
*{
*lrc@rc = x;
*return lrc;
*}
|
*lrc@rc = x;
*return lrc;
)

@display2@
identifier display1.rc;
type t;
@@
*t rc;
 ... when != rc


The following test result is generated so far for an example.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm64/kernel/ptrace.c?id=08f103b9a9502974109fab47ea35ca8542c4e57a

elfring@Sonne:~/Projekte/Linux/next-patched> spatch 
~/Projekte/Coccinelle/janitor/show_code_for_moving_to_return1.cocci 
arch/arm64/kernel/ptrace.c
…
     (ONCE) already tagged but only removed, so safe
…
@@ -290,7 +290,6 @@ static struct perf_event *ptrace_hbp_cre
 {
        struct perf_event *bp;
        struct perf_event_attr attr;
-       int err, type;

        switch (note_type) {
        case NT_ARM_HW_BREAK:
@@ -329,7 +328,6 @@ static int ptrace_hbp_fill_attr_ctrl(uns
                                     struct arch_hw_breakpoint_ctrl ctrl,
                                     struct perf_event_attr *attr)
 {
-       int err, len, type, offset, disabled = !ctrl.enabled;

        attr->disabled = disabled;
        if (disabled)
@@ -434,10 +432,6 @@ static int ptrace_hbp_set_ctrl(unsigned
        struct arch_hw_breakpoint_ctrl ctrl;

        bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
-       if (IS_ERR(bp)) {
-               err = PTR_ERR(bp);
-               return err;
-       }

        attr = bp->attr;
        decode_ctrl_reg(uctrl, &ctrl);
@@ -458,15 +452,9 @@ static int ptrace_hbp_set_addr(unsigned
        struct perf_event_attr attr;

        bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
-       if (IS_ERR(bp)) {
-               err = PTR_ERR(bp);
-               return err;
-       }

        attr = bp->attr;
        attr.bp_addr = addr;
-       err = modify_user_hw_breakpoint(bp, &attr);
-       return err;
 }

 #define PTRACE_HBP_ADDR_SZ     sizeof(u64)



I suggest to compare this output with the following source code transformation 
approach.

@replacement@
expression x;
identifier rc;
local idexpression lrc;
@@
-lrc@rc = x;
 return
-       rc
+       x
 ;

@deletion@
identifier replacement.rc;
type t;
@@
-t rc;
 ... when != rc


elfring@Sonne:~/Projekte/Linux/next-patched> spatch 
~/Projekte/Coccinelle/janitor/simplify_return2.cocci arch/arm64/kernel/ptrace.c
…
@@ -435,8 +435,7 @@ static int ptrace_hbp_set_ctrl(unsigned

        bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
        if (IS_ERR(bp)) {
-               err = PTR_ERR(bp);
-               return err;
+               return PTR_ERR(bp);
        }

        attr = bp->attr;
@@ -453,20 +452,17 @@ static int ptrace_hbp_set_addr(unsigned
                               unsigned long idx,
                               u64 addr)
 {
-       int err;
        struct perf_event *bp;
        struct perf_event_attr attr;

        bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
        if (IS_ERR(bp)) {
-               err = PTR_ERR(bp);
-               return err;
+               return PTR_ERR(bp);
        }

        attr = bp->attr;
        attr.bp_addr = addr;
-       err = modify_user_hw_breakpoint(bp, &attr);
-       return err;
+       return modify_user_hw_breakpoint(bp, &attr);
 }

 #define PTRACE_HBP_ADDR_SZ     sizeof(u64)


* Why are the functions “ptrace_hbp_create” and “ptrace_hbp_fill_attr_ctrl”
  presented here?

* Why is the first variable declaration from the function “ptrace_hbp_set_addr”
  not marked by the SmPL asterisk functionality in this test example?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to