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