-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast2gremlin.py
433 lines (344 loc) · 14.6 KB
/
ast2gremlin.py
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
'''
Created on Nov 21, 2015
@author: Tommi Unruh
'''
from _constants import *
import sys
class AST2Gremlin(object):
"""
Converts PHP ASTs to gremlin queries.
"""
def __init__(self, ast):
'''
Constructor
'''
self.cleanAST(ast)
self.ast = ast
self.vars = set()
self.node_count = 0
def removeNullNodes(self):
query = self.addComment("Remove \"NULL\"-nodes")
query += (
"\ng.V.filter{ it.type == \"NULL\" }"
".each{ g.removeVertex(it) }.iterate();\n\n"
)
return query
def convertPHPAST(self, debug=False):
# Reset counter that counts the number of nodes in a given AST.
self.node_count = 0
ast = self.ast
# Print out AST (for debugging purposes).
if debug:
print ast
query = self.addComment("Start query.") + "\n"
# Get all nodes.
query += "g.V"
# The root node is a AST_STMT_LIST, therefore we skip it and
# analyse its children.
first = True
# This is the first matched node, so save its linenumber
query += self.sideEffect("start_linenumber = it.lineno")
ast_children = ast.getChildren()
if ast_children[-1].getType() == TYPE_NULL:
# If the code ends in e.g. '?>', it will produce a NULL node.
# However if we search for the given code snippet in real code,
# the '?>' will not be part of the code.
# Therefore, we need to ignore it while producing the gremlin
# query.
itr = self.ignoreLastElement(ast_children)
else:
itr = ast_children
itr = self.ignoreLastElement(itr)
i = 0
for child in itr:
# All children except the last
query += self.addComment("Parent #%d" % i)
query += self.convertNode(child, first=first)
query += self.setupNextNode()
first = False
i += 1
try:
# Handle last child here
child = (
ast_children[-2] if ast_children[-1].getType() == TYPE_NULL
else ast_children[-1]
)
query += self.addComment("Parent #%d" % (i))
query += self.convertNode(child)
# Filter out path to last node here,
# because we need to traverse it.
# i_begin = query.rfind("\n")
# last_filter_line = query[i_begin:]
# child_traversal = self.extractChildFromFilter(last_filter_line)
# query += self.addComment(
# "Traverse to last children to get its linenumber."
# )
# query += child_traversal
except:
# Iterator was empty
pass
sys.exit()
query = self.prepareQuery() + query
query = self.prepareEndOfQuery(query)
return query
def cleanAST(self, ast):
for node in ast.iter_DFS():
# Remove nodes of type "null".
if node.getType() == "null":
# node.cutFromParent()
pass
def prepareQuery(self):
"""
Add generic content we need to the gremlin query.
"""
# # Add a function to print any found results later on.
# query = "def printFoundCC(line_start, line_end, file_name) {\n"
# query += (
# "\"Found a code clone on lines \" + line_start + \" to \""
# " + line_end + \"\\n\" + \"File: \" + file_name"
# )
# query += "\n}\n\n"
# Add global variable to track the childnumbers of parsed nodes.
query = "childnumber = 0"
# Initialize each variable that has been seen in the query.
query += self.addComment("Initialize variables of query")
for var in self.vars:
query += "\n_%s = \"\"" % var
return query
def extractChildFromFilter(self, _str):
"""
In a string of format ".filter{ isType(it.....next(), "whatever") }"
filter out the 'it....' part.
"""
string_start = len(".filter{ isType(it") # Remove this part from string.
i_end = _str.find(".next()")
if i_end != -1:
# Found ".next()"
child_traversal = _str[string_start:i_end]
else:
# Did not find ".next()", because the string looks like this:
# ".filter{ isType(it, "whatever") }".
i_end = _str.find("it")
child_traversal = _str[string_start, i_end+2]
return child_traversal
def prepareEndOfQuery(self, query):
# Remove last six from query, because they are
# generalized gremlin-steps, that are only needed to traverse to the
# next node. However, we are at the end of the query.
# i = query.rfind("\n")
# query = self.removeLastLines(query, 0)
#
# i = query[0:i].rfind("\n")
# query = query[0:i]
query = query + self.addComment("Prepare end of query.")
# Save linenumber of last node.
query += "\n" + self.sideEffect("end_linenumber = it.lineno")
# Save filename/
query += "\n.toFile()"
query += "\n.fileToPath()"
query += self.sideEffect("filename = it")[1:]
# Print result.
query += self.addComment("Print all found results.")
query += self.transform(
"printFoundCC(start_linenumber, end_linenumber, filename)"
)
return query
def convertNode_old(self, ast, first=None):
"""
Check the given AST for its type and build a gremlin query.
"""
query = ""
node_type = ast.getType()
node_children = ast.getChildren()
if first:
# This is the first matched node, so save its linenumber
query += self.sideEffect("start_linenumber = it.lineno")
query += self.addComment("Node: %s" % (node_type))
# Filter node.
query += self.filter(self.isType(node_type))
if node_type == TYPE_ASSIGN:
# $var = whatever
# AST_ASSIGN
# 0: AST_VAR
# 0: "string"
# 1: AST_DIM | AST_VAR | AST_ENCAPS_LIST | string | int
# Filter children.
# query += self.filterChildren(node_children)
# Remember the $var's name.
query += self.rememberVarName(node_children)
# Remember its position as a child.
query += self.sideEffect("childnumber = %s" % (
"it.childnum" if first else "childnumber + 1"
))
elif node_type == TYPE_ENCAPS_LIST:
# A string also containing variables ("example $list").
# Each child should have the correct type, therefore add
# filters for them.
pass
elif node_type == TYPE_ASSIGN_OP:
# $var .= whatever (similar to TYPE_ASSIGN)
# Filter left and right child.
query += self.filterChildren(node_children)
# Check if variable name is one of the already defined variables.
# If yes, then add a filter which checks for equality of
# their names.
varname = node_children[0].getChildren()[0].getValue()
if varname in self.vars:
# Var is already known. Check for equality.
query += self.filter(
"it.lval().varToName().next() == %s.next()" % (
varname
)
)
else:
# Remember variable name
query += self.rememberVarName(node_children)
elif node_type == TYPE_IF or node_type == TYPE_IF_ELEM:
# if (...) {...} else {...}
# AST_IF
# 0: AST_IF_ELEM
# 0: any node (if_condition)
# 1: AST_STMT_LIST (code of if-block)
# 1: AST_IF_ELEM
# same as child 0.
pass
elif node_type == TYPE_ISSET:
# Call to isset()
# AST_ISSET
# 0: whatever
pass
query += self.traverseChildren(node_children)
return query
def ignoreLastElement(self, itr):
"""
Iterate 'itr', but ignore its last element, so that we can treat
it differently.
"""
itr = iter(itr)
prev = itr.next()
for elem in itr:
# Yield the PREVIOUS item.
# This will lead to ignoring the last item in 'itr'.
# The last item will not hit the yield statement, since
# the for-loop would need to iterate 'itr' one time more than
# it has elements.
yield prev
prev = elem
def addSeenVariableFilter(self, traversal, var_name):
return self.filter("%s.varToName()%s == _%s" % (
traversal,
".next()" if traversal != "it" else "",
var_name
))
def convertNode(self, ast, first=None):
"""
Check the given AST for its type and build a gremlin query.
"""
query = ""
for traversal, node in self.recursiveFilter("it", ast):
node_type = node.getType()
if node_type == TYPE_SIMPLE_INT or node_type == TYPE_SIMPLE_STRING:
# Ignore string or int nodes to allow for simple edits of
# clones.
continue
result = "isType(%s, \"%s\")"
query += self.filter(result % (traversal, node_type))
# Check if node is of type AST_VAR and its parent
# is not of type AST_DIM.
if node_type == TYPE_VAR and node.getParent().getType() != TYPE_DIM:
# If yes, check if this variable has been seen before.
var_name = node.getChild(0).getValue()
if var_name in self.vars:
# Variable has been seen before
query += self.addSeenVariableFilter(traversal, var_name)
else:
# Variable occured the first time - remember it.
query += self.rememberVarName(var_name, traversal)
return query
def isType(self, _type):
"""
Add the custom gremlin step "isType(node, type)", which returns true
if a given node is of the given type.
"""
query = "isType(it, \"%s\")" % (_type)
return query
def recursiveFilter(self, query, node):
"""
Check the subtree of every node recursively and yield the traversal to
each node.
This approach does not actually traverse any child - the query stays
at the highest level in the AST and only checks if every child node
is of the right type.
"""
yield (query, node)
for i, child in enumerate(node.getChildren()):
# if child.getType() != "null":
next_query = query + ".ithChildren(%d)" % i
for resulting_query in self.recursiveFilter(next_query, child):
yield resulting_query
def traverseChildren(self, children_list):
"""
Legacy code - add the traversal for each child in children_list
to the query. After traversing the child, go back to its parent
(== go up one level in tree).
This code is obsolete, because actually traversing each child in
a even small ASTs will lead to stack overflows in the neo4j graph
database.
The new approach is in 'recursiveFilter'.
"""
query = ""
for i, child in enumerate(children_list):
query += self.goToChild(i)
query += self.convertNode(child)
query += self.goToParent()
return query
def goToParent(self):
query = "\n\n// Go up one level."
query += "\n.parents()"
return query
def goToChild(self, i):
query = "\n\n// Traverse child #%d" % i
query += "\n.ithChildren(%d)" % i
return query
def rememberVarName(self, var_name, traversal):
self.vars.add(var_name)
return (
self.addComment("Remember variable %s" % (var_name)) +
self.sideEffect("_%s = %s.varToName().next()" % (
var_name, traversal
))
)
def setupNextNode(self):
return (
self.sideEffect("childnumber = it.childnum") +
self.sideEffect("childnumber = childnumber + 1") +
self.goToParent() + "\n.children()" +
self.addComment("Check next AST-node (similar to next line of code)") +
self.filter("it.childnum == childnumber")
)
def filter(self, _filter, no_end=False):
query = "\n.filter{ %s }" % (_filter)
if no_end:
return query[:-2]
return query
def filterChildren(self, children_list):
query = self.addComment("Filter node children.")
for i, child in enumerate(children_list):
query += self.filter("it.ithChildren(%d).type.next() == \"%s\"" % (
i, child.getType()
))
return query
def sideEffect(self, sideeffect):
return "\n.sideEffect{ %s }" % (sideeffect)
def transform(self, transform):
return "\n.transform{ %s }" % (transform)
def addComment(self, comment):
return "\n\n// " + comment
def removeLastLines(self, _str, lines=1):
i = _str.rfind("\n")
_str = _str[0:i]
if lines > 1:
for _ in xrange(lines-1):
i = _str[0:i].rfind("\n")
_str = _str[0:i]
return _str