-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.py
executable file
·202 lines (161 loc) · 7.02 KB
/
parse.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
#!/usr/bin/env python3
import sys
import logging
import pdb
import time
from itertools import chain
from pprint import pprint, pformat
from collections import defaultdict
from optparse import OptionParser
import rules
import scrapers
import ast_helpers
from call_tree import build_call_tree, merge_call_trees
from type_augmentor import augment_types
from assignment_checker import check_assignments
from overrides_checker import check_overrides
from rule_checker import check_rules
logging.basicConfig( filename="dbg_output", filemode="w", level=logging.DEBUG )
def scrape_all_files( files, ext_types, options ):
call_subtrees = []
override_subtrees = []
cursor_subsets = []
func_type_subsets = []
funcptr_type_subsets = []
assignment_subsets = []
tu_time = 0.0
start_time = time.time()
for fname in files:
overrideScraper = scrapers.Overrides()
cursorScraper = scrapers.FunctionCursors()
funcTypeScraper = scrapers.FunctionQualifiers()
funPtrTypeScraper = scrapers.FunctionPointers()
assScraper = scrapers.FunPtrAssignments()
pre_tu_time = time.time()
target = ast_helpers.get_translation_unit( fname, options )
logging.info( "Translation unit: " + str( target.spelling ) )
#ast_helpers.dump_ast( target.cursor, lambda x: logging.debug(x) )
tu_time += time.time() - pre_tu_time
scrapers.run_scrapers( target.cursor,
[ overrideScraper, cursorScraper,
funcTypeScraper, funPtrTypeScraper,
assScraper ] )
call_subtrees.append(
build_call_tree( target ) )
override_subtrees.append(
overrideScraper.get() )
cursor_subsets.append(
cursorScraper.get() )
func_type_subsets.append(
funcTypeScraper.get() )
funcptr_type_subsets.append(
funPtrTypeScraper.get() )
assignment_subsets.append(
assScraper.get() )
call_tree = merge_call_trees(
call_subtrees )
overrides = scrapers.Overrides.merge(
override_subtrees )
cursors = scrapers.FunctionCursors.merge(
cursor_subsets )
func_types = scrapers.FunctionQualifiers.merge(
func_type_subsets + [ ext_types ] )
funcptr_types = scrapers.FunctionPointers.merge(
funcptr_type_subsets )
assignments = scrapers.FunPtrAssignments.merge(
assignment_subsets )
call_tree.augment_with_overrides( overrides )
pre_augment_time = time.time()
aug_func_types = augment_types( call_tree, funcptr_types, func_types )
post_augment_time = time.time()
standard_funcs = set( [ key for key in func_types.keys() ] )
all_func_types = scrapers.merge_disjoint_dicts(
[ aug_func_types, funcptr_types ] )
end_time = time.time()
if options.show_time:
print( "time to parse: {:0.5f} seconds".format( tu_time ) )
print( "time to scrape stuff: {:0.5f} seconds".format(
end_time - start_time - tu_time ) )
print( "time to infer indirect type: {:0.5f} seconds".format(
post_augment_time - pre_augment_time ) )
return ( call_tree, all_func_types, cursors, assignments, standard_funcs,
overrides )
def get_violations( files, tagsfile, options ):
ext_types, type_rules = rules.parse_rules_file( tagsfile )
pre_file_parse = time.time()
( call_tree,
all_func_types,
cursors,
assignments,
standard_funcs,
overrides ) = scrape_all_files( files, ext_types, options )
post_file_parse = time.time()
rule_violations = check_rules(
call_tree, all_func_types, type_rules, standard_funcs )
post_rule_checking = time.time()
assignment_violations = check_assignments(
assignments, all_func_types )
post_assignment_checking = time.time()
override_violations = check_overrides(
overrides, all_func_types )
post_overrides_checking = time.time()
if options.show_time:
print( "Time to parse files: {:0.3f} Seconds".format(
post_file_parse - pre_file_parse ) )
print( "Time to check graph rules: {:0.5f} Seconds".format(
post_rule_checking - post_file_parse ) )
print( "Time to check assignments: {:0.5f} Seconds".format(
post_assignment_checking - post_rule_checking ) )
print( "Time to check overrides: {:0.5f} Seconds".format(
post_overrides_checking - post_assignment_checking ) )
print( "Functions examined (includes libraries and ptrs): {}".format(
len( all_func_types ) ) )
print( "Calls examined: {}".format( call_tree.size() ) )
return ( cursors, all_func_types,
chain( assignment_violations,
rule_violations,
override_violations ) )
def parse_args():
parser = OptionParser()
parser.add_option( "-t", "--tags-file", dest="tags_file",
help="Name of file containing extra function qualifiers",
metavar="FILE", default=None )
parser.add_option( "-x", "--language", dest="language",
help=( "Treat input files as having this language. "
+ "Equivalent to clang's -x. Try c++ or c" ),
metavar="LANGUAGE", default="c++" )
parser.add_option( "-s", "--std", dest="standard",
help=( "Specity the language standard to compile for. "
+ "Equivalent to clan's -std=. "
+ "Try c++17 or c89" ),
metavar="LANGUAGE", default="c++17" )
parser.add_option( "-I", "--include", dest="include_path",
help=( "Directory to be added to include search path. "
+ "For multiple directories, comma separate." ),
metavar="FILE", default=None )
parser.add_option( "-f", dest="clang_flags",
help=( "any flags to be passed directly to clang "
+ "(for multiple flags, enclose in quotes)"),
metavar="FLAGS", default=None )
parser.add_option( "-v", action="store_true", dest="verbose",
help="Verbose mode makes more dbg output",
default=False )
parser.add_option( "--time", action="store_true", dest="show_time",
help="Output execution time for different phases of prgm",
default=False )
( options, files ) = parser.parse_args()
if len( sys.argv ) == 1:
parser.print_help( sys.stderr )
sys.exit( 0 )
return ( options, files )
def main():
options, files = parse_args()
cursors, types, violations = get_violations(
files, options.tags_file, options )
for violation in violations:
print(
violation.render_string(
cursors, types ) )
print()
if __name__ == '__main__':
main()