Coccinelle Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: elfring@users.sourceforge.net (SF Markus Elfring)
To: cocci@systeme.lip6.fr
Subject: [Cocci] Finding unstored return values with SmPL
Date: Mon, 20 Jul 2015 13:31:40 +0200	[thread overview]
Message-ID: <55ACDC1C.1040005@users.sourceforge.net> (raw)
In-Reply-To: <alpine.DEB.2.02.1507181351130.2042@localhost6.localdomain6>

> Do you really need to use a regular expression here?
> Why not use a disjunction in the pattern?

Another source code analysis approach like the following might make
the involved software design decisions a bit more clear.


@show_unstored_return_values@
identifier allocation =~ "^(?x)
(?:
   pthread_mutex_(?:try|un)?lock
|
   sigaction
)$", structure_var, var, work;
field element;
struct structure_type;
type data_type, return_type;
@@
 return_type work(...)
 {
  <+...
(
  data_type var = allocation(...);
|
  structure_type structure_var = { ... .element = allocation(...) ... };
|
(
  var
|
  structure_var
)
  = allocation(...)
|
* (void) allocation(...);
|
* allocation(...)
)
  ...+>
 }


elfring at Sonne:~/Projekte/Coccinelle/janitor> spatch.opt -sp-file show_unstored_return_values2.cocci unchecked_return_values1.c
init_defs_builtins: /usr/local/lib/coccinelle/standard.h
181 182
Fatal error: exception Failure("meta: parse error: \n = File \"show_unstored_return_values2.cocci\", line 9, column 21,  charpos = 181\n    around = ';', whole content = struct structure_type;\n")


How can this "surprise" be fixed?



> How is it useful to match just thf function call.

Does the following small source code example contain a few update candidates
for further considerations?

#include <pthread.h>

static pthread_mutex_t my_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned long my_counter = 1;

void increment_a_shared_variable(void)
{
 pthread_mutex_lock(&my_lock);
 ++my_counter;
 pthread_mutex_unlock(&my_lock);
}



> This doesn't show anything about whether or not the return value is stored.

Does the following SmPL script draft show another interesting application
of the semantic patch language?

@show_unstored_return_values@
identifier allocation =~ "^(?x)
(?:
   pthread_mutex_(?:try|un)?lock
|
   sigaction
)$", var, work;
type data_type, return_type;
@@
 return_type work(...)
 {
  <+...
(
  data_type var = allocation(...);
|
  var = allocation(...)
|
* (void) allocation(...);
|
* allocation(...)
)
  ...+>
 }


elfring at Sonne:~/Projekte/Coccinelle/janitor> spatch.opt -sp-file show_unstored_return_values3.cocci unchecked_return_values1.c
init_defs_builtins: /usr/local/lib/coccinelle/standard.h
HANDLING: unchecked_return_values1.c
diff = 
--- unchecked_return_values1.c
+++ /tmp/cocci-output-7564-385170-unchecked_return_values1.c
@@ -5,7 +5,5 @@ static unsigned long my_counter = 1;
 
 void increment_a_shared_variable(void)
 {
- pthread_mutex_lock(&my_lock);
  ++my_counter;
- pthread_mutex_unlock(&my_lock);
 }


How do you think about my imaginations for corresponding software improvements?

Regards,
Markus

  parent reply	other threads:[~2015-07-20 11:31 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-15  9:00 [Cocci] Finding unstored return values with SmPL SF Markus Elfring
2015-07-15 10:30 ` Julia Lawall
2015-07-18 11:40   ` SF Markus Elfring
2015-07-18 11:52     ` Julia Lawall
2015-07-18 12:32       ` SF Markus Elfring
2015-07-18 12:52         ` Julia Lawall
2015-07-18 13:36           ` SF Markus Elfring
2015-07-18 19:57             ` Julia Lawall
2015-07-19 13:43               ` SF Markus Elfring
2015-07-18 20:09             ` Julia Lawall
2015-07-19 12:54               ` SF Markus Elfring
2015-07-19 13:06                 ` Julia Lawall
2015-07-19 14:42                   ` SF Markus Elfring
2015-07-19 16:21                     ` Julia Lawall
2015-07-19 18:48                       ` SF Markus Elfring
2015-07-19 18:49                         ` Julia Lawall
2015-07-20 11:31       ` SF Markus Elfring [this message]
2015-07-20 11:37         ` Julia Lawall
2015-07-20 12:55           ` [Cocci] Finding designated initialisers " SF Markus Elfring
2015-07-20 13:27           ` [Cocci] Finding unstored return values " SF Markus Elfring
2015-07-20 16:28             ` Julia Lawall
2015-07-20 20:23               ` [Cocci] Finding designated initialisers " SF Markus Elfring
2015-07-20 20:38                 ` Julia Lawall
2015-07-21  5:47                   ` SF Markus Elfring
2015-08-08  8:05                     ` SF Markus Elfring
2015-08-08  8:25                       ` Julia Lawall
2015-08-08  8:41                         ` SF Markus Elfring
2015-08-08 11:40                       ` Julia Lawall
2015-08-08 11:58                         ` SF Markus Elfring
2015-08-08 12:10                           ` Julia Lawall
2015-08-08 12:56                             ` SF Markus Elfring
2015-07-22 17:42         ` [Cocci] Finding unstored return values " SF Markus Elfring
2015-07-22 17:44           ` Julia Lawall
2015-07-23  5:20             ` SF Markus Elfring
2015-07-23  5:26               ` Julia Lawall
2015-08-05 11:00                 ` SF Markus Elfring
     [not found]                   ` <alpine.DEB.2.02.1508051434540.2198@localhost6.localdomain6>
     [not found]                     ` <55C2229E.7030409@users.sourceforge.net>
     [not found]                       ` <alpine.DEB.2.02.1508051652140.2198@localhost6.localdomain6>
     [not found]                         ` <55C2280A.6000204@users.sourceforge.net>
     [not found]                           ` <alpine.DEB.2.02.1508051718110.2198@localhost6.localdomain6>
     [not found]                             ` <55C2328C.5020405@users.sourceforge.net>
     [not found]                               ` <alpine.DEB.2.02.1508051948060.2039@localhost6.localdomain6>
2015-08-06  9:04                                 ` [Cocci] Fine-tuning for the processing of function name lists? SF 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=55ACDC1C.1040005@users.sourceforge.net \
    --to=elfring@users.sourceforge.net \
    --cc=cocci@systeme.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