-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathASTAnalyser.py
More file actions
650 lines (534 loc) · 20.8 KB
/
Copy pathASTAnalyser.py
File metadata and controls
650 lines (534 loc) · 20.8 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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import re
from ASTUtils import *
from collections import defaultdict
class ASTAnalyser(ast.NodeVisitor):
"""
df_graph: Data-FLow graph for the source file
scope: Current scope
parent_node: List to keep track of parents for the current node
obj_list: List of the live objects, mapped with their scopes
ignore_list: List of objects to be ignored while considering attribute calls (function arguments)
func_list: Dict of functions in the source file and their respective arguments
imports: Dict of Libraries and their modules imported with info on the alias used in the source file
"""
def __init__(self, func_list):
self.df_graph = DFGraph()
self.scope = ""
self.parent_node = ""
self.obj_list = defaultdict(dict)
self.ignore_list=defaultdict(list)
self.func_list = func_list
self.imports = dict()
#might require defaultdict(list)
self.add_node_to_graph(DummyNode())
"""ClassDef(identifier name, expr* bases, stmt* body, expr* decorator_list)"""
def visit_ClassDef(self,node):
if DEBUG:
print "visit_ClassDef"
count=str(self.df_graph.count)
scope='_'.join(['class',node.name, count])
self.ignore_list[scope]=\
self.ignore_list[self.scope][:]
fn_parent=''
cls_parent=''
node_num=self.parent_node
if node.body:
for stmt in node.body:
self.scope=scope
if isinstance(stmt,ast.FunctionDef):
if not fn_parent:
fn_parent=self.parent_node
self.parent_node=fn_parent
self.visit(stmt)
elif isinstance(stmt,ast.ClassDef):
if not cls_parent:
cls_parent=self.parent_node
self.parent_node=cls_parent
self.visit(stmt)
else:
self.parent_node=node_num
self.visit(stmt)
node_num=self.parent_node
self.parent_node=node_num
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
"""Module(stmt* body)"""
def visit_Module(self, node):
if DEBUG:
print "visit_Module"
scope = "module"
self.ignore_list[scope]=\
self.ignore_list[self.scope][:]
fn_parent=''
cls_parent=''
node_num=self.parent_node
if node.body:
for stmt in node.body:
self.scope=scope
if isinstance(stmt,ast.FunctionDef):
if not fn_parent:
fn_parent=self.parent_node
self.parent_node=fn_parent
self.visit(stmt)
elif isinstance(stmt,ast.ClassDef):
if not cls_parent:
cls_parent=self.parent_node
self.parent_node=cls_parent
self.visit(stmt)
else:
self.parent_node=node_num
self.visit(stmt)
node_num=self.parent_node
self.parent_node=node_num
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
"""FunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list)"""
def visit_FunctionDef(self, node):
if DEBUG:
print "visit_FunctionDef"
count=str(self.df_graph.count)
scope = '_'.join(['function', node.name, count])
self.ignore_list[scope]=\
self.ignore_list[self.scope][:]
"""Ignoring Function args"""
if node.args.args:
for arg in node.args.args:
arg_val='.'.join(get_node_value(arg))
if arg_val!='self':
self.ignore_list[scope].append(arg_val)
fn_parent=''
cls_parent=''
node_num=self.parent_node
if node.body:
for stmt in node.body:
self.scope=scope
if isinstance(stmt,ast.FunctionDef):
if not fn_parent:
fn_parent=self.parent_node
self.parent_node=fn_parent
self.visit(stmt)
elif isinstance(stmt,ast.ClassDef):
if not cls_parent:
cls_parent=self.parent_node
self.parent_node=cls_parent
self.visit(stmt)
else:
self.parent_node=node_num
self.visit(stmt)
node_num=self.parent_node
self.parent_node=node_num
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
"""
ImportFrom(identifier? module, alias* names, int? level)
(might have to change implementation)
"""
def visit_ImportFrom(self, node):
if DEBUG:
print "visit_ImportFrom"
lib = node.module
for name in node.names:
if name.asname is not None:
alias = name.asname
if lib is None:
pass
elif alias not in self.imports.keys():
self.imports[alias] = [lib + '.' + name.name]
else:
if '.'.join([lib, name.name]) not in self.imports[alias]:
self.imports[alias].append('.'.join([lib, name.name]))
else:
module = name.name
if lib is None:
pass
elif '*' == module:
self.add_lib_objects(lib)
elif module not in self.imports.keys():
self.imports[module] = [lib + '.' + module]
else:
if '.'.join([lib, module]) not in self.imports[module]:
self.imports[module].append('.'.join([lib, module]))
"""Import(alias* names)"""
def visit_Import(self, node):
if DEBUG:
print "visit_Import"
"""Alias names are stored in the imports dict"""
if node.names:
for name in node.names:
if name.asname is not None:
self.imports[name.asname] = [name.name]
"""Assign(expr* targets, expr value)"""
def visit_Assign(self, node):
if DEBUG:
print "visit_Assign"
self.generic_visit(node)
obj_list = [obj for values in self.obj_list.values() for obj in values.keys()]
live_objects={}
if node.targets:
for scope in self.obj_list.keys():
live_objects.update(self.obj_list[scope])
for target in node.targets:
ignoreAssignment=False
"""Ignore Subscript-ed Assignments"""
if isinstance(target, ast.Subscript):
ignoreAssignment = True
tgt = [target]
if isinstance(target, ast.Tuple):
tgt = target.elts
t_value = []
for t in tgt:
t_value.append(".".join(get_node_value(t)))
target = ','.join(t_value)
if not ignoreAssignment:
ignore_list=self.ignore_list[self.scope]
if target in ignore_list:
self.ignore_list[self.scope].remove(target)
rhs_val = get_node_value(node.value, live_objects)
if isinstance(node.value, ast.Call) or \
isinstance(node.value, ast.Name) or \
isinstance(node.value, ast.Attribute):
if rhs_val:
fn_name = ".".join(rhs_val)
if not self.is_function_in_src(fn_name):
srclist = self.get_source_list(rhs_val)
self.add_node_to_graph(
AssignmentNode(srclist, target,
node.lineno, node.col_offset,
add_context(target,
node.value,
live_objects)))
self.obj_list[self.scope][target]=srclist
else:
if target in obj_list:
self.kill_obj_after_reassignment(target)
"""Attribute(expr value, identifier attr, expr_context ctx)"""
def visit_Attribute(self, node):
if DEBUG:
print "visit_Attribute"
self.generic_visit(node)
obj_list = [obj for values in self.obj_list.values() for obj in values.keys()]
ignore_list = self.ignore_list[self.scope]
attr_name=".".join(
get_node_value(node.value))
if attr_name in obj_list and attr_name not in ignore_list:
self.add_node_to_graph(
CallNode(attr_name,
node.attr,
node.lineno, node.col_offset))
def visit_Subscript(self, node):
"""dummy function to prevent visiting the nodes if subscripts are present"""
"""For(expr target, expr iter, stmt* body, stmt* orelse)"""
def visit_For(self, node):
if DEBUG:
print "visit_For"
self.visit(node.iter)
scope = "_".join(['for']+
self.parent_node)
parent_scope=self.scope
self.obj_list[scope] = {}
"""Target may contain tuples"""
if get_node_value(node.target):
targets=get_node_value(node.target)[0].split(",")
for tgt in targets:
self.ignore_list[scope].append(tgt)
parent=self.parent_node
else_node=parent
for_node=parent
if node.body:
self.ignore_list[scope]=\
self.ignore_list[parent_scope][:]
for stmt in node.body:
self.scope=scope
self.visit(stmt)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
for_node=self.parent_node
if node.orelse:
scope="_".join(['for-else']+
self.parent_node)
self.parent_node=parent
self.ignore_list[scope]=\
self.ignore_list[parent_scope][:]
for stmt in node.orelse:
self.scope=scope
self.visit(stmt)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
else_node=self.parent_node
parent=set(for_node+else_node)
self.parent_node=list(parent)
"""While(expr test, stmt* body, stmt* orelse)"""
def visit_While(self, node):
if DEBUG:
print "visit_While"
self.visit(node.test)
parent=self.parent_node
else_node=self.parent_node
parent_scope=self.scope
if node.body:
scope = "_".join(['while']+
self.parent_node)
self.ignore_list[scope]=\
self.ignore_list[parent_scope][:]
for stmt in node.body:
self.scope=scope
self.visit(stmt)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
while_node=self.parent_node
if node.orelse:
self.parent_node=parent
scope= "_".join(['while-else']+
self.parent_node)
self.ignore_list[scope]=\
self.ignore_list[parent_scope][:]
for stmt in node.orelse:
self.scope=scope
self.visit(stmt)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
else_node=self.parent_node
parent=set(while_node+else_node)
self.parent_node=list(parent)
"""With(expr context_expr, expr? optional_vars, stmt* body)"""
def visit_With(self, node):
if DEBUG:
print "visit_With"
with_expr = [".".join(get_node_value(node.context_expr))]
scope = "_".join(['with']+ with_expr)
self.ignore_list[scope]=\
self.ignore_list[self.scope][:]
self.scope = scope
if isinstance(node.context_expr, ast.Call):
target = ".".join(get_node_value(node.optional_vars))
if len(target) != 0:
self.add_node_to_graph(
AssignmentNode(with_expr,target,
node.lineno, node.col_offset))
self.obj_list[self.scope][target]=with_expr
if node.body:
for stmt in node.body:
self.scope=scope
self.visit(stmt)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
"""If(expr test, stmt* body, stmt* orelse)"""
def visit_If(self, node):
if DEBUG:
print "visit_If"
self.visit(node.test)
parent = self.parent_node
else_node=self.parent_node
parent_scope=self.scope
if node.body:
scope = '_'.join(['if']+parent)
self.ignore_list[scope]=\
self.ignore_list[parent_scope][:]
for obj in node.body:
self.scope = scope
self.visit(obj)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
if_node=self.parent_node
if node.orelse:
self.parent_node=parent
scope = '_'.join(['else']+
self.parent_node)
self.ignore_list[scope]=\
self.ignore_list[parent_scope][:]
for obj in node.orelse:
self.scope = scope
self.visit(obj)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
else_node=self.parent_node
parent=set(if_node+else_node)
self.parent_node=list(parent)
"""IfExp(expr test, expr body, expr orelse)"""
def visit_IfExp(self, node):
if DEBUG:
print "visit_IfExp"
self.visit(node.test)
parent = self.parent_node
else_node=self.parent_node
parent_scope=self.scope
scope = '_'.join(['ifexp']+parent)
self.ignore_list[scope]=\
self.ignore_list[parent_scope][:]
self.scope = scope
self.visit(node.body)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
if_node=self.parent_node
scope = '_'.join(['else']+parent)
self.ignore_list[scope]=\
self.ignore_list[parent_scope][:]
self.scope = scope
self.visit(node.orelse)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
else_node=self.parent_node
parent=set(if_node+else_node)
self.parent_node=list(parent)
"""TryExcept(stmt* body, excepthandler* handlers, stmt* orelse)"""
def visit_TryExcept(self, node):
if DEBUG:
print "visit_TryExcept"
parent = self.parent_node
except_node = parent
else_node = parent
parent_scope=self.scope
if node.body:
scope = '_'.join(['try']+parent)
self.ignore_list[scope]=\
self.ignore_list[parent_scope][:]
for obj in node.body:
self.scope = scope
self.visit(obj)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
try_node=self.parent_node
if node.handlers:
""" the scoping is handled in ExceptHandler node """
for obj in node.handlers:
self.parent_node = parent
self.visit(obj)
except_node=self.parent_node
if node.orelse:
scope = '_'.join(['try-else']+ parent)
self.parent_node = parent
self.ignore_list[scope]=\
self.ignore_list[parent_scope][:]
for stmt in node.orelse:
self.scope = scope
self.visit(stmt)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
else_node=self.parent_node
parent=set(try_node+except_node+else_node)
self.parent_node=list(parent)
"""TryFinally(stmt* body, stmt* finalbody)"""
def visit_TryFinally(self, node):
if DEBUG:
print "visit_TryFinally"
parent = self.parent_node
finally_node = parent
parent_scope=self.scope
if node.body:
scope = '_'.join(['try']+parent)
self.ignore_list[scope]=\
self.ignore_list[parent_scope][:]
for obj in node.body:
self.scope = scope
self.visit(obj)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
try_node=self.parent_node
if node.finalbody:
scope = '_'.join(['try-finally']+ parent)
self.ignore_list[scope]=\
self.ignore_list[parent_scope][:]
self.parent_node = parent
for stmt in node.finalbody:
self.scope = scope
self.visit(stmt)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
finally_node=self.parent_node
parent=set(try_node+finally_node)
self.parent_node=list(parent)
"""ExceptHandler(expr? type, expr? name, stmt* body)"""
def visit_ExceptHandler(self, node):
if DEBUG:
print "visit_ExceptHandler"
scope = '_'.join(['except']+
self.parent_node)
self.ignore_list[scope]=\
self.ignore_list[self.scope][:]
for obj in node.body:
self.scope = scope
self.visit(obj)
self.clear_obj_list(scope)
self.clear_ignore_list(scope)
def is_function_in_src(self, function_name):
if DEBUG:
print "is_function_in_src"
if function_name in self.func_list.keys():
return True
return False
def add_node_to_graph(self, node):
if DEBUG:
print "add_node_to_graph"
node.parent=self.parent_node
self.parent_node=\
[self.df_graph.add_node(node)]
def add_lib_objects(self, lib_name):
try:
lib = __import__(lib_name)
pattern = re.compile('__\\w+__')
for member in dir(lib):
if pattern.match(member) is None:
if member not in self.imports.keys():
self.imports[member] = [lib_name + '.' + member]
else:
self.imports[member].append(lib_name + '.' + member)
except:
pass
def get_source_list(self, source_fn_list, suffix="", result=None):
if DEBUG:
print "get_source_list"
if result is None:
result = []
if len(source_fn_list) == 0:
if len(result) == 0:
return [suffix[1:]]
return result
elif ".".join(source_fn_list) in self.imports.keys():
key = ".".join(source_fn_list)
for value in self.imports[key]:
result.append(value + suffix)
return result
else:
return self.get_source_list(source_fn_list[:-1],
"." + source_fn_list[-1]
+ suffix, result)
"""
Deletes the objects in a given scope only
if they aren't alive in a parent scope
"""
def clear_obj_list(self, scope):
if DEBUG:
print "in clear_obj_list"
print "scope:", scope
print "obj_list",self.obj_list
obj_list=self.obj_list[scope].keys()
live_obj_list=[]
for key in self.obj_list.keys():
if key!=scope:
live_obj_list.extend(
self.obj_list[key].keys())
for obj in obj_list:
if obj not in live_obj_list:
self.add_node_to_graph(
DeadNode(obj))
self.obj_list[scope].pop(obj)
self.obj_list.pop(scope)
def clear_ignore_list(self, scope):
if DEBUG:
print "in clear_ignore_list"
if scope in self.ignore_list.keys():
del self.ignore_list[scope]
"""
Kills an object if it is in current scope
Ignores the object in current scope otherwise
"""
def kill_obj_after_reassignment(self, target):
if DEBUG:
print "in kill_obj_after_reassignment"
object_list = self.obj_list[self.scope]
if target in object_list.keys():
self.obj_list[self.scope].pop(target)
self.add_node_to_graph(DeadNode(target))
else:
if target not in self.ignore_list[self.scope]:
self.ignore_list[self.scope].append(target)