replacing defines
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.
Signature · 12 lines
- Max Suraev <[email protected]> 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 [email protected] https://systeme.lip6.fr/mailman/listinfo/cocci
Quoted 18 lines · Max
Hi.
I've problem replacing parameterized #define using spatch.
For example, in
test.cI 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)
Quoted 7 lines
I would like to replace all the entries of
old_junk(x, y, z); withnew_stuff(x, y, z);I've tried following spatch:
@@
identifierold_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.
Quoted 1 line
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
Quoted 34 lines
@@
-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_stuffnot 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.phpI 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 <[email protected]> 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
Quoted 5 lines
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/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 пишет:
Quoted 7 lines
Actually, the fact that
old_junkandnew_stuffare 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
Signature · 12 lines
- Max Suraev <[email protected]> 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 [email protected] https://systeme.lip6.fr/mailman/listinfo/cocci
A message that is not in the archive (sent off-list or not yet received): alpine.DEB.2.21.1812130723160.2458@hadrien
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?
Signature · 12 lines
- Max Suraev <[email protected]> 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 [email protected] https://systeme.lip6.fr/mailman/listinfo/cocci
Quoted 38 lines · Max
Here it is:
________________________
@@
expression a, b, c;
@@
-old_junk(a, b, c);
+ if (!new_stuff(a, b, c))
+abort();@@
identifier x, y, z;
@@
- #defineold_junk(x, y, z)
+
________________________This result in
Failure("nothing to attach pragma to")- I've tried variants with "- #defineold_junk(x, y, z)..." and using "identifierold_junk" and "functionold_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); \
Quoted 7 lines
else \
{ printf(b); }
________________________
with the help of spatch?
@@
@@
- #define old_junk(a,b,c) ...
julia
Quoted 10 lines
--
- Max Suraev <[email protected]> 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
Quoted 5 lines
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
Damn! So simple and elegant, thank you.
I was completely overthinking it.
13.12.18 20:56, Julia Lawall пишет:
Quoted 8 lines
@@
@@- #define
old_junk(a,b,c)...julia
Signature · 12 lines
- Max Suraev <[email protected]> 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 [email protected] https://systeme.lip6.fr/mailman/listinfo/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.
Signature · 12 lines
- Max Suraev <[email protected]> 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 [email protected] https://systeme.lip6.fr/mailman/listinfo/cocci
Quoted 20 lines · Max
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@@
@@
- #defineOLD(msg, data, len)...@@
expression a, b, c;
@@
-OLD(a, b, c);
+ if (!my_func(a, b, c))
+abort();
_____________________________
Quoted 6 lines
Gives me following error:
rule starting on line 2: node 25: [end][] in
something_elsereachable by inconsistent control-flow pathswhich 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
Quoted 10 lines
--
- Max Suraev <[email protected]> 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
Quoted 5 lines
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
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 пишет:
Quoted 20 lines
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
Signature · 7 lines
- Max Suraev <[email protected]> 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
/*
* (C)omment
* All Rights Reserved
*/
#include <errno.h>
#define OLD(msg, data, len) \
if (msgb_func(msg) != len) { \
printf("%s:%d ERROR! with %d\n", \
__func__, __LINE__, (int) len); \
abort(); \
}static void test_error()
{
static const uint8_t res[] = { 0x00, 0x01, 0x21 };
struct msgb *msg;
printf("Testing for error\n");
msg = test_complete();
OLD(msg, res, ARRAY_SIZE(res));
msgb_free(msg);
}// 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();
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
Quoted 4 lines · Max
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
Quoted 36 lines
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 пишет:
Quoted 14 lines
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?
Quoted 6 lines
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
Quoted 10 lines
--
- Max Suraev <[email protected]> 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
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci
Thank you, worked like a charm.
18.12.18 17:10, Julia Lawall пишет:
Quoted 7 lines
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
Signature · 12 lines
- Max Suraev <[email protected]> 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 [email protected] https://systeme.lip6.fr/mailman/listinfo/cocci