-
Notifications
You must be signed in to change notification settings - Fork 56
/
inception.py
196 lines (167 loc) · 5.37 KB
/
inception.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
"""
Invoke as follows:
python inception.py path/to/project/sources [libclang.so full path]
"""
from clang.cindex import Index, Cursor, CursorKind, Config
import os, sys, re, fnmatch
def align_doc(doc):
processed_lines = []
for line in doc.split('\n'):
stripped = line.strip()
aligned = '{}{}'.format(' '*(0 if stripped.startswith('/') else 5), stripped)
processed_lines.append(aligned)
return '\n'.join(processed_lines)
def parse_cursor(c):
element = {}
element['file'] = c.location.file
element['line'] = c.location.line
element['spelling'] = c.spelling
element['kind'] = '{}'.format(c.kind)
element['cursor'] = c
return element;
def generate_symbols(elements):
with open('symbols.hpp', mode='w+') as file:
file.write("""
/*
* Generated from sources
* Symbolic introspection the lazy way
*/
#ifndef CTTI_SYMBOLS_HPP
#define CTTI_SYMBOLS_HPP
""")
for element in elements.values():
file.write("""
#ifndef CTTI_SYMBOLS_{0}_DEFINED
#define CTTI_SYMBOLS_{0}_DEFINED
CTTI_DEFINE_SYMBOL({0}); // from {1}:{2} ({3})
#endif // CTTI_SYMBOLS_{0}_DEFINED
""".format(element['spelling'], element['file'], element['line'], element['kind']));
file.write("""
#endif // CTTI_DEFINE_SYMBOLS_HPP""");
def parse(project_includedir, libclang_lib):
if libclang_lib != "":
Config.set_library_file(libclang_lib)
index = Index.create()
values = {};
processed_files = [];
abs_project_includedir = os.path.abspath(project_includedir)
valid_kinds = [
CursorKind.NAMESPACE,
CursorKind.CLASS_DECL,
CursorKind.STRUCT_DECL,
CursorKind.UNION_DECL,
CursorKind.ENUM_DECL,
CursorKind.FIELD_DECL,
CursorKind.TYPE_ALIAS_DECL,
CursorKind.NAMESPACE_ALIAS,
CursorKind.CLASS_TEMPLATE,
CursorKind.CXX_METHOD,
CursorKind.ENUM_CONSTANT_DECL
];
invalid_names = [
"",
"static_assert", # An static assert expression is parsed as a CXX METHOD, lol
"std",
"hash",
"get_member",
"value_type",
"member_type",
"MOCK_METHOD0",
"MOCK_METHOD1",
"MOCK_METHOD2",
"MOCK_METHOD3",
"MOCK_METHOD4",
"MOCK_METHOD5",
"MOCK_METHOD6",
"MOCK_METHOD7",
"MOCK_METHOD8",
"MOCK_METHOD9",
"MOCK_METHOD10",
"MOCK_CONST_METHOD0",
"MOCK_CONST_METHOD1",
"MOCK_CONST_METHOD2",
"MOCK_CONST_METHOD3",
"MOCK_CONST_METHOD4",
"MOCK_CONST_METHOD5",
"MOCK_CONST_METHOD6",
"MOCK_CONST_METHOD7",
"MOCK_CONST_METHOD8",
"MOCK_CONST_METHOD9",
"MOCK_CONST_METHOD10",
"operator()",
"operator+",
"operator++",
"operator+=",
"operator-",
"operator--",
"operator-=",
"operator<",
"operator<=",
"operator<<",
"operator<<=",
"operator>",
"operator>=",
"operator>>",
"operator>>=",
"operator=",
"operator*=",
"operator->",
"operator*",
"operator==",
"operator!=",
"operator~",
"operator^",
"operator^=",
"operator%",
"operator%=",
"operator|",
"operator|=",
"operator||",
"operator&",
"operator&=",
"operator&&",
"operator[]",
"operator/",
"operator/=",
"operator!",
"operator!="
];
print 'project include dir: ' + abs_project_includedir
def visitor(cursor):
if cursor.kind == CursorKind.TRANSLATION_UNIT:
abs_file = os.path.abspath(str(cursor.spelling))
else:
if cursor.location.file is not None:
abs_file = os.path.abspath(str(cursor.location.file))
else:
abs_file = ""
common_path = os.path.commonprefix([abs_file, abs_project_includedir])
if common_path == abs_project_includedir and cursor.spelling not in invalid_names and \
cursor.kind in valid_kinds and \
cursor.spelling not in values:
print '{}# {}:{}: {} (Kind={})'.format(len(values), cursor.location.file, cursor.location.line, cursor.spelling, cursor.kind)
values[cursor.spelling] = parse_cursor(cursor)
if common_path == abs_project_includedir and ((cursor.kind == CursorKind.TRANSLATION_UNIT and (not cursor.location.file in processed_files)) or \
cursor.kind != CursorKind.TRANSLATION_UNIT):
for c in cursor.get_children():
visitor(c)
for root, dirnames, filenames in os.walk(project_includedir):
for filename in \
fnmatch.filter(filenames, '*.pb.h') + \
fnmatch.filter(filenames, '*.pb.cc') + \
fnmatch.filter(filenames, '*.h') + \
fnmatch.filter(filenames, '*.hpp') + \
fnmatch.filter(filenames, '*.c') + \
fnmatch.filter(filenames, '*.cpp'):
file = os.path.join(root, filename)
tu = index.parse(file)
visitor(tu.cursor)
generate_symbols(values)
def main():
project_includedir = sys.argv[1]
libclang_lib = "";
if len(sys.argv) > 2:
libclang_lib = sys.argv[2]
parse(project_includedir, libclang_lib)
if __name__ == '__main__':
main()