From: Julia Lawall <julia.lawall@inria.fr>
To: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Cc: Julia Lawall <julia.lawall@inria.fr>, cocci@inria.fr
Subject: Re: [cocci] HELP: multiple inserts at one position
Date: Mon, 13 Mar 2023 22:09:37 +0100 (CET) [thread overview]
Message-ID: <alpine.DEB.2.22.394.2303132208060.3007@hadrien> (raw)
In-Reply-To: <20230313203847.33c9ff24@nbbrfq>
On Mon, 13 Mar 2023, Bernhard Reutner-Fischer wrote:
> On Fri, 10 Mar 2023 11:12:27 +0100 (CET)
> Julia Lawall <julia.lawall@inria.fr> wrote:
>
> > On Fri, 10 Mar 2023, Bernhard Reutner-Fischer wrote:
> >
> > > On Thu, 9 Mar 2023 09:47:17 +0100 (CET)
> > > Julia Lawall <julia.lawall@inria.fr> wrote:
> > >
> > > > On Thu, 9 Mar 2023, Julia Lawall wrote:
> > > > > On Thu, 9 Mar 2023, Bernhard Reutner-Fischer wrote:
> > > []
> > > > Another issue is that your semantic patch could be more efficient. You
> > > > have the declaration
> > > >
> > > > type mpz_t;
> > > >
> > > > I think you meant:
> > > >
> > > > typedef mpz_t;
> > > >
> > > > Then the only goal of the pattern
> > > >
> > > > mpz_t i;
> > > > ...
> > > >
> > > > seems to be to ensure the type of i. This can be done in the metavariable
> > > > declaration:
> > > >
> > > > local idexpression mpz_t i
> > > >
> > > > and then in the second rule, it would be:
> > > >
> > > > local idexpression mpz_t mpz_0_find.i;
> > > >
> > > > In this way, you will not be matching from the top of the function, but
> > > > rather only from the relevant call to the return.
> > >
> > > mhm. But if there is no return statement whatsoever in that scope?
> > >
> > > Consider:
> > > int myround (double dbl)
> > > {
> > > int ret = 0;
> > > // mpfr_t outer; mpfr_init (outer);
> > > if (global_var)
> > > {
> > > unsigned long ul;
> > > mpfr_t i;
> > > mpfr_init (i);
> > > mpfr_frac (i, dbl, MPFR_RNDZ);
> > > if (mpfr_cmp_si (i, 0) != 0)
> > > {
> > > ret = 42;
> > > moan ("round");
> > > }
> > > /* mpfr_clear (i); missing here */
> > > }
> > > // mpfr_clear (outer) insertion works, there is a return stmt.
> > > // probably breaks in a void function like in the scope above..
> > > // There is no 'i' to clear _here_, of course!
> > > return ret;
> > > }
> > > EOF
> > >
> > > If i'd understand a local idexpression 'i' to have a scope, i would have
> > > hoped to somehow get at the position of i at end of scope?
> > > But i think i need to match either a return or end-of-block, so i can
> > > insert the mpfr_clear properly in either case. Somehow.
> >
> > OK, there are some hacks to deal with the issue at the end of the
> > function, but not in the case of an arbitrary scope.
> >
> > Typically missing frees affect if branches, so the problem doesn't arise,
> > but if you want the whole scope, it seems that you would need the {}.
>
> So IIUC i would need to attach a position to the closing curly brace
> '}'.
> But how would i do that? I tried:
I would suggest ot have one set of rules that takes care of the cases
where the return is explicit in the code. Then you can have a rule like
the following one, that just takes care of the final trailing return case
(bcause youhave already taken care of the explicit returns cases):
@exists@
typedef mpfr_t;
identifier i, f;
@@
void f(...) {
... when any
mpfr_t i;
...
mpfr_init (i)
... when != mpfr_clear (i)
++mpfr_clear (i);
}
julia
>
> /// mpfr ///////////////////////////////////////////////////////////////
>
> @ mpfr_0_find exists @
> type mpfr_t;
> identifier i;
> position ret_pos, e_pos, s_pos;
> expression E0, E1;
> statement s0;
> @@
> {...
> mpfr_t i;...
> // rejected: <...{...>
> ( mpfr_init_set_str (i, ...)
> | mpfr_init (i)
> | mpfr_init2 (i, ...)
> | mpfr_init3 (i, ...)
> )
> <+... when != mpfr_clear (i)
> when != mpfr_clears (...,i,...)
> ( return \(<+...i...+>\);
> | return@ret_pos ...;
> //rejected: | }@e_pos
> )
> ...+>
> //rejected: <...}...>
> // the below does not seem to work for me (2)
> }@e_pos
>
> @ mpfr_0_replace @
> identifier mpfr_0_find.i;
> expression mpfr_0_find.E0;
> statement mpfr_0_find.s0;
> position mpfr_0_find.ret_pos;
> position mpfr_0_find.e_pos;
> position mpfr_0_find.s_pos;
> @@
> (
> ++ mpfr_clear (i) /* ret */;
> ? return@ret_pos ...;
> |
> ++ mpfr_clear (i) /* E0 */;
> // below an attempt to reference e_pos as per (2) above:
> //assertion failed: e_pos;
> //rejected: @e_pos
> //rejected, no semicolon?: E0@e_pos
> //rejected: no semicolon?:? E0@e_pos
> //the below does not work, E0 was not set
> E0@e_pos;
> |
> ++ mpfr_clear (i) /* s0 */;
> s0@s_pos;
> )
>
prev parent reply other threads:[~2023-03-13 21:09 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-09 8:28 [cocci] HELP: multiple inserts at one position Bernhard Reutner-Fischer
[not found] ` <89959f1e-b5ed-6b2c-2931-4fdd65428e89@inria.fr>
2023-03-09 8:47 ` Julia Lawall
2023-03-09 9:10 ` Bernhard Reutner-Fischer
2023-03-09 9:38 ` Julia Lawall
2023-03-09 10:15 ` Bernhard Reutner-Fischer
2023-03-09 10:23 ` Julia Lawall
2023-03-10 10:01 ` Bernhard Reutner-Fischer
2023-03-10 10:12 ` Julia Lawall
2023-03-13 19:38 ` Bernhard Reutner-Fischer
2023-03-13 21:09 ` Julia Lawall [this message]
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=alpine.DEB.2.22.394.2303132208060.3007@hadrien \
--to=julia.lawall@inria.fr \
--cc=cocci@inria.fr \
--cc=rep.dot.nop@gmail.com \
/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 read-only IMAP folder(s) and NNTP newsgroup(s).