]> git.proxmox.com Git - mirror_frr.git/blame - tools/vty_index.cocci
Merge pull request #3454 from rodnymolina/isis_openfabric_enhacements
[mirror_frr.git] / tools / vty_index.cocci
CommitLineData
52c6b0e2
DL
1/*
2 * prep: strip off casts, they cause things to fail matching later.
3 */
4
5@@
6identifier casttarget;
7symbol vty;
8@@
9
10- (struct casttarget *)vty->index
11+ vty->index
12
13/*
14 * variant 1: local variable assigned from vty->index
15 */
16
17@@
18identifier sn, nn;
19identifier fn;
20@@
21
22 int fn(...)
23 {
24+ VTY_DECLVAR_CONTEXT (sn, nn);
25 ...
26 \(
27- struct sn *nn;
28 ...
29- nn = vty->index;
30 \|
31- struct sn *nn = vty->index;
32 \|
33- struct sn *nn = vty->index;
34 ...
35- nn = vty->index;
36 \)
37 ...
38 }
39
40@@
41identifier sn, nn;
42identifier fn;
43type Tr;
44@@
45
46 Tr *fn(...)
47 {
48+ struct sn *nn = VTY_GET_CONTEXT(sn);
49 ...
50 \(
51- struct sn *nn;
52 ...
53- nn = vty->index;
54+ if (!nn) {
55+ return NULL;
56+ }
57 \|
58- struct sn *nn = vty->index;
59+ if (!nn) {
60+ return NULL;
61+ }
62 \|
63- struct sn *nn = vty->index;
64 ...
65- nn = vty->index;
66+ if (!nn) {
67+ return NULL;
68+ }
69 \)
70 ...
71 }
72
73/*
74 * variant 2: vty wrapper func with (vty, vty->index, ...) signature
75 */
76
77/* find calls of this pattern first; arg will be dropped in rule3 */
78@rule1@
79identifier fn !~ "generic_(set|match)_";
80expression arg;
81@@
82
83 fn(arg, arg->index, ...)
84
85@ script:python @
86fn << rule1.fn;
87arg << rule1.arg;
88@@
89print "R01 removing vty-index argument on %s(%s, ...)" % (fn, arg)
90
91#/* strip arg on the vty wrapper func, add local handling */
92@ rule2 @
93identifier rule1.fn;
94identifier arg;
95identifier T;
96@@
97
98 static int fn (struct vty *vty,
99- struct T * arg,
100 ...)
101 {
102+ VTY_DECLVAR_CONTEXT (T, arg);
103 ...
104 }
105
106/* drop argument on call sites identified earlier */
107@ rule3 @
108identifier rule1.fn;
109expression arg;
110@@
111
112 fn(arg,
113- arg->index,
114 ...)
115
116
117/*
118 * variant 3: function calls with "vty->index" argument (but no vty)
119 *
120 * a bit more complicated since we need to find the type from the header.
121 */
122
123/* find call sites first
124 * remember function name for later declvar insertion
125 */
126@ rule11 exists@
127identifier fn;
128identifier fparent;
129type Tr;
130@@
131
132 Tr fparent (...)
133 {
134 ...
135 fn(vty->index, ...)
136 ...
137 }
138
139@ script:python @
140fn << rule11.fn;
141@@
142print "R11 removing vty-index argument on %s(...)" % (fn)
143
1a8c43f1 144#/* find type of the argument - note args are mostly unnamed in FRR :( */
52c6b0e2
DL
145@ rule12 @
146identifier rule11.fn;
147identifier T, argname;
148type Tr;
149@@
150
151(
152 Tr fn(struct T *, ...);
153|
154 Tr fn(struct T * argname, ...);
155)
156
157@ script:python @
158fn << rule11.fn;
159T << rule12.T;
160@@
161print "R12 removing vty-index type is %s for %s(...)" % (T, fn)
162
163#/* add declvar
164# * this is split from rule14 so we support multiple calls in one func */
165@ rule13a @
166identifier rule11.fparent;
167identifier rule12.T;
168@@
169
170 int fparent (...)
171 {
172+ VTY_DECLVAR_CONTEXT(T, T);
173 ...
174 }
175
176@ rule13b @
177identifier rule11.fparent;
178identifier rule12.T;
179type Tr;
180@@
181
182 Tr *fparent (...)
183 {
184+ struct T *T = VTY_GET_CONTEXT(T);
185+ if (!T) {
186+ return NULL;
187+ }
188 ...
189 }
190
191/* now replace the argument in the call */
192@ rule14 exists @
193identifier rule11.fn;
194identifier rule12.T;
195@@
196
197 {
198 ...
199 \(
200 fn(
201- vty->index,
202+ T,
203 ...)
204 \|
205 fn(
206- vty->index
207+ T
208 )
209 \)
210 ...
211 }
212
213/* special case ... */
214@rule30@
215identifier fn =~ "generic_(set|match)_";
216expression arg;
217@@
218
219 fn(arg,
220- arg->index,
221+ VTY_GET_CONTEXT(route_map_index),
222 ...)
223
224/* and finally - PUSH_CONTEXT */
225@ rule99a exists @
226identifier tnode;
227identifier vexpr =~ "NULL";
228@@
229
230- vty->node = tnode;
231 ...
232- vty->index = vexpr;
233+ VTY_PUSH_CONTEXT_NULL(tnode);
234
235@ rule99b exists @
236identifier tnode;
237expression vexpr;
238@@
239
240- vty->node = tnode;
241 ...
242- vty->index = vexpr;
243+ VTY_PUSH_CONTEXT(tnode, vexpr);
244