-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclang-hash-global
executable file
·174 lines (130 loc) · 5.86 KB
/
clang-hash-global
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
#!/usr/bin/env python
import fnmatch
import hashlib
import os
import sys
from chashutil import static_vars
# TODO: is this ok? or better prefix every call?
from chashutil import get_param_of, get_list_of_info_files, get_record_from, get_name_of, get_prefix_of, read_info_files
from chashutil import FUNCTION_PREFIX, VARIABLE_PREFIX, INFO_EXTENSION, OUTPUT_FLAG, HELP_FLAG
DEFINITION_FLAG = '--definition'
OBJECT_FILE_FLAG = '--object-file'
LOCAL_FLAG = '--local' # print the symbol's local hash
ALL_FLAG = '--all'
def usage():
# TODO: add detailed description
print "%s %s <symbol> [directory] [%s <outfile>]" % (sys.argv[0], DEFINITION_FLAG, OUTPUT_FLAG)
print "%s %s <file> [directory] [%s <outfile>]" % (sys.argv[0], OBJECT_FILE_FLAG, OUTPUT_FLAG)
print "%s %s <symbol> [directory] [%s <outfile>]" % (sys.argv[0], LOCAL_FLAG, OUTPUT_FLAG)
print "%s %s [directory] [%s <outfile>]" % (sys.argv[0], ALL_FLAG, OUTPUT_FLAG)
@static_vars(currently_processed=set())
def get_global_hash(symbol, global_hashes, local_hashes, used_definitions, visited=None):
"""Calculates the global hash of the symbol recursively over all used definitions
"""
currently_processed = get_global_hash.currently_processed # to break recursion
if visited is not None:
visited.add(symbol)
# If a symbol does not have a local hash, we assume it is a library
# function and therefore does not require a local hash.
if symbol not in local_hashes:
return None
if symbol in global_hashes:
return global_hashes[symbol]
if symbol in currently_processed: # TODO: ok? prevents recursion
return local_hashes[symbol]
currently_processed.add(symbol)
global_hash = hashlib.md5()
global_hash.update(local_hashes[symbol])
if symbol in used_definitions:
used_defs = used_definitions[symbol]
# sys.stderr.write(repr([symbol, used_defs])+"\n")
for used_def in used_defs:
#used_def = get_name_of(used_def)
used_def_global_hash = get_global_hash(used_def, global_hashes, local_hashes, used_definitions,visited)
if used_def_global_hash is not None:
global_hash.update(used_def_global_hash)
currently_processed.remove(symbol)
global_hashes[symbol] = global_hash.hexdigest()
return global_hashes[symbol]
def print_checked_symbol_hashes(symbols_to_check, global_hashes):
print "{"
for k, v in global_hashes.items():
if k in symbols_to_check:
print "'{}':'{}',".format(k, v)
print "}"
def write_checked_symbol_hashes(symbols_to_check, global_hashes, fn):
with open(fn, 'w') as o_file:
o_file.write("{")
for k, v in global_hashes.items():
if k in symbols_to_check:
o_file.write("'{}':'{}',\n".format(k, v))
o_file.write("}")
def get_symbols_to_check(filename):
record = get_record_from(filename)
symbols_to_check = []
for elem in record['element-hashes']:
prefix = get_prefix_of(elem[0])
symbol = get_name_of(elem[0])
if prefix == FUNCTION_PREFIX or prefix == VARIABLE_PREFIX:
symbols_to_check.append(symbol)
return symbols_to_check
################################################################################
def run():
args = sys.argv
argc = len(args)
if argc > 2:
if argc > 3 or ALL_FLAG in args: # 3rd param is path
working_directory = args[2 if ALL_FLAG in args else 3]
os.chdir(working_directory)
working_directory = os.getcwd()
# fill local_hashes
local_hashes, used_definitions = read_info_files(working_directory)
symbols_to_check = []
if DEFINITION_FLAG in args:
"""Recursive starting from a single function or global variable definition"""
symbol = get_param_of(DEFINITION_FLAG)
symbols_to_check.append(symbol)
elif OBJECT_FILE_FLAG in args:
"""Recursive, starting from all definitions within an object file"""
object_filename = get_param_of(OBJECT_FILE_FLAG)
info_filename = object_filename + INFO_EXTENSION
symbols_to_check = get_symbols_to_check(info_filename)
elif LOCAL_FLAG in args:
symbol = get_param_of(LOCAL_FLAG)
possibilities = ['function:' + symbol, 'variable:'+symbol,
'record:'+symbol]
for real in possibilities:
if real in local_hashes:
print local_hashes[real]
sys.exit(0)
sys.stderr.write("%s not found\n" % symbol)
sys.exit(1)
elif ALL_FLAG in args:
symbols_to_check = local_hashes.keys()
# map function -> global hash TODO: could be moved to get_global_hash?
global_hashes = {}
for symbol in symbols_to_check:
possibilities = ['function:' + symbol, 'variable:'+symbol, None]
for real_sym in possibilities:
if real_sym in local_hashes:
break
if not real_sym:
print "Error: symbol '%s' not found" % symbol
sys.exit(1)
visited = set()
x = get_global_hash(real_sym, global_hashes, local_hashes, used_definitions,visited)
global_hashes[symbol] = x
out_filename = None
if OUTPUT_FLAG in args:
out_filename = get_param_of(OUTPUT_FLAG)
if out_filename is None:
if DEFINITION_FLAG in args:
print global_hashes[symbol] # if in definition mode, only print that definition's global hash
else:
print_checked_symbol_hashes(symbols_to_check, global_hashes)
else:
write_checked_symbol_hashes(symbols_to_check, global_hashes, out_filename)
elif argc == 2 and HELP_FLAG in args:
usage()
if __name__ == "__main__":
run()