-
Notifications
You must be signed in to change notification settings - Fork 0
/
bc.py
262 lines (249 loc) · 5.2 KB
/
bc.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from clang.cindex import *
from tagdb import TagDb
import ply.lex as lex
import lexer
from ref_visitor import RefVisitor
import sys
html_header = '''
<!DOCTYPE html>
<html>
<head>
<title>Beauty Cpp</title>
<script type="text/javascript" src="/js/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="/css/main.css">
</head>
<body class="question-page">
'''
html_footer = '''
</body>
'''
db = TagDb()
def wrapper_file_name(f):
return "data/"+f.replace('/', '_').replace('.', '_')+".html"
def wrapper_def(f, lineno):
defines = db.get_defs(f, lineno)
if len(defines) == 0:
return lineno
define = defines[0]
html = '<span id="%s">%d</span>'%(define[0], lineno)
for define in defines[1:]:
html += '<span id="%s"></span>'%(define[0])
return html
def wrapper_ref(f, offset, tag):
ref = db.get_ref(f, offset, tag)
if ref is not None:
html_file = wrapper_file_name(ref[1])
html = '<a href="../%s#%s">%s</a>'%(html_file, ref[0], tag)
else:
html = tag
return html
def wrapper_include(f, inc_tag):
inc = db.get_include(f, inc_tag.lexpos)
if inc is None:
if inc_tag.inc_type == '<':
return '#include <%s>'%(inc_tag.inc)
else:
return '#include "%s"'%(inc_tag.inc)
html_file = wrapper_file_name(inc[1])
if inc_tag.inc_type == '<':
html = '#include <<a href="../%s">%s</a>>'%(html_file, inc_tag.inc)
else:
html = '#include "<a href="../%s">%s</a>"'%(html_file, inc_tag.inc)
return html
def parse(f, code):
l = lex.lex(module=lexer)
l.input(code)
html = html_header + '<table><tr><td>%s</td><td>'%wrapper_def(f, 1)
while True:
t = l.token()
if t is None:
break
elif t.type == '<':
html += '<'
elif t.type == '>':
html += '>'
elif t.type == 'NEWLINE':
html += '</td></tr>\n<tr><td class="bc_lineno">%s</td><td>'%wrapper_def(f,t.lineno+1)
elif t.type == 'INCLUDE':
html += wrapper_include(f, t)
elif t.type == 'ID':
html += wrapper_ref(f, t.lexpos, t.value)
elif hasattr(t, 'html'):
if type(t.html) is list:
comment_line = t.lineno
for h in t.html:
html += h
comment_line+=1
html += '</td></tr><tr><td>%d</td><td>'%comment_line
else:
html += t.html
else:
html += t.value
html += '</td></tr>' + html_footer
return html
'''
The source files
Each one will be processed as a clang translation unit
srcs = [
'assoc.c',
'cache.c',
'daemon.c',
'hash.c',
'items.c',
'memcached.c',
'sasl_defs.c',
'sizes.c',
'slabs.c',
'solaris_priv.c',
'stats.c',
'testapp.c',
'thread.c',
'timedrun.c',
'util.c'
]
'''
'''
The arguments
the first 4 args are the system include path in my mac
you should get your real path with the command: "cpp -v"
args = [
'-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/include',
'-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include',
'-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include',
'-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks',
'-DHAVE_CONFIG_H',
'-I.',
'-DNDEBUG',
'-I/usr/local/include'
]
'''
srcs = [
'global.c',
'ctime.c',
'status.c',
'date.c',
'os.c',
'fault.c',
'mem0.c',
'mem1.c',
'mem2.c',
'mem3.c',
'mem5.c',
'mutex.c',
'mutex_noop.c',
'mutex_unix.c',
'mutex_w32.c',
'malloc.c',
'printf.c',
'random.c',
'utf.c',
'util.c',
'hash.c',
'opcodes.c',
'os_unix.c',
'os_win.c',
'bitvec.c',
'pcache.c',
'pcache1.c',
'rowset.c',
'pager.c',
'wal.c',
'btmutex.c',
'btree.c',
'backup.c',
'vdbemem.c',
'vdbeaux.c',
'vdbeapi.c',
'vdbetrace.c',
'vdbe.c',
'vdbeblob.c',
'vdbesort.c',
'journal.c',
'memjournal.c',
'walker.c',
'resolve.c',
'expr.c',
'alter.c',
'analyze.c',
'attach.c',
'auth.c',
'build.c',
'callback.c',
'delete.c',
'func.c',
'fkey.c',
'insert.c',
'legacy.c',
'loadext.c',
'pragma.c',
'prepare.c',
'select.c',
'table.c',
'trigger.c',
'update.c',
'vacuum.c',
'vtab.c',
'where.c',
'parse.c',
'tokenize.c',
'complete.c',
'main.c',
'notify.c',
'fts3.c',
'fts3_aux.c',
'fts3_expr.c',
'fts3_hash.c',
'fts3_porter.c',
'fts3_tokenizer.c',
'fts3_tokenizer1.c',
'fts3_tokenize_vtab.c',
'fts3_write.c',
'fts3_snippet.c',
'fts3_unicode.c',
'fts3_unicode2.c',
'rtree.c',
'icu.c',
'fts3_icu.c'
]
args = [
'-DSQLITE_OS_UNIX=1',
'-I.',
'-I./src',
'-I./ext/rtree',
'-D_HAVE_SQLITE_CONFIG_H',
'-DBUILD_sqlite',
'-DNDEBUG',
'-DSQLITE_THREADSAFE=1',
'-DSQLITE_OMIT_LOAD_EXTENSION=1',
'-DSQLITE_TEMP_STORE=1'
]
def main():
reload(sys)
sys.setdefaultencoding('utf-8')
'''
global args
for f in srcs:
index = Index.create()
print f
tu = index.parse(f, args)
if not tu:
print "cannot parse: %s"%f
db = TagDb()
v = RefVisitor(db)
v.run(tu)
'''
for f in db.files():
if f == 'None':
continue
print f
fd = open(f, 'r')
code = fd.read()
html = parse(f, code)
html_file = wrapper_file_name(f)
wfd = open(html_file, 'w')
wfd.write(html)
wfd.close()
if __name__ == '__main__':
main()