-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrapers.py
executable file
·321 lines (252 loc) · 9.02 KB
/
scrapers.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
#!/usr/bin/env python3
import sys
from clang.cindex import CursorKind
from ast_helpers import get_translation_unit, is_function_pointer
from collections import defaultdict
class AnnotationKind:
DIRECT = 1
INDIRECT = 2
def parse_annotation( annotation ):
"""
Parse an annotation. If it starts with a known prefix, return
a tuple of (type, annotation). Otherwise, return None
"""
annotation = annotation.split( "::" )
if len( annotation ) != 2:
return None
if annotation[ 0 ] == 'funqual':
return ( AnnotationKind.DIRECT, annotation[ 1 ] )
if annotation[ 0 ] == 'funqual_indirect':
return ( AnnotationKind.INDIRECT, annotation[ 1 ] )
return None
def get_qualifiers( node ):
"""
Given a node, return the set of all its explicit qualifiers
"""
qualifiers = set()
for child in node.get_children():
if child.kind == CursorKind.ANNOTATE_ATTR:
if parse_annotation( child.displayname ):
qualifiers.add( parse_annotation( child.displayname ) )
return qualifiers
def merge_disjoint_dicts( dicts ):
"""
Merge a list of dictionaries where none of them share any keys
"""
result = {}
for mapping in dicts:
for key, value in mapping.items():
if key in result:
raise Exception(
"key `{}` defined in two dictionaries".format(
key ) )
result[ key ] = value
return result
def merge_overlapping_dicts( dicts ):
"""
Merge a list of dictionaries where some may share keys but where
the value must be the same
"""
result = {}
for mapping in dicts:
for key, value in mapping.items():
if key in result and result[ key ] != value:
raise Exception(
"Key `{}` maps to both `{}` and `{}`".format(
key, value, result[ key ] ) )
result[ key ] = value
return result
def sloppy_merge_dicts( dicts ):
"""
Merge a list of dictionaries where some values may be overridden
"""
result = {}
for mapping in dicts:
for key, value in mapping.items():
result[ key ] = value
return result
def run_scrapers( tu, scrapers ):
for trav in tu.walk_preorder():
for scraper in scrapers:
scraper.scrape( trav )
class FunctionPointers:
def __init__( self ):
self.funptrs = {}
def scrape( self, trav ):
"""
Given a node in a tu, scrape a mapping of function pointers to their
qualified types
"""
if trav.kind == CursorKind.VAR_DECL:
self.funptrs[ trav.get_usr() ] = get_qualifiers( trav )
def get( self ):
return self.funptrs
merge = merge_overlapping_dicts
class FunctionQualifiers():
"""
Contains helper methods for scraping the function qualifiers out of
a set of translation units
"""
def __init__( self ):
self.func_tags = defaultdict( lambda: set() )
def scrape( self, trav ):
"""
Given a node in a tu, scrape a mapping of function/methods to their
qualified types (direct type only)
"""
if trav.kind in [ CursorKind.FUNCTION_DECL, CursorKind.CXX_METHOD ]:
full_name = trav.get_usr()
qualifiers = get_qualifiers( trav )
self.func_tags[ full_name ] |= qualifiers
def get( self ):
return dict( self.func_tags )
@staticmethod
def merge( mappings ):
func_tags = defaultdict( lambda: set() )
for mapping in mappings:
for symbol, qualifiers in mapping.items():
func_tags[ symbol ] |= qualifiers
return dict( func_tags )
class FunctionCursors:
"""
Contains helper methods for scraping the function cursors out of a
set of translation units
"""
def __init__( self ):
self.func_cursors = {}
def scrape( self, trav ):
"""
Given a node in a tu, scrape a mapping of function usr to the
cannonical cursor (we need to be able to retrieve the cursor later
for error reporting)
"""
if trav.kind in [ CursorKind.FUNCTION_DECL, CursorKind.CXX_METHOD ]:
full_name = trav.get_usr()
cursor = trav.canonical
self.func_cursors[ full_name ] = cursor
if ( trav.kind in [ CursorKind.VAR_DECL ]
and is_function_pointer( trav ) ):
full_name = trav.get_usr()
cursor = trav.canonical
self.func_cursors[ full_name ] = cursor
def get( self ):
return self.func_cursors
merge = sloppy_merge_dicts
class Overrides:
"""
Contains helper methods for scrapping function overrides out of a
translation unit and merging together override mappings from
multiple translation units.
"""
def __init__( self ):
self.overrides = defaultdict( lambda: set() )
def scrape( self, trav ):
"""
Scrape a translation unit and generate a mapping of methods to the
methods that override them
"""
if trav.kind == CursorKind.CXX_METHOD:
to_visit = list( trav.get_overridden_cursors() )
while to_visit:
overridden = to_visit.pop()
self.overrides[ overridden.get_usr() ].add( trav.get_usr() )
to_visit += list( overridden.get_overridden_cursors() )
def get( self ):
return self.overrides
@staticmethod
def merge( override_maps ):
"""
Merge two override maps
"""
result = defaultdict( lambda: set() )
for override_map in override_maps:
for key, val in override_map.items():
result[ key ] |= val
return result
class FunPtrAssignments:
"""
Scrapes the assignments of function pointers
"""
@classmethod
def get_operator( cls, binop_node ):
"""
Cheap hack to get the binary operator out of a clang expression
because clang doesn't expose the binop interface to the python
api
"""
return [ x.spelling for x in binop_node.get_tokens() ][1]
@classmethod
def get_lvalue( cls, binop_node ):
"Cheap hack to get lvalue from binop expression"
return [ x for x in binop_node.get_children() ][ 0 ].referenced
@classmethod
def get_rvalue( cls, binop_node ):
"Cheap hack to get rvalue from binop expression"
return [ x for x in binop_node.get_children() ][ 1 ].referenced
@classmethod
def is_lvalue_funptr( cls, binop_node ):
"Check whether the lvalue of this operation is a function pointer"
if '(*)' in cls.get_lvalue( binop_node ).type.spelling:
return True
else:
return False
def __init__( self ):
self.results = []
def scrape( self, trav ):
"""
Scrape a single translation unit for all assignments into function
pointers. Grab usr so they can be typechecked.
Return list of tuples in the following format:
( lvalue usr, rvalue usr, cursor of assignment for error reporting )
"""
if ( trav.kind == CursorKind.BINARY_OPERATOR and
len( list( trav.get_children() ) ) == 2 ):
try:
if ( self.get_operator( trav ) == '=' and
self.is_lvalue_funptr( trav ) ):
self.results.append(
( self.get_lvalue( trav ).get_usr(),
self.get_rvalue( trav ).get_usr(),
trav.canonical ) )
except:
pass
def get( self ):
return self.results
@classmethod
def merge( cls, lists_of_assignments ):
"""
Takes the result of scraping several translation units
and merges the results into one list
"""
results = []
for list_of_assignments in lists_of_assignments:
for assignment in list_of_assignments:
results.append( assignment )
return results
if __name__ == '__main__':
from pprint import pprint
if len( sys.argv ) == 2:
target = get_translation_unit( sys.argv[ 1 ] )
do_all = True
elif len( sys.argv ) == 3:
target = get_translation_unit( sys.argv[ 2 ] )
do_all = False
else:
print( "Usage: {} [ptr|qual|override|assignment] file.cpp".format(
sys.argv[ 0 ] ) )
if sys.argv[ 1 ] == 'ptr' or do_all:
print( "Function pointers:" )
pprint( FunctionPointers.scrape( target ) )
print()
if sys.argv[ 1 ] == 'qual' or do_all:
print( "Functions and Methods:" )
pprint( FunctionQualifiers.scrape( target ) )
print()
if sys.argv[ 1 ] == 'override' or do_all:
print( "Overrides:" )
pprint( dict( Overrides.scrape( target ) ) )
print()
if sys.argv[ 1 ] == 'assignment' or do_all:
print( "Assignments:" )
pprint( FunPtrAssignments.scrape( target ) )
print()