Coccinelle archive on lore.kernel.org
 help / Atom feed
* [Cocci] replacing defines
@ 2018-12-12 12:17 Max
  2018-12-12 12:26 ` Julia Lawall
  0 siblings, 1 reply; 11+ messages in thread
From: Max @ 2018-12-12 12:17 UTC (permalink / raw)
  To: cocci

Hi.

I've problem replacing parameterized #define using spatch.

For example, in test.c I have following defines:

...

#define old_junk(a, b, c) if (c) \

                                           printf(a); \

                                          else \

                                           { printf(b); }

#define new_stuff(a, b, b) my_func(a, b, c)

I would like to replace all the entries of old_junk(x, y, z); with 
new_stuff(x, y, z);

I've tried following spatch:

@@
identifier old_junk, new_stuff, abort;
identifier a, b, c;
@@
- old_junk(a, b, c);
+ if (new_stuff(a, b, c))

+        abort();

Which used as "spatch --in-place --sp-file my.spatch tests/test.c"

Unfortunately all I got is "metavariable new_stuff not used in the - or 
context code".

I've tried using "function new_stuff" instead and got another failure: 
"MetaFunc, need more semantic info about id".

Which is the right way to make this sort of replacements?

I'd be happy to get points to the docs which clarify this part as I was 
unable to find anything relevant on 
http://coccinelle.lip6.fr/documentation.php

I went over tutorial slides as well and it feels like I'm missing 
something obvious in here.

I'm using spatch 1.0.4 in latest ubuntu.

-- 
- Max Suraev <msuraev@sysmocom.de>       http://www.sysmocom.de/
=======================================================================
* sysmocom - systems for mobile communications GmbH
* Alt-Moabit 93
* 10559 Berlin, Germany
* Sitz / Registered office: Berlin, HRB 134158 B
* Geschaeftsfuehrer / Managing Directors: Harald Welte

_______________________________________________
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] replacing defines
  2018-12-12 12:17 [Cocci] replacing defines Max
@ 2018-12-12 12:26 ` Julia Lawall
  2018-12-12 18:00   ` Max
  0 siblings, 1 reply; 11+ messages in thread
From: Julia Lawall @ 2018-12-12 12:26 UTC (permalink / raw)
  To: Max; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 2874 bytes --]



On Wed, 12 Dec 2018, Max wrote:

> Hi.
>
> I've problem replacing parameterized #define using spatch.
>
> For example, in test.c I have following defines:
>
> ...
>
> #define old_junk(a, b, c) if (c) \
>
>                                            printf(a); \
>
>                                           else \
>
>                                            { printf(b); }
>
> #define new_stuff(a, b, b) my_func(a, b, c)
>
> I would like to replace all the entries of old_junk(x, y, z); with
> new_stuff(x, y, z);
>
> I've tried following spatch:
>
> @@
> identifier old_junk, new_stuff, abort;

You have declared new_stuff and abort as metavariables, meaning that
Coccinelle has to figure out what they should be.  But it has no way of
figuring that out in this case.  I think that you can just drop the above
line completely, since you say you really want to replace a call to
old_junk by a call to new_stuff.  If you wanted to replace all calls to
three argument functions by a call to new_stuff, then you would want
old_junk to be a metavariable, but that is probably not useful.

> identifier a, b, c;

Are you sure that a b and c will only match identifiers, like x.  If they
may match expressions, like 2 + 3, then you would want expression in these
cases.

Actually, the fact that old_junk and new_stuff are defined as macros is
irrelevant for this rule.  Coccinelle will just look at the calls.  It
doesn't expland macros, so it doesn't know whether the call is to a macr
or to a function.

julia

> @@
> - old_junk(a, b, c);
> + if (new_stuff(a, b, c))
>
> +        abort();
>
> Which used as "spatch --in-place --sp-file my.spatch tests/test.c"
>
> Unfortunately all I got is "metavariable new_stuff not used in the - or
> context code".
>
> I've tried using "function new_stuff" instead and got another failure:
> "MetaFunc, need more semantic info about id".
>
> Which is the right way to make this sort of replacements?
>
> I'd be happy to get points to the docs which clarify this part as I was
> unable to find anything relevant on
> http://coccinelle.lip6.fr/documentation.php
>
> I went over tutorial slides as well and it feels like I'm missing
> something obvious in here.
>
> I'm using spatch 1.0.4 in latest ubuntu.
>
> --
> - Max Suraev <msuraev@sysmocom.de>       http://www.sysmocom.de/
> =======================================================================
> * sysmocom - systems for mobile communications GmbH
> * Alt-Moabit 93
> * 10559 Berlin, Germany
> * Sitz / Registered office: Berlin, HRB 134158 B
> * Geschaeftsfuehrer / Managing Directors: Harald Welte
>
> _______________________________________________
> Cocci mailing list
> Cocci@systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>
[-- Attachment #2: Type: text/plain, Size: 136 bytes --]

_______________________________________________
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] replacing defines
  2018-12-12 12:26 ` Julia Lawall
@ 2018-12-12 18:00   ` Max
       [not found]     ` <alpine.DEB.2.21.1812130723160.2458@hadrien>
  0 siblings, 1 reply; 11+ messages in thread
From: Max @ 2018-12-12 18:00 UTC (permalink / raw)
  To: cocci

Splendid, worked like a charm once I've left only "expression a, b, c" 
in the header. Thank you for quick and detailed response.

On a related note: would it be possible to remove old multi-line #define 
using spatch as well?

That's of course more of a nitpicking but would be nice to get rid of 
the old definition once it's not used anymore as well.

My attempts at that resulted only in "nothing to attach pragma to" error 
so it seems like #define itself is handled as a special case of some 
sort while the use of macro and function is equivalent.

12.12.18 13:26, Julia Lawall пишет:
>
> Actually, the fact that old_junk and new_stuff are defined as macros is
> irrelevant for this rule.  Coccinelle will just look at the calls.  It
> doesn't expland macros, so it doesn't know whether the call is to a macr
> or to a function.
>
> julia

-- 
- Max Suraev <msuraev@sysmocom.de>       http://www.sysmocom.de/
=======================================================================
* sysmocom - systems for mobile communications GmbH
* Alt-Moabit 93
* 10559 Berlin, Germany
* Sitz / Registered office: Berlin, HRB 134158 B
* Geschaeftsfuehrer / Managing Directors: Harald Welte

_______________________________________________
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] replacing defines
       [not found]     ` <alpine.DEB.2.21.1812130723160.2458@hadrien>
@ 2018-12-13 16:16       ` Max
  2018-12-13 19:56         ` Julia Lawall
  0 siblings, 1 reply; 11+ messages in thread
From: Max @ 2018-12-13 16:16 UTC (permalink / raw)
  To: cocci

Here it is:

________________________

@@
expression a, b, c;
@@
- old_junk(a, b, c);
+ if (!new_stuff(a, b, c))
+  abort();

@@
identifier x, y, z;
@@
- #define old_junk(x, y, z)
+
________________________

This result in Failure("nothing to attach pragma to") - I've tried 
variants with "- #define old_junk(x, y, z) ..." and using "identifier 
old_junk" and "function old_junk" with the same result.

Is there some syntax for "remove line which starts from this prefix" 
which works with multilne defines which use "\"?

It seems like the "#" symbol is causing the trouble but I'm not sure if 
I should escape it somehow or there's specific declaration for macros 
which I should use.

What would be the right approach to get rid of

________________________


#define old_junk(a, b, c) if (c) \

                                           printf(a); \

                                          else \

                                           { printf(b); }

________________________

with the help of spatch?

-- 
- Max Suraev <msuraev@sysmocom.de>       http://www.sysmocom.de/
=======================================================================
* sysmocom - systems for mobile communications GmbH
* Alt-Moabit 93
* 10559 Berlin, Germany
* Sitz / Registered office: Berlin, HRB 134158 B
* Geschaeftsfuehrer / Managing Directors: Harald Welte

_______________________________________________
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] replacing defines
  2018-12-13 16:16       ` Max
@ 2018-12-13 19:56         ` Julia Lawall
  2018-12-14 11:31           ` Max
  0 siblings, 1 reply; 11+ messages in thread
From: Julia Lawall @ 2018-12-13 19:56 UTC (permalink / raw)
  To: Max; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 1792 bytes --]



On Thu, 13 Dec 2018, Max wrote:

> Here it is:
>
> ________________________
>
> @@
> expression a, b, c;
> @@
> - old_junk(a, b, c);
> + if (!new_stuff(a, b, c))
> +  abort();
>
> @@
> identifier x, y, z;
> @@
> - #define old_junk(x, y, z)
> +
> ________________________
>
> This result in Failure("nothing to attach pragma to") - I've tried
> variants with "- #define old_junk(x, y, z) ..." and using "identifier
> old_junk" and "function old_junk" with the same result.
>
> Is there some syntax for "remove line which starts from this prefix"
> which works with multilne defines which use "\"?
>
> It seems like the "#" symbol is causing the trouble but I'm not sure if
> I should escape it somehow or there's specific declaration for macros
> which I should use.
>
> What would be the right approach to get rid of
>
> ________________________
>
>
> #define old_junk(a, b, c) if (c) \
>
>                                            printf(a); \
>
>                                           else \
>
>                                            { printf(b); }
>
> ________________________
>
> with the help of spatch?

@@
@@

- #define old_junk(a,b,c) ...

julia

>
> --
> - Max Suraev <msuraev@sysmocom.de>       http://www.sysmocom.de/
> =======================================================================
> * sysmocom - systems for mobile communications GmbH
> * Alt-Moabit 93
> * 10559 Berlin, Germany
> * Sitz / Registered office: Berlin, HRB 134158 B
> * Geschaeftsfuehrer / Managing Directors: Harald Welte
>
> _______________________________________________
> Cocci mailing list
> Cocci@systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>
[-- Attachment #2: Type: text/plain, Size: 136 bytes --]

_______________________________________________
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] replacing defines
  2018-12-13 19:56         ` Julia Lawall
@ 2018-12-14 11:31           ` Max
  2018-12-18 15:25             ` Max
  0 siblings, 1 reply; 11+ messages in thread
From: Max @ 2018-12-14 11:31 UTC (permalink / raw)
  To: cocci

Damn! So simple and elegant, thank you.

I was completely overthinking it.

13.12.18 20:56, Julia Lawall пишет:
>
> @@
> @@
>
> - #define old_junk(a,b,c) ...
>
> julia
>
-- 
- Max Suraev <msuraev@sysmocom.de>       http://www.sysmocom.de/
=======================================================================
* sysmocom - systems for mobile communications GmbH
* Alt-Moabit 93
* 10559 Berlin, Germany
* Sitz / Registered office: Berlin, HRB 134158 B
* Geschaeftsfuehrer / Managing Directors: Harald Welte

_______________________________________________
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] replacing defines
  2018-12-14 11:31           ` Max
@ 2018-12-18 15:25             ` Max
  2018-12-18 15:34               ` Julia Lawall
  0 siblings, 1 reply; 11+ messages in thread
From: Max @ 2018-12-18 15:25 UTC (permalink / raw)
  To: cocci

Is there a way to combine those 2 patches in one file?

For example, following spatch:

_____________________________

// spatch --in-place --sp-file my.spatch tests/test.c

@@
@@
- #define OLD(msg, data, len) ...

@@
expression a, b, c;
@@
- OLD(a, b, c);
+ if (!my_func(a, b, c))
+  abort();
_____________________________

Gives me following error:

rule starting on line 2: node 25: [end][] in something_else reachable by 
inconsistent control-flow paths

which I don't know how to interpret.

-- 
- Max Suraev <msuraev@sysmocom.de>       http://www.sysmocom.de/
=======================================================================
* sysmocom - systems for mobile communications GmbH
* Alt-Moabit 93
* 10559 Berlin, Germany
* Sitz / Registered office: Berlin, HRB 134158 B
* Geschaeftsfuehrer / Managing Directors: Harald Welte

_______________________________________________
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] replacing defines
  2018-12-18 15:25             ` Max
@ 2018-12-18 15:34               ` Julia Lawall
  2018-12-18 15:48                 ` Max
  0 siblings, 1 reply; 11+ messages in thread
From: Julia Lawall @ 2018-12-18 15:34 UTC (permalink / raw)
  To: Max; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 1977 bytes --]



On Tue, 18 Dec 2018, Max wrote:

> Is there a way to combine those 2 patches in one file?
>
> For example, following spatch:
>
> _____________________________
>
> // spatch --in-place --sp-file my.spatch tests/test.c
>
> @@
> @@
> - #define OLD(msg, data, len) ...
>
> @@
> expression a, b, c;
> @@
> - OLD(a, b, c);
> + if (!my_func(a, b, c))
> +  abort();
> _____________________________
>
> Gives me following error:
>
> rule starting on line 2: node 25: [end][] in something_else reachable by
> inconsistent control-flow paths
>
> which I don't know how to interpret.

It's complaining about the rule starting on line 2, which is the one
removing the #define.  So the problem doesn't have anything to do with the
two rules being together - it never starts executing the second one.

Something being reachable by inconsistent control-flow paths occurs when
if you trace through the function in one way (ie via one if branch) you
get one transformation, and if you trace through it in another way you get
another transformation (or no transformation).

It's a bit strange that this would occur in your case, where you just want
to remove the entire body of the #define.  Can you mak a simple example
that illustrates the problem?

To figure out what #define is being considered, you can try the
command-line argument --show-trying.  If that doesn't suffice (I don't
know to what extent it is supported for #define), then --verbose-match
might be helpful as well.

julia

>
> --
> - Max Suraev <msuraev@sysmocom.de>       http://www.sysmocom.de/
> =======================================================================
> * sysmocom - systems for mobile communications GmbH
> * Alt-Moabit 93
> * 10559 Berlin, Germany
> * Sitz / Registered office: Berlin, HRB 134158 B
> * Geschaeftsfuehrer / Managing Directors: Harald Welte
>
> _______________________________________________
> Cocci mailing list
> Cocci@systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>
[-- Attachment #2: Type: text/plain, Size: 136 bytes --]

_______________________________________________
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] replacing defines
  2018-12-18 15:34               ` Julia Lawall
@ 2018-12-18 15:48                 ` Max
  2018-12-18 16:10                   ` Julia Lawall
  0 siblings, 1 reply; 11+ messages in thread
From: Max @ 2018-12-18 15:48 UTC (permalink / raw)
  To: cocci

[-- Attachment #1: Type: text/plain, Size: 1977 bytes --]

Attached are spatch and minimal example to reproduce the error.

I believe that the reason is the last '}' of multiline #define. This 
seems like a valid syntax to gcc but spatch gives aforementioned error.

Just in case ML won't accept attachements, here's how #define looks like:

________________

#define OLD(msg, data, len)                         \
     if (msgb_func(msg) != len) {                    \
         printf("%s:%d ERROR! with %d\n",     \
             __func__, __LINE__, (int) len);     \
         abort();                        \
     }
________________


18.12.18 16:34, Julia Lawall пишет:
>
> It's complaining about the rule starting on line 2, which is the one
> removing the #define.  So the problem doesn't have anything to do with the
> two rules being together - it never starts executing the second one.
>
> Something being reachable by inconsistent control-flow paths occurs when
> if you trace through the function in one way (ie via one if branch) you
> get one transformation, and if you trace through it in another way you get
> another transformation (or no transformation).
>
> It's a bit strange that this would occur in your case, where you just want
> to remove the entire body of the #define.  Can you mak a simple example
> that illustrates the problem?
>
> To figure out what #define is being considered, you can try the
> command-line argument --show-trying.  If that doesn't suffice (I don't
> know to what extent it is supported for #define), then --verbose-match
> might be helpful as well.
>
> julia

-- 
- Max Suraev <msuraev@sysmocom.de>       http://www.sysmocom.de/
=======================================================================
* sysmocom - systems for mobile communications GmbH
* Alt-Moabit 93
* 10559 Berlin, Germany
* Sitz / Registered office: Berlin, HRB 134158 B
* Geschaeftsfuehrer / Managing Directors: Harald Welte


[-- Attachment #2: test.c --]
[-- Type: text/x-csrc, Size: 441 bytes --]

[-- Attachment #3: my.spatch --]
[-- Type: text/plain, Size: 168 bytes --]

// spatch --in-place --sp-file my.spatch test.c
@@
@@
- #define OLD(msg, data, len) ...

@@
expression a, b, c;
@@
- OLD(a, b, c);
+ if (!my_func(a, b, c))
+  abort();

[-- Attachment #4: Type: text/plain, Size: 136 bytes --]

_______________________________________________
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] replacing defines
  2018-12-18 15:48                 ` Max
@ 2018-12-18 16:10                   ` Julia Lawall
  2018-12-18 16:26                     ` Max
  0 siblings, 1 reply; 11+ messages in thread
From: Julia Lawall @ 2018-12-18 16:10 UTC (permalink / raw)
  To: Max; +Cc: cocci

[-- Attachment #1: Type: text/plain, Size: 2274 bytes --]



On Tue, 18 Dec 2018, Max wrote:

> Attached are spatch and minimal example to reproduce the error.
>
> I believe that the reason is the last '}' of multiline #define. This seems
> like a valid syntax to gcc but spatch gives aforementioned error.

There seems to be something unanticipated about the control-flow graph of
a macro definition.  You can use the argument --allow-inconsistent-paths
to bypass this check.

julia

>
> Just in case ML won't accept attachements, here's how #define looks like:
>
> ________________
>
> #define OLD(msg, data, len)                         \
>     if (msgb_func(msg) != len) {                    \
>         printf("%s:%d ERROR! with %d\n",     \
>             __func__, __LINE__, (int) len);     \
>         abort();                        \
>     }
> ________________
>
>
> 18.12.18 16:34, Julia Lawall пишет:
> >
> > It's complaining about the rule starting on line 2, which is the one
> > removing the #define.  So the problem doesn't have anything to do with the
> > two rules being together - it never starts executing the second one.
> >
> > Something being reachable by inconsistent control-flow paths occurs when
> > if you trace through the function in one way (ie via one if branch) you
> > get one transformation, and if you trace through it in another way you get
> > another transformation (or no transformation).
> >
> > It's a bit strange that this would occur in your case, where you just want
> > to remove the entire body of the #define.  Can you mak a simple example
> > that illustrates the problem?
> >
> > To figure out what #define is being considered, you can try the
> > command-line argument --show-trying.  If that doesn't suffice (I don't
> > know to what extent it is supported for #define), then --verbose-match
> > might be helpful as well.
> >
> > julia
>
> --
> - Max Suraev <msuraev@sysmocom.de>       http://www.sysmocom.de/
> =======================================================================
> * sysmocom - systems for mobile communications GmbH
> * Alt-Moabit 93
> * 10559 Berlin, Germany
> * Sitz / Registered office: Berlin, HRB 134158 B
> * Geschaeftsfuehrer / Managing Directors: Harald Welte
>
>
[-- Attachment #2: Type: text/plain, Size: 136 bytes --]

_______________________________________________
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] replacing defines
  2018-12-18 16:10                   ` Julia Lawall
@ 2018-12-18 16:26                     ` Max
  0 siblings, 0 replies; 11+ messages in thread
From: Max @ 2018-12-18 16:26 UTC (permalink / raw)
  To: cocci

Thank you, worked like a charm.

18.12.18 17:10, Julia Lawall пишет:
>
> There seems to be something unanticipated about the control-flow graph of
> a macro definition.  You can use the argument --allow-inconsistent-paths
> to bypass this check.
>
> julia
>
-- 
- Max Suraev <msuraev@sysmocom.de>       http://www.sysmocom.de/
=======================================================================
* sysmocom - systems for mobile communications GmbH
* Alt-Moabit 93
* 10559 Berlin, Germany
* Sitz / Registered office: Berlin, HRB 134158 B
* Geschaeftsfuehrer / Managing Directors: Harald Welte

_______________________________________________
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, back to index

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-12 12:17 [Cocci] replacing defines Max
2018-12-12 12:26 ` Julia Lawall
2018-12-12 18:00   ` Max
     [not found]     ` <alpine.DEB.2.21.1812130723160.2458@hadrien>
2018-12-13 16:16       ` Max
2018-12-13 19:56         ` Julia Lawall
2018-12-14 11:31           ` Max
2018-12-18 15:25             ` Max
2018-12-18 15:34               ` Julia Lawall
2018-12-18 15:48                 ` Max
2018-12-18 16:10                   ` Julia Lawall
2018-12-18 16:26                     ` Max

Coccinelle archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/cocci/0 cocci/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 cocci cocci/ https://lore.kernel.org/cocci \
		cocci@systeme.lip6.fr cocci@archiver.kernel.org
	public-inbox-index cocci


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/fr.lip6.systeme.cocci


AGPL code for this site: git clone https://public-inbox.org/ public-inbox