Can fields from structs be used added code as expressions?
Hi all,
I have a bunch of structs with lots of members. I need a way to compare to instances of those structs to see if they have changed. The easiest way to do that is compare if (a.j != b.j) { match() } but it would be lots of tedious hand-typing all those if's, or doing a bunch of ugly greps and cuts on the .c files to hand-parse what I want.
I would like to use Coccinelle to add all those struct member comparisons for me. It would be really great if this is possible because there are a dozen structs each with lots of members:
Lets say I have t.c as follows:
typedef struct
{
int x, y, z;
int u, v;
int w;
} A;
A a, b;
#define NOMATCH(s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member);
f()
{
}The NOMATCH macro will stringify the struct members so I can print them for debugging. For example, inside of f() I would like to add this:
if (a.F != b.F) NOMATCH(a, b, F);
I started writing this SmPL from what I've learned about Coccinelle so far and I don't quite have it working. This almost works:
@ r1 @
typedef A;
type T, FT;
identifier F;
@@
typedef struct
{
...
FT F;
} T;
@ r2 @
type r1.T;
identifier r1.F;
@@
f()
{
...
+ if (a.F != b.F) NOMATCH(a, b, F);
}which produces:
--- t.c
+++ /tmp/cocci-output-7874-a515f6-t.c
@@ -10,4 +10,6 @@ A a, b;
#define NOMATCH(s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member);
f()
{
+ if (a.w != b.w)
+ NOMATCH(a, b, w);
}Can Coccinelle do this for all struct members?
I tried playing with <+ ... F ... +> and ... when exists when any .... I've also tried field and field list instead of type with identifier withing the typedef struct { ... } braces.
Note that the number of variables declared on a single line varies:
int x, y, z; int u, v; int w;
so I know that the FT F; part of the SmPL is insufficent, but I'm not sure how to address that to match all struct declarations.
Ideas?
-Eric
Quoted 27 lines · Eric Wheeler
I have a bunch of structs with lots of members. I need a way to compare to instances of those structs to see if they have changed. The easiest way to do that is compare
if (a.j != b.j) { match() }but it would be lots of tedious hand-typing all those if's, or doing a bunch of ugly greps and cuts on the .c files to hand-parse what I want.I would like to use Coccinelle to add all those struct member comparisons for me. It would be really great if this is possible because there are a dozen structs each with lots of members:
Lets say I have
t.cas follows:typedef struct { int x, y, z; int u, v; int w; } A; A a, b; #define NOMATCH(s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member); f() { }
Quoted 8 lines
The NOMATCH macro will stringify the struct members so I can print them for debugging. For example, inside of
f()I would like to add this:if (a.F != b.F) NOMATCH(a, b, F);I started writing this SmPL from what I've learned about Coccinelle so far and I don't quite have it working. This almost works:
Oh! Adding another ... and changing + to ++ did it:
@ r1 @
typedef A;
type T, FT;
identifier F, j;
@@
typedef struct
{
...
FT F@j;
...
} T;
@ r2 @
type r1.T;
identifier r1.F;
identifier r1.j;
@@
f()
{
...
++ if (a.j != b.j) NOMATCH(a, b, j);
}which produces:
--- t.c
+++ /tmp/cocci-output-19090-251154-t.c
@@ -10,4 +10,16 @@ A a, b;
#define NOMATCH(s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member);
f()
{
+ if (a.z != b.z)
+ NOMATCH(a, b, z);
+ if (a.y != b.y)
+ NOMATCH(a, b, y);
+ if (a.x != b.x)
+ NOMATCH(a, b, x);
+ if (a.w != b.w)
+ NOMATCH(a, b, w);
+ if (a.v != b.v)
+ NOMATCH(a, b, v);
+ if (a.u != b.u)
+ NOMATCH(a, b, u);
}So, hurray for the SmPL ++ operator! Also it doesn't look like I needed @j.
Is this how you would approach the problem?
-Eric
Quoted 33 lines
which produces:
--- t.c +++ /tmp/cocci-output-7874-a515f6-t.c @@ -10,4 +10,6 @@ A a, b; #define NOMATCH(s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member); f() { + if (a.w != b.w) + NOMATCH(a, b, w); }Can Coccinelle do this for all struct members?
I tried playing with
<+ ... F ... +>and... when exists when any .... I've also triedfieldandfield listinstead oftypewithidentifierwithing thetypedef struct { ... }braces.Note that the number of variables declared on a single line varies:
int x, y, z; int u, v; int w;so I know that the
FT F;part of the SmPL is insufficent, but I'm not sure how to address that to match all struct declarations.Ideas?
-Eric
Quoted 6 lines · Eric Wheeler
Quoted 5 lines · Eric Wheeler
I have a bunch of structs with lots of members. I need a way to compare to instances of those structs to see if they have changed. The easiest way to do that is compare
if (a.j != b.j) { match() }but it would be lots of tedious hand-typing all those if's, or doing a bunch of ugly greps and cuts on the .c files to hand-parse what I want.
I am continuously amazed by what Coccinelle is capable of.
We are working to make the xnec2c EM simulator thread safe (www.xnec2c.org) but we need to figure out what is being modified between frequency loops to isolate the data. Check this out:
spatch --sp-file cocci/nec-data-diff.cocci t.c src/common.h src/shared.c
src/common.h and src/shared.c are in the xnec2c codebase if you're curious:
https://github.com/KJ7LNW/xnec2c/tree/master/src
This is t.c where I'm staging the comparisons:
typedef struct
{
int x, y, z;
int u, v;
int w;
} A;
A a, b;
#define NOMATCH(s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member);
f()
{
}Here is the SmPL:
// Match these struct types:
@ r1 @
typedef cm_t, cmag_t, crnt_t, ct1m_t, ct2m_t, data_t, dataj_t, fpat_t,
ggrid_t, gnd_t, gwav_t, impedance_data_t, incom_t, matpar_t, near_field_t,
netcx_t, save_t, segj_t, smat_t, zload_t, vsorc_t;
type T = {cm_t, cmag_t, crnt_t, ct1m_t, ct2m_t, data_t, dataj_t, fpat_t,
ggrid_t, gnd_t, gwav_t, impedance_data_t, incom_t, matpar_t, near_field_t,
netcx_t, save_t, segj_t, smat_t, zload_t, vsorc_t};
type FT;
identifier F;
@@
typedef struct
{
...
FT F;
...
} T;
// Match the variable definition:
@ struct_name @
type r1.T;
identifier I;
@@
T I;
// Add the compare_T() function call and compare_T function definition for each type:
@ r2 @
identifier struct_name.I;
fresh identifier FN = "compare_"##I;
@@
f()
{
++ FN();
}
++void FN(){}
// Add the comparisons for each type member:
@ r3 @
identifier r2.FN;
type r1.T;
identifier r1.F;
@@
void FN()
{
...
++ if (a.F != b.F) NOMATCH(a, b, T, F);
} And here is the output that probably saved hours of time.
Kudos to the Coccinelle team!
[root@el7-dev xnec2c-git]# spatch --sp-file cocci/nec-data-diff.cocci t.c src/common.h src/shared.cinit_defs_builtins: /usr/local/coccinelle-git/bin/lib/coccinelle/standard.h
HANDLING: t.c src/common.h src/shared.c
tmp/cocci-output-26246-5722fe-t.c renamed from t.c +573 -0
@@ -10,6 +10,579 @@ A a, b;#define NOMATCH (s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member); f() { +compare_impedance_data (); +compare_gwav (); +compare_smat (); +compare_zload (); +compare_incom (); +compare_netcx (); +compare_vsorc (); +compare_matpar (); +compare_fpat (); +compare_gnd (); +compare_save (); +compare_data (); +compare_dataj (); +compare_segj (); +compare_ggrid (); +compare_near_field (); +compare_crnt (); +} + +void compare_impedance_data () +{ +if (a.zreal != b.zreal) +NOMATCH (a, b, impedance_data_t, zreal); +if (a.zphase != b.zphase) +NOMATCH (a, b, impedance_data_t, zphase); +if (a.zmagn != b.zmagn) +NOMATCH (a, b, impedance_data_t, zmagn); +if (a.zimag != b.zimag) +NOMATCH (a, b, impedance_data_t, zimag); +} + +void compare_gwav () +{ +if (a.zph != b.zph) +NOMATCH (a, b, gwav_t, zph); +if (a.zmh != b.zmh) +NOMATCH (a, b, gwav_t, zmh); +if (a.xx2 != b.xx2) +NOMATCH (a, b, gwav_t, xx2); +if (a.xx1 != b.xx1) +NOMATCH (a, b, gwav_t, xx1); +if (a.u2 != b.u2) +NOMATCH (a, b, gwav_t, u2); +if (a.u != b.u) +NOMATCH (a, b, gwav_t, u); +if (a.r2 != b.r2) +NOMATCH (a, b, gwav_t, r2); +if (a.r1 != b.r1) +NOMATCH (a, b, gwav_t, r1); +} + +void compare_smat () +{ +if (a.ssx != b.ssx) +NOMATCH (a, b, smat_t, ssx); +if (a.nop != b.nop) +NOMATCH (a, b, smat_t, nop); +} + +void compare_zload () +{ +if (a.zarray != b.zarray) +NOMATCH (a, b, zload_t, zarray); +if (a.nload != b.nload) +NOMATCH (a, b, zload_t, nload); +if (a.nldseg != b.nldseg) +NOMATCH (a, b, zload_t, nldseg); +if (a.ldtype != b.ldtype) +NOMATCH (a, b, zload_t, ldtype); +if (a.ldsegn != b.ldsegn) +NOMATCH (a, b, zload_t, ldsegn); +} + +void compare_incom () +{ +if (a.zo != b.zo) +NOMATCH (a, b, incom_t, zo); +if (a.ysn != b.ysn) +NOMATCH (a, b, incom_t, ysn); +if (a.yo != b.yo) +NOMATCH (a, b, incom_t, yo); +if (a.xsn != b.xsn) +NOMATCH (a, b, incom_t, xsn); +if (a.xo != b.xo) +NOMATCH (a, b, incom_t, xo); +if (a.sn != b.sn) +NOMATCH (a, b, incom_t, sn); +if (a.isnor != b.isnor) +NOMATCH (a, b, incom_t, isnor); +} + +void compare_netcx () +{ +if (a.zped != b.zped) +NOMATCH (a, b, netcx_t, zped); +if (a.x22r != b.x22r) +NOMATCH (a, b, netcx_t, x22r); +if (a.x22i != b.x22i) +NOMATCH (a, b, netcx_t, x22i); +if (a.x12r != b.x12r) +NOMATCH (a, b, netcx_t, x12r); +if (a.x12i != b.x12i) +NOMATCH (a, b, netcx_t, x12i); +if (a.x11r != b.x11r) +NOMATCH (a, b, netcx_t, x11r); +if (a.x11i != b.x11i) +NOMATCH (a, b, netcx_t, x11i); +if (a.pnls != b.pnls) +NOMATCH (a, b, netcx_t, pnls); +if (a.pin != b.pin) +NOMATCH (a, b, netcx_t, pin); +if (a.ntyp != b.ntyp) +NOMATCH (a, b, netcx_t, ntyp); +if (a.ntsol != b.ntsol) +NOMATCH (a, b, netcx_t, ntsol); +if (a.nprint != b.nprint) +NOMATCH (a, b, netcx_t, nprint); +if (a.npeq != b.npeq) +NOMATCH (a, b, netcx_t, npeq); +if (a.nonet != b.nonet) +NOMATCH (a, b, netcx_t, nonet); +if (a.neq2 != b.neq2) +NOMATCH (a, b, netcx_t, neq2); +if (a.neq != b.neq) +NOMATCH (a, b, netcx_t, neq); +if (a.masym != b.masym) +NOMATCH (a, b, netcx_t, masym); +if (a.iseg2 != b.iseg2) +NOMATCH (a, b, netcx_t, iseg2); +if (a.iseg1 != b.iseg1) +NOMATCH (a, b, netcx_t, iseg1); +} + +void compare_vsorc () +{ +if (a.vsant != b.vsant) +NOMATCH (a, b, vsorc_t, vsant); +if (a.vqds != b.vqds) +NOMATCH (a, b, vsorc_t, vqds); +if (a.vqd != b.vqd) +NOMATCH (a, b, vsorc_t, vqd); +if (a.nvqd != b.nvqd) +NOMATCH (a, b, vsorc_t, nvqd); +if (a.nsant != b.nsant) +NOMATCH (a, b, vsorc_t, nsant); +if (a.nqds != b.nqds) +NOMATCH (a, b, vsorc_t, nqds); +if (a.ivqd != b.ivqd) +NOMATCH (a, b, vsorc_t, ivqd); +if (a.isant != b.isant) +NOMATCH (a, b, vsorc_t, isant); +if (a.iqds != b.iqds) +NOMATCH (a, b, vsorc_t, iqds); +} + +void compare_matpar () +{ +if (a.npblk != b.npblk) +NOMATCH (a, b, matpar_t, npblk); +if (a.nlast != b.nlast) +NOMATCH (a, b, matpar_t, nlast); +if (a.imat != b.imat) +NOMATCH (a, b, matpar_t, imat); +if (a.icase != b.icase) +NOMATCH (a, b, matpar_t, icase); +} + +void compare_fpat () +{ +if (a.znr != b.znr) +NOMATCH (a, b, fpat_t, znr); +if (a.ynr != b.ynr) +NOMATCH (a, b, fpat_t, ynr); +if (a.xnr != b.xnr) +NOMATCH (a, b, fpat_t, xnr); +if (a.thets != b.thets) +NOMATCH (a, b, fpat_t, thets); +if (a.sig2 != b.sig2) +NOMATCH (a, b, fpat_t, sig2); +if (a.rfld != b.rfld) +NOMATCH (a, b, fpat_t, rfld); +if (a.pnlr != b.pnlr) +NOMATCH (a, b, fpat_t, pnlr); +if (a.ploss != b.ploss) +NOMATCH (a, b, fpat_t, ploss); +if (a.pinr != b.pinr) +NOMATCH (a, b, fpat_t, pinr); +if (a.phis != b.phis) +NOMATCH (a, b, fpat_t, phis); +if (a.nth != b.nth) +NOMATCH (a, b, fpat_t, nth); +if (a.nrz != b.nrz) +NOMATCH (a, b, fpat_t, nrz); +if (a.nry != b.nry) +NOMATCH (a, b, fpat_t, nry); +if (a.nrx != b.nrx) +NOMATCH (a, b, fpat_t, nrx); +if (a.nph != b.nph) +NOMATCH (a, b, fpat_t, nph); +if (a.nfeh != b.nfeh) +NOMATCH (a, b, fpat_t, nfeh); +if (a.near != b.near) +NOMATCH (a, b, fpat_t, near); +if (a.ixtyp != b.ixtyp) +NOMATCH (a, b, fpat_t, ixtyp); +if (a.ipd != b.ipd) +NOMATCH (a, b, fpat_t, ipd); +if (a.inor != b.inor) +NOMATCH (a, b, fpat_t, inor); +if (a.iax != b.iax) +NOMATCH (a, b, fpat_t, iax); +if (a.iavp != b.iavp) +NOMATCH (a, b, fpat_t, iavp); +if (a.gnor != b.gnor) +NOMATCH (a, b, fpat_t, gnor); +if (a.epsr2 != b.epsr2) +NOMATCH (a, b, fpat_t, epsr2); +if (a.dznr != b.dznr) +NOMATCH (a, b, fpat_t, dznr); +if (a.dynr != b.dynr) +NOMATCH (a, b, fpat_t, dynr); +if (a.dxnr != b.dxnr) +NOMATCH (a, b, fpat_t, dxnr); +if (a.dth != b.dth) +NOMATCH (a, b, fpat_t, dth); +if (a.dph != b.dph) +NOMATCH (a, b, fpat_t, dph); +if (a.clt != b.clt) +NOMATCH (a, b, fpat_t, clt); +if (a.cht != b.cht) +NOMATCH (a, b, fpat_t, cht); +} + +void compare_gnd () +{ +if (a.zrati2 != b.zrati2) +NOMATCH (a, b, gnd_t, zrati2); +if (a.zrati != b.zrati) +NOMATCH (a, b, gnd_t, zrati); +if (a.t2 != b.t2) +NOMATCH (a, b, gnd_t, t2); +if (a.t1 != b.t1) +NOMATCH (a, b, gnd_t, t1); +if (a.scrwr != b.scrwr) +NOMATCH (a, b, gnd_t, scrwr); +if (a.scrwl != b.scrwl) +NOMATCH (a, b, gnd_t, scrwl); +if (a.nradl != b.nradl) +NOMATCH (a, b, gnd_t, nradl); +if (a.ksymp != b.ksymp) +NOMATCH (a, b, gnd_t, ksymp); +if (a.iperf != b.iperf) +NOMATCH (a, b, gnd_t, iperf); +if (a.ifar != b.ifar) +NOMATCH (a, b, gnd_t, ifar); +if (a.gpflag != b.gpflag) +NOMATCH (a, b, gnd_t, gpflag); +if (a.frati != b.frati) +NOMATCH (a, b, gnd_t, frati); +if (a.cl != b.cl) +NOMATCH (a, b, gnd_t, cl); +if (a.ch != b.ch) +NOMATCH (a, b, gnd_t, ch); +} + +void compare_save () +{ +if (a.ztemp != b.ztemp) +NOMATCH (a, b, save_t, ztemp); +if (a.ytemp != b.ytemp) +NOMATCH (a, b, save_t, ytemp); +if (a.xtemp != b.xtemp) +NOMATCH (a, b, save_t, xtemp); +if (a.sitemp != b.sitemp) +NOMATCH (a, b, save_t, sitemp); +if (a.sig != b.sig) +NOMATCH (a, b, save_t, sig); +if (a.scrwrt != b.scrwrt) +NOMATCH (a, b, save_t, scrwrt); +if (a.scrwlt != b.scrwlt) +NOMATCH (a, b, save_t, scrwlt); +if (a.last_freq != b.last_freq) +NOMATCH (a, b, save_t, last_freq); +if (a.ip != b.ip) +NOMATCH (a, b, save_t, ip); +if (a.fstep != b.fstep) +NOMATCH (a, b, save_t, fstep); +if (a.freq != b.freq) +NOMATCH (a, b, save_t, freq); +if (a.fmhz != b.fmhz) +NOMATCH (a, b, save_t, fmhz); +if (a.epsr != b.epsr) +NOMATCH (a, b, save_t, epsr); +if (a.bitemp != b.bitemp) +NOMATCH (a, b, save_t, bitemp); +} + +void compare_data () +{ +if (a.z2 != b.z2) +NOMATCH (a, b, data_t, z2); +if (a.z1 != b.z1) +NOMATCH (a, b, data_t, z1); +if (a.z != b.z) +NOMATCH (a, b, data_t, z); +if (a.y2 != b.y2) +NOMATCH (a, b, data_t, y2); +if (a.y1 != b.y1) +NOMATCH (a, b, data_t, y1); +if (a.y != b.y) +NOMATCH (a, b, data_t, y); +if (a.x2 != b.x2) +NOMATCH (a, b, data_t, x2); +if (a.x1 != b.x1) +NOMATCH (a, b, data_t, x1); +if (a.x != b.x) +NOMATCH (a, b, data_t, x); +if (a.wlam != b.wlam) +NOMATCH (a, b, data_t, wlam); +if (a.t2z != b.t2z) +NOMATCH (a, b, data_t, t2z); +if (a.t2y != b.t2y) +NOMATCH (a, b, data_t, t2y); +if (a.t2x != b.t2x) +NOMATCH (a, b, data_t, t2x); +if (a.t1z != b.t1z) +NOMATCH (a, b, data_t, t1z); +if (a.t1y != b.t1y) +NOMATCH (a, b, data_t, t1y); +if (a.t1x != b.t1x) +NOMATCH (a, b, data_t, t1x); +if (a.si != b.si) +NOMATCH (a, b, data_t, si); +if (a.salp != b.salp) +NOMATCH (a, b, data_t, salp); +if (a.sab != b.sab) +NOMATCH (a, b, data_t, sab); +if (a.pz2 != b.pz2) +NOMATCH (a, b, data_t, pz2); +if (a.pz1 != b.pz1) +NOMATCH (a, b, data_t, pz1); +if (a.pz != b.pz) +NOMATCH (a, b, data_t, pz); +if (a.py2 != b.py2) +NOMATCH (a, b, data_t, py2); +if (a.py1 != b.py1) +NOMATCH (a, b, data_t, py1); +if (a.py != b.py) +NOMATCH (a, b, data_t, py); +if (a.px2 != b.px2) +NOMATCH (a, b, data_t, px2); +if (a.px1 != b.px1) +NOMATCH (a, b, data_t, px1); +if (a.px != b.px) +NOMATCH (a, b, data_t, px); +if (a.psalp != b.psalp) +NOMATCH (a, b, data_t, psalp); +if (a.pbi != b.pbi) +NOMATCH (a, b, data_t, pbi); +if (a.npm != b.npm) +NOMATCH (a, b, data_t, npm); +if (a.np3m != b.np3m) +NOMATCH (a, b, data_t, np3m); +if (a.np2m != b.np2m) +NOMATCH (a, b, data_t, np2m); +if (a.np != b.np) +NOMATCH (a, b, data_t, np); +if (a.n != b.n) +NOMATCH (a, b, data_t, n); +if (a.mp != b.mp) +NOMATCH (a, b, data_t, mp); +if (a.m != b.m) +NOMATCH (a, b, data_t, m); +if (a.itag != b.itag) +NOMATCH (a, b, data_t, itag); +if (a.ipsym != b.ipsym) +NOMATCH (a, b, data_t, ipsym); +if (a.icon2 != b.icon2) +NOMATCH (a, b, data_t, icon2); +if (a.icon1 != b.icon1) +NOMATCH (a, b, data_t, icon1); +if (a.cab != b.cab) +NOMATCH (a, b, data_t, cab); +if (a.bi != b.bi) +NOMATCH (a, b, data_t, bi); +} + +void compare_dataj () +{ +if (a.zj != b.zj) +NOMATCH (a, b, dataj_t, zj); +if (a.yj != b.yj) +NOMATCH (a, b, dataj_t, yj); +if (a.xj != b.xj) +NOMATCH (a, b, dataj_t, xj); +if (a.t2zj != b.t2zj) +NOMATCH (a, b, dataj_t, t2zj); +if (a.t2yj != b.t2yj) +NOMATCH (a, b, dataj_t, t2yj); +if (a.t2xj != b.t2xj) +NOMATCH (a, b, dataj_t, t2xj); +if (a.t1zj != b.t1zj) +NOMATCH (a, b, dataj_t, t1zj); +if (a.t1yj != b.t1yj) +NOMATCH (a, b, dataj_t, t1yj); +if (a.t1xj != b.t1xj) +NOMATCH (a, b, dataj_t, t1xj); +if (a.salpj != b.salpj) +NOMATCH (a, b, dataj_t, salpj); +if (a.sabj != b.sabj) +NOMATCH (a, b, dataj_t, sabj); +if (a.s != b.s) +NOMATCH (a, b, dataj_t, s); +if (a.rkh != b.rkh) +NOMATCH (a, b, dataj_t, rkh); +if (a.ipgnd != b.ipgnd) +NOMATCH (a, b, dataj_t, ipgnd); +if (a.indd2 != b.indd2) +NOMATCH (a, b, dataj_t, indd2); +if (a.indd1 != b.indd1) +NOMATCH (a, b, dataj_t, indd1); +if (a.ind2 != b.ind2) +NOMATCH (a, b, dataj_t, ind2); +if (a.ind1 != b.ind1) +NOMATCH (a, b, dataj_t, ind1); +if (a.iexk != b.iexk) +NOMATCH (a, b, dataj_t, iexk); +if (a.ezs != b.ezs) +NOMATCH (a, b, dataj_t, ezs); +if (a.ezk != b.ezk) +NOMATCH (a, b, dataj_t, ezk); +if (a.ezc != b.ezc) +NOMATCH (a, b, dataj_t, ezc); +if (a.eys != b.eys) +NOMATCH (a, b, dataj_t, eys); +if (a.eyk != b.eyk) +NOMATCH (a, b, dataj_t, eyk); +if (a.eyc != b.eyc) +NOMATCH (a, b, dataj_t, eyc); +if (a.exs != b.exs) +NOMATCH (a, b, dataj_t, exs); +if (a.exk != b.exk) +NOMATCH (a, b, dataj_t, exk); +if (a.exc != b.exc) +NOMATCH (a, b, dataj_t, exc); +if (a.cabj != b.cabj) +NOMATCH (a, b, dataj_t, cabj); +if (a.b != b.b) +NOMATCH (a, b, dataj_t, b); +} + +void compare_segj () +{ +if (a.maxcon != b.maxcon) +NOMATCH (a, b, segj_t, maxcon); +if (a.jsno != b.jsno) +NOMATCH (a, b, segj_t, jsno); +if (a.jco != b.jco) +NOMATCH (a, b, segj_t, jco); +if (a.cx != b.cx) +NOMATCH (a, b, segj_t, cx); +if (a.bx != b.bx) +NOMATCH (a, b, segj_t, bx); +if (a.ax != b.ax) +NOMATCH (a, b, segj_t, ax); +} + +void compare_ggrid () +{ +if (a.ysa != b.ysa) +NOMATCH (a, b, ggrid_t, ysa); +if (a.xsa != b.xsa) +NOMATCH (a, b, ggrid_t, xsa); +if (a.nya != b.nya) +NOMATCH (a, b, ggrid_t, nya); +if (a.nxa != b.nxa) +NOMATCH (a, b, ggrid_t, nxa); +if (a.epscf != b.epscf) +NOMATCH (a, b, ggrid_t, epscf); +if (a.dya != b.dya) +NOMATCH (a, b, ggrid_t, dya); +if (a.dxa != b.dxa) +NOMATCH (a, b, ggrid_t, dxa); +if (a.ar3 != b.ar3) +NOMATCH (a, b, ggrid_t, ar3); +if (a.ar2 != b.ar2) +NOMATCH (a, b, ggrid_t, ar2); +if (a.ar1 != b.ar1) +NOMATCH (a, b, ggrid_t, ar1); +} + +void compare_near_field () +{ +if (a.valid != b.valid) +NOMATCH (a, b, near_field_t, valid); +if (a.r_max != b.r_max) +NOMATCH (a, b, near_field_t, r_max); +if (a.pz != b.pz) +NOMATCH (a, b, near_field_t, pz); +if (a.py != b.py) +NOMATCH (a, b, near_field_t, py); +if (a.px != b.px) +NOMATCH (a, b, near_field_t, px); +if (a.newer != b.newer) +NOMATCH (a, b, near_field_t, newer); +if (a.max_hr != b.max_hr) +NOMATCH (a, b, near_field_t, max_hr); +if (a.max_er != b.max_er) +NOMATCH (a, b, near_field_t, max_er); +if (a.hz != b.hz) +NOMATCH (a, b, near_field_t, hz); +if (a.hy != b.hy) +NOMATCH (a, b, near_field_t, hy); +if (a.hx != b.hx) +NOMATCH (a, b, near_field_t, hx); +if (a.hrz != b.hrz) +NOMATCH (a, b, near_field_t, hrz); +if (a.hry != b.hry) +NOMATCH (a, b, near_field_t, hry); +if (a.hrx != b.hrx) +NOMATCH (a, b, near_field_t, hrx); +if (a.hr != b.hr) +NOMATCH (a, b, near_field_t, hr); +if (a.fhz != b.fhz) +NOMATCH (a, b, near_field_t, fhz); +if (a.fhy != b.fhy) +NOMATCH (a, b, near_field_t, fhy); +if (a.fhx != b.fhx) +NOMATCH (a, b, near_field_t, fhx); +if (a.fez != b.fez) +NOMATCH (a, b, near_field_t, fez); +if (a.fey != b.fey) +NOMATCH (a, b, near_field_t, fey); +if (a.fex != b.fex) +NOMATCH (a, b, near_field_t, fex); +if (a.ez != b.ez) +NOMATCH (a, b, near_field_t, ez); +if (a.ey != b.ey) +NOMATCH (a, b, near_field_t, ey); +if (a.ex != b.ex) +NOMATCH (a, b, near_field_t, ex); +if (a.erz != b.erz) +NOMATCH (a, b, near_field_t, erz); +if (a.ery != b.ery) +NOMATCH (a, b, near_field_t, ery); +if (a.erx != b.erx) +NOMATCH (a, b, near_field_t, erx); +if (a.er != b.er) +NOMATCH (a, b, near_field_t, er); +if (a.anim_step != b.anim_step) +NOMATCH (a, b, near_field_t, anim_step); +} + +void compare_crnt () +{ +if (a.valid != b.valid) +NOMATCH (a, b, crnt_t, valid); +if (a.newer != b.newer) +NOMATCH (a, b, crnt_t, newer); +if (a.cur != b.cur) +NOMATCH (a, b, crnt_t, cur); +if (a.cir != b.cir) +NOMATCH (a, b, crnt_t, cir); +if (a.cii != b.cii) +NOMATCH (a, b, crnt_t, cii); +if (a.bir != b.bir) +NOMATCH (a, b, crnt_t, bir); +if (a.bii != b.bii) +NOMATCH (a, b, crnt_t, bii); +if (a.air != b.air) +NOMATCH (a, b, crnt_t, air); +if (a.aii != b.aii) +NOMATCH (a, b, crnt_t, aii); }
Quoted 9 lines · Eric Wheeler
Quoted 6 lines · Eric Wheeler
Quoted 5 lines · Eric Wheeler
I have a bunch of structs with lots of members. I need a way to compare to instances of those structs to see if they have changed. The easiest way to do that is compare
if (a.j != b.j) { match() }but it would be lots of tedious hand-typing all those if's, or doing a bunch of ugly greps and cuts on the .c files to hand-parse what I want.I am continuously amazed by what Coccinelle is capable of.
That looks really nice. I'm not sure if anyone has considered this use case previously :)
julia
Quoted 668 lines
We are working to make the xnec2c EM simulator thread safe (www.xnec2c.org) but we need to figure out what is being modified between frequency loops to isolate the data. Check this out:
spatch --sp-file cocci/nec-data-diff.cocci t.c src/common.h src/shared.c
src/common.handsrc/shared.care in the xnec2c codebase if you're curious:
https://github.com/KJ7LNW/xnec2c/tree/master/srcThis is
t.cwhere I'm staging the comparisons:typedef struct { int x, y, z; int u, v; int w; } A; A a, b; #define NOMATCH(s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member); f() { }Here is the SmPL:
// Match these struct types: @ r1 @ typedef cm_t, cmag_t, crnt_t, ct1m_t, ct2m_t, data_t, dataj_t, fpat_t, ggrid_t, gnd_t, gwav_t, impedance_data_t, incom_t, matpar_t, near_field_t, netcx_t, save_t, segj_t, smat_t, zload_t, vsorc_t; type T = {cm_t, cmag_t, crnt_t, ct1m_t, ct2m_t, data_t, dataj_t, fpat_t, ggrid_t, gnd_t, gwav_t, impedance_data_t, incom_t, matpar_t, near_field_t, netcx_t, save_t, segj_t, smat_t, zload_t, vsorc_t}; type FT; identifier F; @@ typedef struct { ... FT F; ... } T; // Match the variable definition: @ struct_name @ type r1.T; identifier I; @@ T I; // Add the compare_T() function call and compare_T function definition for each type: @ r2 @ identifier struct_name.I; fresh identifier FN = "compare_"##I; @@ f() { ++ FN(); } ++void FN(){} // Add the comparisons for each type member: @ r3 @ identifier r2.FN; type r1.T; identifier r1.F; @@ void FN() { ... ++ if (a.F != b.F) NOMATCH(a, b, T, F); }And here is the output that probably saved hours of time.
Kudos to the Coccinelle team!
[root@el7-dev xnec2c-git]# spatch --sp-file
cocci/nec-data-diff.coccit.csrc/common.hsrc/shared.cinit_defs_builtins:/usr/local/coccinelle-git/bin/lib/coccinelle/standard.h
HANDLING:t.csrc/common.hsrc/shared.c
diff =tmp/cocci-output-26246-5722fe-t.c renamed from t.c +573
-0@@ -10,6 +10,579 @@ A a, b;#define NOMATCH (s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member); f() { +compare_impedance_data (); +compare_gwav (); +compare_smat (); +compare_zload (); +compare_incom (); +compare_netcx (); +compare_vsorc (); +compare_matpar (); +compare_fpat (); +compare_gnd (); +compare_save (); +compare_data (); +compare_dataj (); +compare_segj (); +compare_ggrid (); +compare_near_field (); +compare_crnt (); +} + +void compare_impedance_data () +{ +if (a.zreal != b.zreal) +NOMATCH (a, b, impedance_data_t, zreal); +if (a.zphase != b.zphase) +NOMATCH (a, b, impedance_data_t, zphase); +if (a.zmagn != b.zmagn) +NOMATCH (a, b, impedance_data_t, zmagn); +if (a.zimag != b.zimag) +NOMATCH (a, b, impedance_data_t, zimag); +} + +void compare_gwav () +{ +if (a.zph != b.zph) +NOMATCH (a, b, gwav_t, zph); +if (a.zmh != b.zmh) +NOMATCH (a, b, gwav_t, zmh); +if (a.xx2 != b.xx2) +NOMATCH (a, b, gwav_t, xx2); +if (a.xx1 != b.xx1) +NOMATCH (a, b, gwav_t, xx1); +if (a.u2 != b.u2) +NOMATCH (a, b, gwav_t, u2); +if (a.u != b.u) +NOMATCH (a, b, gwav_t, u); +if (a.r2 != b.r2) +NOMATCH (a, b, gwav_t, r2); +if (a.r1 != b.r1) +NOMATCH (a, b, gwav_t, r1); +} + +void compare_smat () +{ +if (a.ssx != b.ssx) +NOMATCH (a, b, smat_t, ssx); +if (a.nop != b.nop) +NOMATCH (a, b, smat_t, nop); +} + +void compare_zload () +{ +if (a.zarray != b.zarray) +NOMATCH (a, b, zload_t, zarray); +if (a.nload != b.nload) +NOMATCH (a, b, zload_t, nload); +if (a.nldseg != b.nldseg) +NOMATCH (a, b, zload_t, nldseg); +if (a.ldtype != b.ldtype) +NOMATCH (a, b, zload_t, ldtype); +if (a.ldsegn != b.ldsegn) +NOMATCH (a, b, zload_t, ldsegn); +} + +void compare_incom () +{ +if (a.zo != b.zo) +NOMATCH (a, b, incom_t, zo); +if (a.ysn != b.ysn) +NOMATCH (a, b, incom_t, ysn); +if (a.yo != b.yo) +NOMATCH (a, b, incom_t, yo); +if (a.xsn != b.xsn) +NOMATCH (a, b, incom_t, xsn); +if (a.xo != b.xo) +NOMATCH (a, b, incom_t, xo); +if (a.sn != b.sn) +NOMATCH (a, b, incom_t, sn); +if (a.isnor != b.isnor) +NOMATCH (a, b, incom_t, isnor); +} + +void compare_netcx () +{ +if (a.zped != b.zped) +NOMATCH (a, b, netcx_t, zped); +if (a.x22r != b.x22r) +NOMATCH (a, b, netcx_t, x22r); +if (a.x22i != b.x22i) +NOMATCH (a, b, netcx_t, x22i); +if (a.x12r != b.x12r) +NOMATCH (a, b, netcx_t, x12r); +if (a.x12i != b.x12i) +NOMATCH (a, b, netcx_t, x12i); +if (a.x11r != b.x11r) +NOMATCH (a, b, netcx_t, x11r); +if (a.x11i != b.x11i) +NOMATCH (a, b, netcx_t, x11i); +if (a.pnls != b.pnls) +NOMATCH (a, b, netcx_t, pnls); +if (a.pin != b.pin) +NOMATCH (a, b, netcx_t, pin); +if (a.ntyp != b.ntyp) +NOMATCH (a, b, netcx_t, ntyp); +if (a.ntsol != b.ntsol) +NOMATCH (a, b, netcx_t, ntsol); +if (a.nprint != b.nprint) +NOMATCH (a, b, netcx_t, nprint); +if (a.npeq != b.npeq) +NOMATCH (a, b, netcx_t, npeq); +if (a.nonet != b.nonet) +NOMATCH (a, b, netcx_t, nonet); +if (a.neq2 != b.neq2) +NOMATCH (a, b, netcx_t, neq2); +if (a.neq != b.neq) +NOMATCH (a, b, netcx_t, neq); +if (a.masym != b.masym) +NOMATCH (a, b, netcx_t, masym); +if (a.iseg2 != b.iseg2) +NOMATCH (a, b, netcx_t, iseg2); +if (a.iseg1 != b.iseg1) +NOMATCH (a, b, netcx_t, iseg1); +} + +void compare_vsorc () +{ +if (a.vsant != b.vsant) +NOMATCH (a, b, vsorc_t, vsant); +if (a.vqds != b.vqds) +NOMATCH (a, b, vsorc_t, vqds); +if (a.vqd != b.vqd) +NOMATCH (a, b, vsorc_t, vqd); +if (a.nvqd != b.nvqd) +NOMATCH (a, b, vsorc_t, nvqd); +if (a.nsant != b.nsant) +NOMATCH (a, b, vsorc_t, nsant); +if (a.nqds != b.nqds) +NOMATCH (a, b, vsorc_t, nqds); +if (a.ivqd != b.ivqd) +NOMATCH (a, b, vsorc_t, ivqd); +if (a.isant != b.isant) +NOMATCH (a, b, vsorc_t, isant); +if (a.iqds != b.iqds) +NOMATCH (a, b, vsorc_t, iqds); +} + +void compare_matpar () +{ +if (a.npblk != b.npblk) +NOMATCH (a, b, matpar_t, npblk); +if (a.nlast != b.nlast) +NOMATCH (a, b, matpar_t, nlast); +if (a.imat != b.imat) +NOMATCH (a, b, matpar_t, imat); +if (a.icase != b.icase) +NOMATCH (a, b, matpar_t, icase); +} + +void compare_fpat () +{ +if (a.znr != b.znr) +NOMATCH (a, b, fpat_t, znr); +if (a.ynr != b.ynr) +NOMATCH (a, b, fpat_t, ynr); +if (a.xnr != b.xnr) +NOMATCH (a, b, fpat_t, xnr); +if (a.thets != b.thets) +NOMATCH (a, b, fpat_t, thets); +if (a.sig2 != b.sig2) +NOMATCH (a, b, fpat_t, sig2); +if (a.rfld != b.rfld) +NOMATCH (a, b, fpat_t, rfld); +if (a.pnlr != b.pnlr) +NOMATCH (a, b, fpat_t, pnlr); +if (a.ploss != b.ploss) +NOMATCH (a, b, fpat_t, ploss); +if (a.pinr != b.pinr) +NOMATCH (a, b, fpat_t, pinr); +if (a.phis != b.phis) +NOMATCH (a, b, fpat_t, phis); +if (a.nth != b.nth) +NOMATCH (a, b, fpat_t, nth); +if (a.nrz != b.nrz) +NOMATCH (a, b, fpat_t, nrz); +if (a.nry != b.nry) +NOMATCH (a, b, fpat_t, nry); +if (a.nrx != b.nrx) +NOMATCH (a, b, fpat_t, nrx); +if (a.nph != b.nph) +NOMATCH (a, b, fpat_t, nph); +if (a.nfeh != b.nfeh) +NOMATCH (a, b, fpat_t, nfeh); +if (a.near != b.near) +NOMATCH (a, b, fpat_t, near); +if (a.ixtyp != b.ixtyp) +NOMATCH (a, b, fpat_t, ixtyp); +if (a.ipd != b.ipd) +NOMATCH (a, b, fpat_t, ipd); +if (a.inor != b.inor) +NOMATCH (a, b, fpat_t, inor); +if (a.iax != b.iax) +NOMATCH (a, b, fpat_t, iax); +if (a.iavp != b.iavp) +NOMATCH (a, b, fpat_t, iavp); +if (a.gnor != b.gnor) +NOMATCH (a, b, fpat_t, gnor); +if (a.epsr2 != b.epsr2) +NOMATCH (a, b, fpat_t, epsr2); +if (a.dznr != b.dznr) +NOMATCH (a, b, fpat_t, dznr); +if (a.dynr != b.dynr) +NOMATCH (a, b, fpat_t, dynr); +if (a.dxnr != b.dxnr) +NOMATCH (a, b, fpat_t, dxnr); +if (a.dth != b.dth) +NOMATCH (a, b, fpat_t, dth); +if (a.dph != b.dph) +NOMATCH (a, b, fpat_t, dph); +if (a.clt != b.clt) +NOMATCH (a, b, fpat_t, clt); +if (a.cht != b.cht) +NOMATCH (a, b, fpat_t, cht); +} + +void compare_gnd () +{ +if (a.zrati2 != b.zrati2) +NOMATCH (a, b, gnd_t, zrati2); +if (a.zrati != b.zrati) +NOMATCH (a, b, gnd_t, zrati); +if (a.t2 != b.t2) +NOMATCH (a, b, gnd_t, t2); +if (a.t1 != b.t1) +NOMATCH (a, b, gnd_t, t1); +if (a.scrwr != b.scrwr) +NOMATCH (a, b, gnd_t, scrwr); +if (a.scrwl != b.scrwl) +NOMATCH (a, b, gnd_t, scrwl); +if (a.nradl != b.nradl) +NOMATCH (a, b, gnd_t, nradl); +if (a.ksymp != b.ksymp) +NOMATCH (a, b, gnd_t, ksymp); +if (a.iperf != b.iperf) +NOMATCH (a, b, gnd_t, iperf); +if (a.ifar != b.ifar) +NOMATCH (a, b, gnd_t, ifar); +if (a.gpflag != b.gpflag) +NOMATCH (a, b, gnd_t, gpflag); +if (a.frati != b.frati) +NOMATCH (a, b, gnd_t, frati); +if (a.cl != b.cl) +NOMATCH (a, b, gnd_t, cl); +if (a.ch != b.ch) +NOMATCH (a, b, gnd_t, ch); +} + +void compare_save () +{ +if (a.ztemp != b.ztemp) +NOMATCH (a, b, save_t, ztemp); +if (a.ytemp != b.ytemp) +NOMATCH (a, b, save_t, ytemp); +if (a.xtemp != b.xtemp) +NOMATCH (a, b, save_t, xtemp); +if (a.sitemp != b.sitemp) +NOMATCH (a, b, save_t, sitemp); +if (a.sig != b.sig) +NOMATCH (a, b, save_t, sig); +if (a.scrwrt != b.scrwrt) +NOMATCH (a, b, save_t, scrwrt); +if (a.scrwlt != b.scrwlt) +NOMATCH (a, b, save_t, scrwlt); +if (a.last_freq != b.last_freq) +NOMATCH (a, b, save_t, last_freq); +if (a.ip != b.ip) +NOMATCH (a, b, save_t, ip); +if (a.fstep != b.fstep) +NOMATCH (a, b, save_t, fstep); +if (a.freq != b.freq) +NOMATCH (a, b, save_t, freq); +if (a.fmhz != b.fmhz) +NOMATCH (a, b, save_t, fmhz); +if (a.epsr != b.epsr) +NOMATCH (a, b, save_t, epsr); +if (a.bitemp != b.bitemp) +NOMATCH (a, b, save_t, bitemp); +} + +void compare_data () +{ +if (a.z2 != b.z2) +NOMATCH (a, b, data_t, z2); +if (a.z1 != b.z1) +NOMATCH (a, b, data_t, z1); +if (a.z != b.z) +NOMATCH (a, b, data_t, z); +if (a.y2 != b.y2) +NOMATCH (a, b, data_t, y2); +if (a.y1 != b.y1) +NOMATCH (a, b, data_t, y1); +if (a.y != b.y) +NOMATCH (a, b, data_t, y); +if (a.x2 != b.x2) +NOMATCH (a, b, data_t, x2); +if (a.x1 != b.x1) +NOMATCH (a, b, data_t, x1); +if (a.x != b.x) +NOMATCH (a, b, data_t, x); +if (a.wlam != b.wlam) +NOMATCH (a, b, data_t, wlam); +if (a.t2z != b.t2z) +NOMATCH (a, b, data_t, t2z); +if (a.t2y != b.t2y) +NOMATCH (a, b, data_t, t2y); +if (a.t2x != b.t2x) +NOMATCH (a, b, data_t, t2x); +if (a.t1z != b.t1z) +NOMATCH (a, b, data_t, t1z); +if (a.t1y != b.t1y) +NOMATCH (a, b, data_t, t1y); +if (a.t1x != b.t1x) +NOMATCH (a, b, data_t, t1x); +if (a.si != b.si) +NOMATCH (a, b, data_t, si); +if (a.salp != b.salp) +NOMATCH (a, b, data_t, salp); +if (a.sab != b.sab) +NOMATCH (a, b, data_t, sab); +if (a.pz2 != b.pz2) +NOMATCH (a, b, data_t, pz2); +if (a.pz1 != b.pz1) +NOMATCH (a, b, data_t, pz1); +if (a.pz != b.pz) +NOMATCH (a, b, data_t, pz); +if (a.py2 != b.py2) +NOMATCH (a, b, data_t, py2); +if (a.py1 != b.py1) +NOMATCH (a, b, data_t, py1); +if (a.py != b.py) +NOMATCH (a, b, data_t, py); +if (a.px2 != b.px2) +NOMATCH (a, b, data_t, px2); +if (a.px1 != b.px1) +NOMATCH (a, b, data_t, px1); +if (a.px != b.px) +NOMATCH (a, b, data_t, px); +if (a.psalp != b.psalp) +NOMATCH (a, b, data_t, psalp); +if (a.pbi != b.pbi) +NOMATCH (a, b, data_t, pbi); +if (a.npm != b.npm) +NOMATCH (a, b, data_t, npm); +if (a.np3m != b.np3m) +NOMATCH (a, b, data_t, np3m); +if (a.np2m != b.np2m) +NOMATCH (a, b, data_t, np2m); +if (a.np != b.np) +NOMATCH (a, b, data_t, np); +if (a.n != b.n) +NOMATCH (a, b, data_t, n); +if (a.mp != b.mp) +NOMATCH (a, b, data_t, mp); +if (a.m != b.m) +NOMATCH (a, b, data_t, m); +if (a.itag != b.itag) +NOMATCH (a, b, data_t, itag); +if (a.ipsym != b.ipsym) +NOMATCH (a, b, data_t, ipsym); +if (a.icon2 != b.icon2) +NOMATCH (a, b, data_t, icon2); +if (a.icon1 != b.icon1) +NOMATCH (a, b, data_t, icon1); +if (a.cab != b.cab) +NOMATCH (a, b, data_t, cab); +if (a.bi != b.bi) +NOMATCH (a, b, data_t, bi); +} + +void compare_dataj () +{ +if (a.zj != b.zj) +NOMATCH (a, b, dataj_t, zj); +if (a.yj != b.yj) +NOMATCH (a, b, dataj_t, yj); +if (a.xj != b.xj) +NOMATCH (a, b, dataj_t, xj); +if (a.t2zj != b.t2zj) +NOMATCH (a, b, dataj_t, t2zj); +if (a.t2yj != b.t2yj) +NOMATCH (a, b, dataj_t, t2yj); +if (a.t2xj != b.t2xj) +NOMATCH (a, b, dataj_t, t2xj); +if (a.t1zj != b.t1zj) +NOMATCH (a, b, dataj_t, t1zj); +if (a.t1yj != b.t1yj) +NOMATCH (a, b, dataj_t, t1yj); +if (a.t1xj != b.t1xj) +NOMATCH (a, b, dataj_t, t1xj); +if (a.salpj != b.salpj) +NOMATCH (a, b, dataj_t, salpj); +if (a.sabj != b.sabj) +NOMATCH (a, b, dataj_t, sabj); +if (a.s != b.s) +NOMATCH (a, b, dataj_t, s); +if (a.rkh != b.rkh) +NOMATCH (a, b, dataj_t, rkh); +if (a.ipgnd != b.ipgnd) +NOMATCH (a, b, dataj_t, ipgnd); +if (a.indd2 != b.indd2) +NOMATCH (a, b, dataj_t, indd2); +if (a.indd1 != b.indd1) +NOMATCH (a, b, dataj_t, indd1); +if (a.ind2 != b.ind2) +NOMATCH (a, b, dataj_t, ind2); +if (a.ind1 != b.ind1) +NOMATCH (a, b, dataj_t, ind1); +if (a.iexk != b.iexk) +NOMATCH (a, b, dataj_t, iexk); +if (a.ezs != b.ezs) +NOMATCH (a, b, dataj_t, ezs); +if (a.ezk != b.ezk) +NOMATCH (a, b, dataj_t, ezk); +if (a.ezc != b.ezc) +NOMATCH (a, b, dataj_t, ezc); +if (a.eys != b.eys) +NOMATCH (a, b, dataj_t, eys); +if (a.eyk != b.eyk) +NOMATCH (a, b, dataj_t, eyk); +if (a.eyc != b.eyc) +NOMATCH (a, b, dataj_t, eyc); +if (a.exs != b.exs) +NOMATCH (a, b, dataj_t, exs); +if (a.exk != b.exk) +NOMATCH (a, b, dataj_t, exk); +if (a.exc != b.exc) +NOMATCH (a, b, dataj_t, exc); +if (a.cabj != b.cabj) +NOMATCH (a, b, dataj_t, cabj); +if (a.b != b.b) +NOMATCH (a, b, dataj_t, b); +} + +void compare_segj () +{ +if (a.maxcon != b.maxcon) +NOMATCH (a, b, segj_t, maxcon); +if (a.jsno != b.jsno) +NOMATCH (a, b, segj_t, jsno); +if (a.jco != b.jco) +NOMATCH (a, b, segj_t, jco); +if (a.cx != b.cx) +NOMATCH (a, b, segj_t, cx); +if (a.bx != b.bx) +NOMATCH (a, b, segj_t, bx); +if (a.ax != b.ax) +NOMATCH (a, b, segj_t, ax); +} + +void compare_ggrid () +{ +if (a.ysa != b.ysa) +NOMATCH (a, b, ggrid_t, ysa); +if (a.xsa != b.xsa) +NOMATCH (a, b, ggrid_t, xsa); +if (a.nya != b.nya) +NOMATCH (a, b, ggrid_t, nya); +if (a.nxa != b.nxa) +NOMATCH (a, b, ggrid_t, nxa); +if (a.epscf != b.epscf) +NOMATCH (a, b, ggrid_t, epscf); +if (a.dya != b.dya) +NOMATCH (a, b, ggrid_t, dya); +if (a.dxa != b.dxa) +NOMATCH (a, b, ggrid_t, dxa); +if (a.ar3 != b.ar3) +NOMATCH (a, b, ggrid_t, ar3); +if (a.ar2 != b.ar2) +NOMATCH (a, b, ggrid_t, ar2); +if (a.ar1 != b.ar1) +NOMATCH (a, b, ggrid_t, ar1); +} + +void compare_near_field () +{ +if (a.valid != b.valid) +NOMATCH (a, b, near_field_t, valid); +if (a.r_max != b.r_max) +NOMATCH (a, b, near_field_t, r_max); +if (a.pz != b.pz) +NOMATCH (a, b, near_field_t, pz); +if (a.py != b.py) +NOMATCH (a, b, near_field_t, py); +if (a.px != b.px) +NOMATCH (a, b, near_field_t, px); +if (a.newer != b.newer) +NOMATCH (a, b, near_field_t, newer); +if (a.max_hr != b.max_hr) +NOMATCH (a, b, near_field_t, max_hr); +if (a.max_er != b.max_er) +NOMATCH (a, b, near_field_t, max_er); +if (a.hz != b.hz) +NOMATCH (a, b, near_field_t, hz); +if (a.hy != b.hy) +NOMATCH (a, b, near_field_t, hy); +if (a.hx != b.hx) +NOMATCH (a, b, near_field_t, hx); +if (a.hrz != b.hrz) +NOMATCH (a, b, near_field_t, hrz); +if (a.hry != b.hry) +NOMATCH (a, b, near_field_t, hry); +if (a.hrx != b.hrx) +NOMATCH (a, b, near_field_t, hrx); +if (a.hr != b.hr) +NOMATCH (a, b, near_field_t, hr); +if (a.fhz != b.fhz) +NOMATCH (a, b, near_field_t, fhz); +if (a.fhy != b.fhy) +NOMATCH (a, b, near_field_t, fhy); +if (a.fhx != b.fhx) +NOMATCH (a, b, near_field_t, fhx); +if (a.fez != b.fez) +NOMATCH (a, b, near_field_t, fez); +if (a.fey != b.fey) +NOMATCH (a, b, near_field_t, fey); +if (a.fex != b.fex) +NOMATCH (a, b, near_field_t, fex); +if (a.ez != b.ez) +NOMATCH (a, b, near_field_t, ez); +if (a.ey != b.ey) +NOMATCH (a, b, near_field_t, ey); +if (a.ex != b.ex) +NOMATCH (a, b, near_field_t, ex); +if (a.erz != b.erz) +NOMATCH (a, b, near_field_t, erz); +if (a.ery != b.ery) +NOMATCH (a, b, near_field_t, ery); +if (a.erx != b.erx) +NOMATCH (a, b, near_field_t, erx); +if (a.er != b.er) +NOMATCH (a, b, near_field_t, er); +if (a.anim_step != b.anim_step) +NOMATCH (a, b, near_field_t, anim_step); +} + +void compare_crnt () +{ +if (a.valid != b.valid) +NOMATCH (a, b, crnt_t, valid); +if (a.newer != b.newer) +NOMATCH (a, b, crnt_t, newer); +if (a.cur != b.cur) +NOMATCH (a, b, crnt_t, cur); +if (a.cir != b.cir) +NOMATCH (a, b, crnt_t, cir); +if (a.cii != b.cii) +NOMATCH (a, b, crnt_t, cii); +if (a.bir != b.bir) +NOMATCH (a, b, crnt_t, bir); +if (a.bii != b.bii) +NOMATCH (a, b, crnt_t, bii); +if (a.air != b.air) +NOMATCH (a, b, crnt_t, air); +if (a.aii != b.aii) +NOMATCH (a, b, crnt_t, aii); }
Quoted 2 lines
I am continuously amazed by what Coccinelle is capable of.
I am curious how your development interests will grow also in this area.
Quoted 10 lines
// Add the compare_T() function call and compare_T function definition for each type:
@ r2 @
identifier struct_name.I;
fresh identifier FN = "compare_"##I;
@@
f()
{
++ FN();
}
++void FN(){}I suggest to reconsider this SmPL change approach a bit more.
I doubt that you would like to add only a single function (or macro) call within the implementation of the function “f”.
See also:
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/20fdb67f4b20a242f222337e13091115884cf6bb/docs/manual/cocci_syntax.tex#L1318
https://github.com/coccinelle/coccinelle/blob/ae337fce1512ff15aabc3ad5b6d2e537f97ab62a/docs/manual/cocci_syntax.tex#L1318
Are you sure that the specification of an empty function implementation would be appropriate here?
Regards,
Markus
Quoted 20 lines · Markus Elfring
Quoted 2 lines
I am continuously amazed by what Coccinelle is capable of.
I am curious how your development interests will grow also in this area.
Quoted 10 lines
// Add the compare_T() function call and compare_T function definition for each type: @ r2 @ identifier struct_name.I; fresh identifier FN = "compare_"##I; @@ f() { ++ FN(); } ++void FN(){}
Quoted 4 lines
I suggest to reconsider this SmPL change approach a bit more.
I doubt that you would like to add only a single function (or macro) call within the implementation of the function “f”.
He has ++, which allows adding as many definitions and calls as there are values of name.I.
julia
Quoted 13 lines
See also:
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/20fdb67f4b20a242f222337e13091115884cf6bb/docs/manual/cocci_syntax.tex#L1318
https://github.com/coccinelle/coccinelle/blob/ae337fce1512ff15aabc3ad5b6d2e537f97ab62a/docs/manual/cocci_syntax.tex#L1318Are you sure that the specification of an empty function implementation would be appropriate here?
Regards,
Markus
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
// Add the compare_T() function call and compare_T function definition for each type: @ r2 @ identifier struct_name.I; fresh identifier FN = "compare_"##I; @@ f() { ++ FN(); } ++void FN(){}
…
Quoted 4 lines
Quoted 2 lines
I doubt that you would like to add only a single function (or macro) call within the implementation of the function “f”.
He has ++, which allows adding as many definitions and calls as there are values of name.I.
How do you think about the usage of further change details?
* Should the identifier “f” be a metavariable?
* Additional context information for relevant source code places
* Non-empty function implementations are probably preferred, aren't they?
Regards,
Markus
Quoted 17 lines · Markus Elfring
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
// Add the compare_T() function call and compare_T function definition for each type: @ r2 @ identifier struct_name.I; fresh identifier FN = "compare_"##I; @@ f() { ++ FN(); } ++void FN(){}…
Quoted 4 lines
Quoted 2 lines
I doubt that you would like to add only a single function (or macro) call within the implementation of the function “f”.
He has ++, which allows adding as many definitions and calls as there are values of name.I.
Quoted 7 lines
How do you think about the usage of further change details?
* Should the identifier “f” be a metavariable?
* Additional context information for relevant source code places
* Non-empty function implementations are probably preferred, aren't they?
Look closer at later rules in the SmPL. The empty function definition is built out further down. That is a placeholder for the compare_T functions that hold the all comparisons.
The reason for the functions is to group them by type (or really struct name) because otherwise ++ will make them randomly ordered. At least grouping by function gave some amount of organization.
-Eric
Quoted 5 lines
Regards,
Markus
Quoted 5 lines
Quoted 3 lines
* Additional context information for relevant source code places
* Non-empty function implementations are probably preferred, aren't they?
Look closer at later rules in the SmPL. The empty function definition is built out further down.
I noticed this also.
How do you think about to try out if the desired function implementations can be directly constructed by the SmPL rule “r2” (instead of using an extra third SmPL rule)?
Quoted 2 lines
That is a placeholder for the
compare_Tfunctions
that hold the all comparisons.
This aspect is reasonable.
I propose to take another look at possibilities for fine-tuning of SmPL code. Would you like to recheck the need for any SmPL ellipses?
Regards,
Markus
Re: [cocci] Improving the implementation of the software “Xnec2c”
In reply to Eric Wheeler…
Quoted 3 lines
We are working to make the xnec2c EM simulator thread safe (www.xnec2c.org) but we need to figure out what is being modified between frequency loops to isolate the data.
…
Quoted 2 lines
src/common.handsrc/shared.care in the xnec2c codebase if you're curious:
https://github.com/KJ7LNW/xnec2c/tree/master/src
I suggest to increase the usage of advanced static source code analysis and transformation tools so that remaining open issues will be improved also for this software.
Example:
Would you like to fix signal handling here?
https://github.com/KJ7LNW/xnec2c/blob/956d33d923849a4f80ec47fb8afb90258e83cf24/src/main.c#L521
Regards,
Markus
Re: [cocci] Improving the implementation of the software “Xnec2c”
Quoted 8 lines · Markus Elfring
Quoted 3 lines
We are working to make the xnec2c EM simulator thread safe (www.xnec2c.org) but we need to figure out what is being modified between frequency loops to isolate the data.
…
Quoted 2 lines
src/common.handsrc/shared.care in the xnec2c codebase if you're curious:
https://github.com/KJ7LNW/xnec2c/tree/master/src
Quoted 6 lines
I suggest to increase the usage of advanced static source code analysis and transformation tools so that remaining open issues will be improved also for this software.
Example:
Would you like to fix signal handling here?
https://github.com/KJ7LNW/xnec2c/blob/956d33d923849a4f80ec47fb8afb90258e83cf24/src/main.c#L521
I'm sure there are many places that the code could be cleaned up. I'm open to patches and PR's if you'd like to help on the project.
--
Eric Wheeler
Quoted 5 lines
Regards,
Markus
Quoted 85 lines · Eric Wheeler
Quoted 27 lines · Eric Wheeler
I have a bunch of structs with lots of members. I need a way to compare to instances of those structs to see if they have changed. The easiest way to do that is compare
if (a.j != b.j) { match() }but it would be lots of tedious hand-typing all those if's, or doing a bunch of ugly greps and cuts on the .c files to hand-parse what I want.I would like to use Coccinelle to add all those struct member comparisons for me. It would be really great if this is possible because there are a dozen structs each with lots of members:
Lets say I have
t.cas follows:typedef struct { int x, y, z; int u, v; int w; } A; A a, b; #define NOMATCH(s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member); f() { }Quoted 8 lines
The NOMATCH macro will stringify the struct members so I can print them for debugging. For example, inside of
f()I would like to add this:if (a.F != b.F) NOMATCH(a, b, F);I started writing this SmPL from what I've learned about Coccinelle so far and I don't quite have it working. This almost works:
Oh! Adding another ... and changing + to ++ did it:
@ r1 @ typedef A; type T, FT; identifier F, j; @@ typedef struct { ... FT F@j; ... } T; @ r2 @ type r1.T; identifier r1.F; identifier r1.j; @@ f() { ... ++ if (a.j != b.j) NOMATCH(a, b, j); }which produces:
--- t.c +++ /tmp/cocci-output-19090-251154-t.c @@ -10,4 +10,16 @@ A a, b; #define NOMATCH(s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member); f() { + if (a.z != b.z) + NOMATCH(a, b, z); + if (a.y != b.y) + NOMATCH(a, b, y); + if (a.x != b.x) + NOMATCH(a, b, x); + if (a.w != b.w) + NOMATCH(a, b, w); + if (a.v != b.v) + NOMATCH(a, b, v); + if (a.u != b.u) + NOMATCH(a, b, u); }
Quoted 3 lines
So, hurray for the SmPL ++ operator! Also it doesn't look like I needed @j.
Is this how you would approach the problem?
OK, I see now. This seems like a good solution.
julia
Quoted 38 lines
-Eric
Quoted 33 lines
which produces:
--- t.c +++ /tmp/cocci-output-7874-a515f6-t.c @@ -10,4 +10,6 @@ A a, b; #define NOMATCH(s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member); f() { + if (a.w != b.w) + NOMATCH(a, b, w); }Can Coccinelle do this for all struct members?
I tried playing with
<+ ... F ... +>and... when exists when any .... I've also triedfieldandfield listinstead oftypewithidentifierwithing thetypedef struct { ... }braces.Note that the number of variables declared on a single line varies:
int x, y, z; int u, v; int w;so I know that the
FT F;part of the SmPL is insufficent, but I'm not sure how to address that to match all struct declarations.Ideas?
-Eric
Quoted 79 lines · Eric Wheeler
Hi all,
I have a bunch of structs with lots of members. I need a way to compare to instances of those structs to see if they have changed. The easiest way to do that is compare
if (a.j != b.j) { match() }but it would be lots of tedious hand-typing all those if's, or doing a bunch of ugly greps and cuts on the .c files to hand-parse what I want.I would like to use Coccinelle to add all those struct member comparisons for me. It would be really great if this is possible because there are a dozen structs each with lots of members:
Lets say I have
t.cas follows:typedef struct { int x, y, z; int u, v; int w; } A; A a, b; #define NOMATCH(s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member); f() { }The NOMATCH macro will stringify the struct members so I can print them for debugging. For example, inside of
f()I would like to add this:if (a.F != b.F) NOMATCH(a, b, F);I started writing this SmPL from what I've learned about Coccinelle so far and I don't quite have it working. This almost works:
@ r1 @ typedef A; type T, FT; identifier F; @@ typedef struct { ... FT F; } T; @ r2 @ type r1.T; identifier r1.F; @@ f() { ... + if (a.F != b.F) NOMATCH(a, b, F); }which produces:
--- t.c +++ /tmp/cocci-output-7874-a515f6-t.c @@ -10,4 +10,6 @@ A a, b; #define NOMATCH(s1, s2, member) printf(#s1 "." #member "!=" #s2 "." #member); f() { + if (a.w != b.w) + NOMATCH(a, b, w); }Can Coccinelle do this for all struct members?
I tried playing with
<+ ... F ... +>and... when exists when any .... I've also triedfieldandfield listinstead oftypewithidentifierwithing thetypedef struct { ... }braces.Note that the number of variables declared on a single line varies:
Quoted 8 lines
int x, y, z; int u, v; int w;so I know that the
FT F;part of the SmPL is insufficent, but I'm not sure how to address that to match all struct declarations.Ideas?
Why not just use
@@
identifier i;
field list fs;
@@
struct i { fs };
julia