object_id.cocci: match only expressions of type 'struct object_id'

Most of our semantic patches in 'contrib/coccinelle/object_id.cocci' turn calls of SHA1-specific functions into calls of their corresponding object_id counterparts, e.g. sha1_to_hex() to oid_to_hex(). These semantic patches look something like this: @@ expression E1; @@ - sha1_to_hex(E1.hash) + oid_to_hex(&E1) and match the access to the 'hash' field in any data type, not only in 'struct object_id', and, consquently, can produce wrong transformations. Case in point is the recent hash function transition patch "rerere: convert to use the_hash_algo" [1], which, among other things, renamed 'struct rerere_dir's 'sha1' field to 'hash', and then 'make coccicheck' started to suggest the following wrong transformations for 'rerere.c' [2]: - return sha1_to_hex(id->collection->hash); + return oid_to_hex(id->collection); and - DIR *dir = opendir(git_path("rr-cache/%s", sha1_to_hex(rr_dir->hash))); + DIR *dir = opendir(git_path("rr-cache/%s", oid_to_hex(rr_dir))); Avoid such wrong transformations by tightening semantic patches in 'object_id.cocci' to match only type of or pointers to 'struct object_id'. [1] https://public-inbox.org/git/20181008215701.779099-15-sandals@crustytoothpaste.net/ [2] https://travis-ci.org/git/git/jobs/440463476#L580 Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Oct 15, 2018 at 00:01 UTC 6afedba8c97b5a8463e45ff801218433f8392d5d
1 file changed +63 -54
contrib/coccinelle/object_id.cocci
+63 -54
@@ -1,119 +1,127 @@
1 @@
2 -expression E1;
2 +struct object_id OID;
3 @@
4 -- is_null_sha1(E1.hash)
5 -+ is_null_oid(&E1)
4 +- is_null_sha1(OID.hash)
5 ++ is_null_oid(&OID)
6
7 @@
8 -expression E1;
8 +struct object_id *OIDPTR;
9 @@
10 -- is_null_sha1(E1->hash)
11 -+ is_null_oid(E1)
10 +- is_null_sha1(OIDPTR->hash)
11 ++ is_null_oid(OIDPTR)
12
13 @@
14 -expression E1;
14 +struct object_id OID;
15 @@
16 -- sha1_to_hex(E1.hash)
17 -+ oid_to_hex(&E1)
16 +- sha1_to_hex(OID.hash)
17 ++ oid_to_hex(&OID)
18
19 @@
20 identifier f != oid_to_hex;
21 -expression E1;
21 +struct object_id *OIDPTR;
22 @@
23 f(...) {<...
24 -- sha1_to_hex(E1->hash)
25 -+ oid_to_hex(E1)
24 +- sha1_to_hex(OIDPTR->hash)
25 ++ oid_to_hex(OIDPTR)
26 ...>}
27
28 @@
29 -expression E1, E2;
29 +expression E;
30 +struct object_id OID;
31 @@
31 -- sha1_to_hex_r(E1, E2.hash)
32 -+ oid_to_hex_r(E1, &E2)
32 +- sha1_to_hex_r(E, OID.hash)
33 ++ oid_to_hex_r(E, &OID)
34
35 @@
36 identifier f != oid_to_hex_r;
36 -expression E1, E2;
37 +expression E;
38 +struct object_id *OIDPTR;
39 @@
40 f(...) {<...
39 -- sha1_to_hex_r(E1, E2->hash)
40 -+ oid_to_hex_r(E1, E2)
41 +- sha1_to_hex_r(E, OIDPTR->hash)
42 ++ oid_to_hex_r(E, OIDPTR)
43 ...>}
44
45 @@
44 -expression E1;
46 +struct object_id OID;
47 @@
46 -- hashclr(E1.hash)
47 -+ oidclr(&E1)
48 +- hashclr(OID.hash)
49 ++ oidclr(&OID)
50
51 @@
52 identifier f != oidclr;
51 -expression E1;
53 +struct object_id *OIDPTR;
54 @@
55 f(...) {<...
54 -- hashclr(E1->hash)
55 -+ oidclr(E1)
56 +- hashclr(OIDPTR->hash)
57 ++ oidclr(OIDPTR)
58 ...>}
59
60 @@
59 -expression E1, E2;
61 +struct object_id OID1, OID2;
62 @@
61 -- hashcmp(E1.hash, E2.hash)
62 -+ oidcmp(&E1, &E2)
63 +- hashcmp(OID1.hash, OID2.hash)
64 ++ oidcmp(&OID1, &OID2)
65
66 @@
67 identifier f != oidcmp;
66 -expression E1, E2;
68 +struct object_id *OIDPTR1, OIDPTR2;
69 @@
70 f(...) {<...
69 -- hashcmp(E1->hash, E2->hash)
70 -+ oidcmp(E1, E2)
71 +- hashcmp(OIDPTR1->hash, OIDPTR2->hash)
72 ++ oidcmp(OIDPTR1, OIDPTR2)
73 ...>}
74
75 @@
74 -expression E1, E2;
76 +struct object_id *OIDPTR;
77 +struct object_id OID;
78 @@
76 -- hashcmp(E1->hash, E2.hash)
77 -+ oidcmp(E1, &E2)
79 +- hashcmp(OIDPTR->hash, OID.hash)
80 ++ oidcmp(OIDPTR, &OID)
81
82 @@
80 -expression E1, E2;
83 +struct object_id *OIDPTR;
84 +struct object_id OID;
85 @@
82 -- hashcmp(E1.hash, E2->hash)
83 -+ oidcmp(&E1, E2)
86 +- hashcmp(OID.hash, OIDPTR->hash)
87 ++ oidcmp(&OID, OIDPTR)
88
89 @@
86 -expression E1, E2;
90 +struct object_id OID1, OID2;
91 @@
88 -- hashcpy(E1.hash, E2.hash)
89 -+ oidcpy(&E1, &E2)
92 +- hashcpy(OID1.hash, OID2.hash)
93 ++ oidcpy(&OID1, &OID2)
94
95 @@
96 identifier f != oidcpy;
93 -expression E1, E2;
97 +struct object_id *OIDPTR1;
98 +struct object_id *OIDPTR2;
99 @@
100 f(...) {<...
96 -- hashcpy(E1->hash, E2->hash)
97 -+ oidcpy(E1, E2)
101 +- hashcpy(OIDPTR1->hash, OIDPTR2->hash)
102 ++ oidcpy(OIDPTR1, OIDPTR2)
103 ...>}
104
105 @@
101 -expression E1, E2;
106 +struct object_id *OIDPTR;
107 +struct object_id OID;
108 @@
103 -- hashcpy(E1->hash, E2.hash)
104 -+ oidcpy(E1, &E2)
109 +- hashcpy(OIDPTR->hash, OID.hash)
110 ++ oidcpy(OIDPTR, &OID)
111
112 @@
107 -expression E1, E2;
113 +struct object_id *OIDPTR;
114 +struct object_id OID;
115 @@
109 -- hashcpy(E1.hash, E2->hash)
110 -+ oidcpy(&E1, E2)
116 +- hashcpy(OID.hash, OIDPTR->hash)
117 ++ oidcpy(&OID, OIDPTR)
118
119 @@
113 -expression E1, E2;
120 +struct object_id *OIDPTR1;
121 +struct object_id *OIDPTR2;
122 @@
115 -- oidcmp(E1, E2) == 0
116 -+ oideq(E1, E2)
123 +- oidcmp(OIDPTR1, OIDPTR2) == 0
124 ++ oideq(OIDPTR1, OIDPTR2)
125
126 @@
127 identifier f != hasheq;
@@ -125,10 +133,11 @@ expression E1, E2;
133 ...>}
134
135 @@
128 -expression E1, E2;
136 +struct object_id *OIDPTR1;
137 +struct object_id *OIDPTR2;
138 @@
130 -- oidcmp(E1, E2) != 0
131 -+ !oideq(E1, E2)
139 +- oidcmp(OIDPTR1, OIDPTR2) != 0
140 ++ !oideq(OIDPTR1, OIDPTR2)
141
142 @@
143 identifier f != hasheq;