cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
From: Markus Elfring <Markus.Elfring@web.de>
To: Julia Lawall <julia.lawall@lip6.fr>
Cc: Coccinelle <cocci@systeme.lip6.fr>
Subject: Re: [Cocci] Reducing source code around return statements with SmPL?
Date: Tue, 24 Sep 2019 20:20:10 +0200	[thread overview]
Message-ID: <e3e922ef-fab2-ee5c-57f1-0f36ccc682fd@web.de> (raw)
In-Reply-To: <alpine.DEB.2.21.1909231058380.2283@hadrien>

>> @@
>> -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
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

  parent reply	other threads:[~2019-09-24 18:20 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-23  8:50 [Cocci] Reducing source code around return statements with SmPL? Markus Elfring
2019-09-23  8:59 ` Julia Lawall
2019-09-23 11:17   ` Markus Elfring
2019-09-24 15:27   ` Markus Elfring
2019-09-24 15:30     ` Julia Lawall
2019-09-24 15:40       ` Markus Elfring
     [not found]         ` <alpine.DEB.2.21.1909241750490.2281@hadrien>
2019-09-24 16:00           ` Markus Elfring
     [not found]             ` <alpine.DEB.2.21.1909241804490.2281@hadrien>
2019-09-24 16:14               ` Markus Elfring
2019-09-24 19:24       ` Markus Elfring
2019-09-24 20:16         ` Julia Lawall
2019-09-24 20:35           ` Markus Elfring
2019-09-24 20:49             ` Julia Lawall
2019-09-24 20:55               ` Markus Elfring
2019-09-25  6:13               ` Markus Elfring
2019-09-25  6:22                 ` Julia Lawall
2019-09-25  6:28                   ` Markus Elfring
     [not found]                     ` <alpine.DEB.2.21.1909250837140.2482@hadrien>
2019-09-25  6:40                       ` Markus Elfring
     [not found]                         ` <alpine.DEB.2.21.1909250854190.2482@hadrien>
2019-09-25  7:00                           ` Markus Elfring
     [not found]                             ` <alpine.DEB.2.21.1909251138120.2654@hadrien>
2019-09-25 11:00                               ` Markus Elfring
2019-09-24 18:20   ` Markus Elfring [this message]
2019-09-24 20:21     ` Julia Lawall
2019-09-24 20:48       ` Markus Elfring
2019-09-24 20:52         ` Julia Lawall
2019-09-24 21:00           ` Markus Elfring
2019-09-24 21:03             ` Julia Lawall
2019-09-25  6:04               ` Markus Elfring
2019-09-25  8:22               ` Markus Elfring
2019-09-25  8:22               ` Markus Elfring
2019-09-24 18:33   ` Markus Elfring
2019-09-25 11:33   ` [Cocci] [PATCH] Coccinelle: Add a SmPL script for the reconsideration of specific combinations of assignment and return statements Markus Elfring
2019-10-01 14:30     ` [Cocci] [PATCH v2] " Markus Elfring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e3e922ef-fab2-ee5c-57f1-0f36ccc682fd@web.de \
    --to=markus.elfring@web.de \
    --cc=cocci@systeme.lip6.fr \
    --cc=julia.lawall@lip6.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).