On Sun, 2020-08-30 at 10:27 +0200, Julia Lawall wrote: > > On Sun, 30 Aug 2020, Joe Perches wrote: > > > On Sun, 2020-08-30 at 08:57 +0200, Julia Lawall wrote: > > > On Sat, 29 Aug 2020, Joe Perches wrote: > > > > > > > Is it me not understanding cocci grammar again? > > > > > > The problem is the loop. You are trying to change something in the body > > > of a loop and the body of a for loop might not be executed. ... means > > > that the thing must be found on every execution path. > > > > > > Do you want to make the change in the function header even if there are > > > not changes in the body? If so, <... ...> is what you are looking for. > > > If you want to be sure there is a change to make in the function body then > > > you can use <+... ...+> but it will be more expensive. > > > > Thanks. <... works (and I thought I had tried that, oh well) > > > > Another thing I'd like to do is to change the various uses > > of a type and identifier to a specific type and identifier. > > > > In this sysfs_emit transform I've been working on, there > > are many variable names used in the assignment of the > > sysfs_emit result. > > > > $ git grep -P -oh '\w+\s*\+?=\s*sysfs_emit' | \ > > sort | uniq -c | sort -rn > > 145 ret = sysfs_emit > > 80 len = sysfs_emit > > 74 len += sysfs_emit > > 69 rc = sysfs_emit > > 50 count = sysfs_emit > > 25 count += sysfs_emit > > 19 size = sysfs_emit > > 17 n += sysfs_emit > > 15 n = sysfs_emit > > 14 status = sysfs_emit > > 12 ret += sysfs_emit > > 12 output_len += sysfs_emit > > 11 retval = sysfs_emit > > 9 res += sysfs_emit > > 7 rv = sysfs_emit > > 7 offset += sysfs_emit > > 7 l = sysfs_emit > > 6 i = sysfs_emit > > 5 size += sysfs_emit > > 5 err = sysfs_emit > > 4 written = sysfs_emit > > 4 l += sysfs_emit > > 3 written += sysfs_emit > > 2 rz = sysfs_emit > > 2 r = sysfs_emit > > 2 result = sysfs_emit > > 2 res = sysfs_emit > > 2 i += sysfs_emit > > 2 idx += sysfs_emit > > 2 error = sysfs_emit > > 2 cnt += sysfs_emit > > 2 buf_len += sysfs_emit > > 1 offset = sysfs_emit > > 1 length = sysfs_emit > > 1 cnt = sysfs_emit > > 1 bytes = sysfs_emit > > 1 bytes += sysfs_emit > > > > Most are declared int, some are ssize_t. > > > > I'd like to change them all to int len. > > @r exists@ > type T != int; > identifier x != len; > position p; > assignment operator aop; > @@ > > T x@p; > ... > x aop sysfs_emit > > @@ > type r.T; > identifier r.x; > position r.p; > @@ > > - T x@p; > + int len > <... > - x > + len > ...> > > This only works for the case where the type is not int and the name is not > len. You would need other similar pairs of rules for the case where the > type is int and the variable is something else and where the type is len > and the variable is len.
Thanks, I used the slightly different from your suggestion where sysfs is an identifier with function args and a semicolon after the transform type, (otherwise I get cocci errors). like this below: But it doesn't work (no renaming) when there is an initializer to the variable to be transformed. ie: { ssize_t count = 0; ... count += sysfs_emit_at(buf, count, ...); ... return count; } I tried adding =0 in various places without success. Suggestions? ------------------ // Rename the sysfs_emit assigned variables not named len and not already int // and set the name to len and type to int @not_int_not_len exists@ type T != int; identifier x != len; position p; identifier sysfs =~ "^sysfs_emit.*$"; assignment operator aop; @@ T x@p; ... x aop sysfs(...) ... @@ type not_int_not_len.T; identifier not_int_not_len.x; position not_int_not_len.p; @@ - T x@p; + int len; <... - x + len ...> ------------------ _______________________________________________ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci