-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathThrift.qll
More file actions
320 lines (233 loc) · 6.64 KB
/
Thrift.qll
File metadata and controls
320 lines (233 loc) · 6.64 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* Provides classes for working with Apache Thrift IDL files.
* This code is under development and may change without warning.
*/
import external.ExternalArtifact
/** An item in the parse tree of the IDL file */
class ThriftElement extends ExternalData {
string kind;
ThriftElement() {
this.getDataPath() = "thrift-" + kind
}
string getKind() {
result = kind
}
string getId() {
result = getField(0)
}
int getIndex() {
result = getFieldAsInt(1)
}
ThriftElement getParent() {
result.getId() = this.getField(2)
}
string getValue() {
result = this.getField(3)
}
ThriftElement getChild(int n) {
result.getIndex() = n and result.getParent() = this
}
ThriftElement getAChild() {
result = this.getChild(_)
}
override string toString() {
result = this.getKind()
}
string getPath() {
result = this.getField(4)
}
private int line() {
result = this.getFieldAsInt(5)
}
private int column() {
result = this.getFieldAsInt(6)
}
predicate hasLocationInfo(string fp, int bl, int bc, int el, int ec) {
fp = this.getPath() and
bl = this.line() and
bc = this.column() and
el = this.line() and
ec = this.column() + this.getValue().length()-1
or
exists(ThriftElement first, ThriftElement last |
first = this.getChild(min(int l | exists(this.getChild(l)))) and
last = this.getChild(max(int l | exists(this.getChild(l)))) and
first.hasLocationInfo(fp, bl, bc, _, _) and
last.hasLocationInfo(fp, _, _, el, ec)
)
}
File getFile() {
this.hasLocationInfo(result.getAbsolutePath(), _, _, _, _)
}
}
abstract class ThriftNamedElement extends ThriftElement {
abstract ThriftElement getNameElement();
final string getName() {
result = this.getNameElement().getValue()
}
override string toString() {
result = this.getKind() + " " + this.getName()
or
not exists(this.getName()) and result = this.getKind() + " ???"
}
override predicate hasLocationInfo(string fp, int bl, int bc, int el, int ec) {
exists(ThriftElement first |
first = this.getChild(min(int l | exists(this.getChild(l)))) and
first.hasLocationInfo(fp, bl, bc, _, _) and
this.getNameElement().hasLocationInfo(fp, _, _, el, ec)
)
}
}
class ThriftType extends ThriftNamedElement {
ThriftType() {
kind.matches("%type")
}
override ThriftElement getNameElement() {
result = this.getChild(0)
or
result = this.getChild(0).(ThriftType).getNameElement()
}
override string toString() {
result = "type " + this.getName()
}
predicate references(ThriftStruct struct) {
this.getName() = struct.getName() and
exists(string path |
this.hasLocationInfo(path, _, _, _, _) and
struct.hasLocationInfo(path, _, _, _, _)
)
}
}
/** A thrift typedef */
class ThriftTypeDef extends ThriftNamedElement {
ThriftTypeDef() {
kind.matches("typedef")
}
override ThriftElement getNameElement() {
result = this.getChild(2).getChild(0)
}
}
/** A thrift enum declaration */
class ThriftEnum extends ThriftNamedElement {
ThriftEnum() {
kind.matches("enum")
}
override ThriftElement getNameElement() {
result = this.getChild(0).getChild(0)
}
}
/** A thrift enum field */
class ThriftEnumField extends ThriftNamedElement {
ThriftEnumField() {
kind.matches("enumfield")
}
override ThriftElement getNameElement() {
result = this.getChild(0).getChild(0)
}
}
/** A thrift service declaration */
class ThriftService extends ThriftNamedElement {
ThriftService() {
kind.matches("service")
}
override ThriftElement getNameElement() {
result = this.getChild(0).getChild(0)
}
ThriftFunction getAFunction() {
result = this.getChild(_)
}
ThriftFunction getFunction(string name) {
result.getName() = name and
result = this.getAFunction()
}
}
/** A thrift function declaration */
class ThriftFunction extends ThriftNamedElement {
ThriftFunction() {
kind.matches("function")
}
override ThriftElement getNameElement() {
result = this.getChild(2).getChild(0)
}
ThriftField getArgument(int n) {
result = this.getChild(n+3)
}
ThriftField getAnArgument() {
result = this.getArgument(_)
}
private ThriftThrows getAllThrows() {
result = this.getChild(_)
}
ThriftField getAThrows() {
result = this.getAllThrows().getAChild()
}
ThriftType getReturnType() {
result = this.getChild(1).getChild(0)
}
override predicate hasLocationInfo(string fp, int bl, int bc, int el, int ec) {
this.getChild(1).hasLocationInfo(fp, bl, bc, _, _) and
this.getChild(2).hasLocationInfo(fp, _, _, el, ec)
}
ThriftService getService() {
result.getAFunction() = this
}
string getQualifiedName() {
result = this.getService().getName() + "." + this.getName()
}
}
class ThriftField extends ThriftNamedElement {
ThriftField() {
kind.matches("field")
}
override ThriftElement getNameElement() {
result = this.getChild(4)
}
ThriftType getType() {
result = this.getChild(2)
}
}
class ThriftStruct extends ThriftNamedElement {
ThriftStruct() {
kind.matches("struct")
}
override ThriftElement getNameElement() {
result = this.getChild(0).getChild(0)
}
ThriftField getMember(int n) {
result = this.getChild(n+1)
}
ThriftField getAMember() {
result = this.getMember(_)
}
}
class ThriftException extends ThriftNamedElement {
ThriftException() {
kind.matches("exception")
}
override ThriftElement getNameElement() {
result = this.getChild(0).getChild(0)
}
ThriftField getMember(int n) {
result = this.getChild(n+1)
}
ThriftField getAMember() {
result = this.getMember(_)
}
}
class ThriftThrows extends ThriftElement {
ThriftThrows() {
kind.matches("throws")
}
ThriftField getAThrows() {
result = this.getChild(_)
}
}
/** A parse tree element that holds a primitive value */
class ThriftValue extends ThriftElement {
ThriftValue() {
exists(this.getValue())
}
override string toString() {
result = this.getKind() + " " + this.getValue()
}
}