All of lore.kernel.org
 help / color / mirror / Atom feed
* [Cocci] transforming arguments to statement macros?
@ 2020-01-02 22:03 David Young
  2020-01-03  6:32 ` Julia Lawall
  0 siblings, 1 reply; 11+ messages in thread
From: David Young @ 2020-01-02 22:03 UTC (permalink / raw)
  To: cocci

I have a semantic patch that renames parameters and local variables
called `index` to `idx`.  It renames most occurrences, however,
it does not know how to handle a macro that expands to a block:

#define HGOTO_ERROR(maj, min, ret_val, ...) {                           \
   do_something(maj, min, __VA_ARGS__);                                 \
   ret_value = ret_val;							\
   goto done;								\
}

I'd like for every occurrence of `index` in the HGOTO_ERROR() arguments
to change to `idx`, HGOTO_ERROR(..., index, ...) -> HGOTO_ERROR(..., idx, ...),
but spatch leaves those occurrences alone.

Can I write an isomorphism or something to force spatch to process each
occurrence of HGOTO_ERROR(...) as if it was either the function call
`hgoto_error(...);` or the block `{ (void)(...); goto done; }` ?


Dave

-- 
David Young
dyoung@pobox.com    Urbana, IL    (217) 721-9981
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Cocci] transforming arguments to statement macros?
  2020-01-02 22:03 [Cocci] transforming arguments to statement macros? David Young
@ 2020-01-03  6:32 ` Julia Lawall
  2020-01-03 16:03   ` David Young
  0 siblings, 1 reply; 11+ messages in thread
From: Julia Lawall @ 2020-01-03  6:32 UTC (permalink / raw)
  To: David Young; +Cc: cocci



--- Please note the new email address ---


On Thu, 2 Jan 2020, David Young wrote:

> I have a semantic patch that renames parameters and local variables
> called `index` to `idx`.  It renames most occurrences, however,
> it does not know how to handle a macro that expands to a block:
>
> #define HGOTO_ERROR(maj, min, ret_val, ...) {                           \
>    do_something(maj, min, __VA_ARGS__);                                 \
>    ret_value = ret_val;							\
>    goto done;								\
> }
>
> I'd like for every occurrence of `index` in the HGOTO_ERROR() arguments
> to change to `idx`, HGOTO_ERROR(..., index, ...) -> HGOTO_ERROR(..., idx, ...),
> but spatch leaves those occurrences alone.
>
> Can I write an isomorphism or something to force spatch to process each
> occurrence of HGOTO_ERROR(...) as if it was either the function call
> `hgoto_error(...);` or the block `{ (void)(...); goto done; }` ?

I think that the problem is that there is no ; in the uses of your macro.
So Coccinelle knows that it is some strange code, and it refuses to change
it.  If you run spatch --parse-c on a file that uses the macro, you will
see something able the code being passed.

The proper way to write such a macro, independent of Coccinelle, is as a
while do(0) loop, so that the uses can end in a semicolon.  Then there is
no possibility of strange mistakes if someone actually does put a
semicolon.  Would that be feasible to do?

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Cocci] transforming arguments to statement macros?
  2020-01-03  6:32 ` Julia Lawall
@ 2020-01-03 16:03   ` David Young
  2020-01-05  9:47     ` Markus Elfring
  2020-01-08 22:05     ` [Cocci] transforming arguments to statement macros? Julia Lawall
  0 siblings, 2 replies; 11+ messages in thread
From: David Young @ 2020-01-03 16:03 UTC (permalink / raw)
  To: cocci

On Fri, Jan 03, 2020 at 07:32:09AM +0100, Julia Lawall wrote:
> I think that the problem is that there is no ; in the uses of your macro.

I was afraid of that.

> The proper way to write such a macro, independent of Coccinelle, is as a
> while do(0) loop, so that the uses can end in a semicolon.  Then there is
> no possibility of strange mistakes if someone actually does put a
> semicolon.  Would that be feasible to do?

I would prefer that the macros were written with the do-while pattern,
but it's a legacy codebase that uses HGOTO_ERROR() no fewer than 12,000
times, and most occurrences have no semicolon.  I will have to automate
the conversion, and I guess that I cannot use spatch to do it. :-) I may
be able to write a suitable vim macro.

Thank you for your help.

Dave

-- 
David Young
dyoung@pobox.com    Urbana, IL    (217) 721-9981
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Cocci] transforming arguments to statement macros?
  2020-01-03 16:03   ` David Young
@ 2020-01-05  9:47     ` Markus Elfring
  2020-01-05 10:05       ` Julia Lawall
  2020-01-08 22:05     ` [Cocci] transforming arguments to statement macros? Julia Lawall
  1 sibling, 1 reply; 11+ messages in thread
From: Markus Elfring @ 2020-01-05  9:47 UTC (permalink / raw)
  To: David Young; +Cc: cocci

> I would prefer that the macros were written with the do-while pattern,
> but it's a legacy codebase that uses HGOTO_ERROR() no fewer than 12,000
> times, and most occurrences have no semicolon.  I will have to automate
> the conversion, and I guess that I cannot use spatch to do it.

Would you like to try out how good semicolons can be added behind
these macro calls by a small script for the semantic patch language?

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Cocci] transforming arguments to statement macros?
  2020-01-05  9:47     ` Markus Elfring
@ 2020-01-05 10:05       ` Julia Lawall
  2020-01-05 11:32         ` Markus Elfring
                           ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Julia Lawall @ 2020-01-05 10:05 UTC (permalink / raw)
  To: Markus Elfring; +Cc: cocci

On Sun, 5 Jan 2020, Markus Elfring wrote:

> > I would prefer that the macros were written with the do-while pattern,
> > but it's a legacy codebase that uses HGOTO_ERROR() no fewer than 12,000
> > times, and most occurrences have no semicolon.  I will have to automate
> > the conversion, and I guess that I cannot use spatch to do it.
>
> Would you like to try out how good semicolons can be added behind
> these macro calls by a small script for the semantic patch language?

The problem is that the arguemnt list is considered to be comments.  THere
is no way to specify to put a semicolon at the end of a comment.

The only short term suggestion I have is the following:

@r@
statement s : script:ocaml() { List.hd (Str.split (Str.regexp " *(") s) =
"ASSERT" };
statement s1,s2;
position p1;
@@

s
s1@p1

@@
statement r.s1;
position r.p1;
@@

+;
s1@p1

-------------------

This uses ASSERT as an example, because standard.h already contains

#define ASSERT(x) MACROSTATEMENT

It uses a regular expression to detect that a statement starts with the
word ASSERT, and matches the next statement as well, and then in another
rule adds a ; before the subsequent statement.  If you add the ; after the
ASSERT statement it comes out after ASSERT, not after the commented
argument list. But with this semantic patch, the ; comes out on a line of
its own, and would have to be moved up manually.  So I don't know if that
gives any real benefit.  It obviously also doesn't work if the ASSERT is
at the end of a function.

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Cocci] transforming arguments to statement macros?
  2020-01-05 10:05       ` Julia Lawall
@ 2020-01-05 11:32         ` Markus Elfring
  2020-01-05 13:54         ` Markus Elfring
  2020-01-06 12:07         ` [Cocci] Adding semicolons after macro calls Markus Elfring
  2 siblings, 0 replies; 11+ messages in thread
From: Markus Elfring @ 2020-01-05 11:32 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

> The problem is that the arguemnt list is considered to be comments.  THere
> is no way to specify to put a semicolon at the end of a comment.

This information needs further clarification.

Are there any other challenges to reconsider for the addition of a semicolon
after a closing parenthesis for the relevant macro call?

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Cocci] transforming arguments to statement macros?
  2020-01-05 10:05       ` Julia Lawall
  2020-01-05 11:32         ` Markus Elfring
@ 2020-01-05 13:54         ` Markus Elfring
  2020-01-06 12:07         ` [Cocci] Adding semicolons after macro calls Markus Elfring
  2 siblings, 0 replies; 11+ messages in thread
From: Markus Elfring @ 2020-01-05 13:54 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

> It uses a regular expression to detect that a statement starts with the
> word ASSERT, and matches the next statement as well, and then in another
> rule adds a ; before the subsequent statement.

Can related improvements for the supported programming interfaces help
to split the available information into more useful pieces?


> If you add the ; after the ASSERT statement it comes out after ASSERT,
> not after the commented argument list. But with this semantic patch,
> the ; comes out on a line of its own, and would have to be moved up manually.

Can this software behaviour be avoided if the semicolon would be inserted
by using the function “make_stmt” of Coccinelle's library in
a SmPL script rule?

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Cocci] Adding semicolons after macro calls
  2020-01-05 10:05       ` Julia Lawall
  2020-01-05 11:32         ` Markus Elfring
  2020-01-05 13:54         ` Markus Elfring
@ 2020-01-06 12:07         ` Markus Elfring
  2020-01-06 12:20           ` Julia Lawall
  2 siblings, 1 reply; 11+ messages in thread
From: Markus Elfring @ 2020-01-06 12:07 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

> The only short term suggestion I have is the following:

I wonder about the specification of two statement variables here.


Under which circumstances can a transformation approach (like the following)
work as desired?

@addition@
identifier action;
statement s;
@@
 <+...
 action(...)
+;
 s
 ...+>

elfring@Sonne:~/Projekte/Coccinelle/Probe> spatch --parse-cocci add_semicolon_behind_call1.cocci
init_defs_builtins: /usr/local/bin/../lib/coccinelle/standard.h
minus: parse error:
  File "add_semicolon_behind_call1.cocci", line 8, column 1, charpos = 70
  around = 's',
  whole content =  s


How will further improvements be achieved for data processing around macro calls?

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Cocci] Adding semicolons after macro calls
  2020-01-06 12:07         ` [Cocci] Adding semicolons after macro calls Markus Elfring
@ 2020-01-06 12:20           ` Julia Lawall
  2020-01-06 12:48             ` [Cocci] Improving support for data processing around " Markus Elfring
  0 siblings, 1 reply; 11+ messages in thread
From: Julia Lawall @ 2020-01-06 12:20 UTC (permalink / raw)
  To: Markus Elfring; +Cc: cocci



On Mon, 6 Jan 2020, Markus Elfring wrote:

> > The only short term suggestion I have is the following:
>
> I wonder about the specification of two statement variables here.
>
>
> Under which circumstances can a transformation approach (like the following)
> work as desired?
>
> @addition@
> identifier action;
> statement s;
> @@
>  <+...
>  action(...)
> +;
>  s
>  ...+>

Currently none.  The SmPL language doesn't support statements that look
like function calls but have no following ;

julia

>
> elfring@Sonne:~/Projekte/Coccinelle/Probe> spatch --parse-cocci add_semicolon_behind_call1.cocci
> init_defs_builtins: /usr/local/bin/../lib/coccinelle/standard.h
> minus: parse error:
>   File "add_semicolon_behind_call1.cocci", line 8, column 1, charpos = 70
>   around = 's',
>   whole content =  s
>
>
> How will further improvements be achieved for data processing around macro calls?
>
> Regards,
> Markus
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Cocci] Improving support for data processing around macro calls
  2020-01-06 12:20           ` Julia Lawall
@ 2020-01-06 12:48             ` Markus Elfring
  0 siblings, 0 replies; 11+ messages in thread
From: Markus Elfring @ 2020-01-06 12:48 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci

> The SmPL language doesn't support statements that look like function calls
> but have no following ;

A missing semicolon can trigger the source code interpretation in different ways.

* Can macro calls be treated similar to attributes?

* How challenging will it become to change the mentioned software limitation?
  https://github.com/coccinelle/coccinelle/issues/140

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Cocci] transforming arguments to statement macros?
  2020-01-03 16:03   ` David Young
  2020-01-05  9:47     ` Markus Elfring
@ 2020-01-08 22:05     ` Julia Lawall
  1 sibling, 0 replies; 11+ messages in thread
From: Julia Lawall @ 2020-01-08 22:05 UTC (permalink / raw)
  To: David Young; +Cc: cocci

On Fri, 3 Jan 2020, David Young wrote:

> On Fri, Jan 03, 2020 at 07:32:09AM +0100, Julia Lawall wrote:
> > I think that the problem is that there is no ; in the uses of your macro.
>
> I was afraid of that.
>
> > The proper way to write such a macro, independent of Coccinelle, is as a
> > while do(0) loop, so that the uses can end in a semicolon.  Then there is
> > no possibility of strange mistakes if someone actually does put a
> > semicolon.  Would that be feasible to do?
>
> I would prefer that the macros were written with the do-while pattern,
> but it's a legacy codebase that uses HGOTO_ERROR() no fewer than 12,000
> times, and most occurrences have no semicolon.  I will have to automate
> the conversion, and I guess that I cannot use spatch to do it. :-) I may
> be able to write a suitable vim macro.

You should now be able to update the arguments, despite the lack of a
semicolon.

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-01-08 22:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-02 22:03 [Cocci] transforming arguments to statement macros? David Young
2020-01-03  6:32 ` Julia Lawall
2020-01-03 16:03   ` David Young
2020-01-05  9:47     ` Markus Elfring
2020-01-05 10:05       ` Julia Lawall
2020-01-05 11:32         ` Markus Elfring
2020-01-05 13:54         ` Markus Elfring
2020-01-06 12:07         ` [Cocci] Adding semicolons after macro calls Markus Elfring
2020-01-06 12:20           ` Julia Lawall
2020-01-06 12:48             ` [Cocci] Improving support for data processing around " Markus Elfring
2020-01-08 22:05     ` [Cocci] transforming arguments to statement macros? Julia Lawall

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.