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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
#!/usr/bin/env txr
@(mdo
(defvar *tags-lib*)
(let ((*tags-lib* t))
(load "txrtags"))
(define-option-struct libtags-opts tags-opts
(v verbose :bool "Print diagnostic messages during processing."))
(defvarl output)
(defvarl emacs)
(defvarl verbose)
(defvarl ix-tags (hash :equal-based))
(defvarl var-syms (hash :equal-based))
(defvarl fun-vars (hash :equal-based))
(defun update-ix-tags (tag newkey : oldkey)
(when oldkey
(del [ix-tags oldkey]))
(upd [ix-tags newkey] (append (sys:var 1) (list tag))))
(defun qualify-sym (sym pkg)
(join (casequal pkg
("user_package" "")
("system_package" "sys:")
("keyword_package" ":")
(t (when verbose
(put-line `@sym: in unknown package @pkg` *stderr*))
pkg))
sym))
(defun op-error-fun-p (fun)
(mequal fun
"op_error" "op_meta_error"
"op_qquote_error" "op_unquote_error")))
@(do
(let ((o (new libtags-opts)))
o.(getopts *args*)
(set output (cond (o.output o.output)
(o.emacs "TAGS")
(t "tags")))
(set emacs o.emacs)
(set verbose o.verbose)))
@(bind var_s #/\w[\w\d]*_s/)
@(bind cident #/\w[\w\d]*/)
@(bind regfun #/reg_(op|mac|fun)/)
@(bind regvar #/reg_(varl?|symacro)|ffi_typedef/)
@(bind lpar "(")
@(bind rpar ")")
@
@(define get-interned-sym (sym))@\
@ (local lit pkg)intern(lit("@lit"), @{pkg cident})@\
@ (bind sym @(qualify-sym lit pkg))@\
@(end)
@
@(define get-fun (fun))@\
@ func_@/[\w\d]+/(@{fun cident}@(maybe), @/\d+/@(end))@\
@(end)
@
@(define get-sym-fun (fun))@\
@ (local fun-var)@\
@ (cases)func_@/[\w\d]+/(if3(opt_compat && opt_compat <= @/\d+/, @\
@ cident, @{fun cident})@(maybe), @/\d+/@(end))@\
@
@ (or)@(get-fun fun)@\
@ (or)@{fun-var cident}@\
@ (do (when verbose
(unless (or (starts-with "op_" fun-var)
[fun-vars fun-var])
(put-line `@{fun-var}: undefined function variable` *stderr*))))@\
@ (bind fun @(or [fun-vars fun-var] fun-var))@\
@ (end)@\
@(end)
@
@(define get-sym-fun (fun))
@ (cases)
@ / +/@(get-sym-fun fun)@rpar;
@ (or)
@ / +/func_@/[\w\d]+/(if3(opt_compat && opt_compat <= @/\d+/,
@ (cases)
@
@ / +/@cident, @{fun cident})@(maybe), @/\d+/@(end))@rpar;
@ (or)
@
@ / +/@cident,
@ / +/@{fun cident}@rpar@(maybe), @/\d+/@(end)@rpar@rpar;
@ (end)
@ (or)
@
@ / +/func_@/[\w\d]+/((opt_compat && opt_compat <= @/\d+/) ?
@ / +/@cident : @{fun cident}@(maybe), @/\d+/@(end))@rpar;
@ (end)
@(end)
@(define get-file-ix-tags (file))
@ (next file)
@ (collect)
@ (local sym var fun pkg)
@ (all)
@ line
@ (and)
@ (cases)
@ / +/@(maybe)val @(end)@{var cident} = @(get-fun fun);
@ (do (set [fun-vars var] fun))
@ (or)
@ / +/@(maybe)val @(end)@{var var_s} = @(get-interned-sym sym);
@ (do (if [var-syms var]
(when verbose
(put-line `@var: reassigned variable` *stderr*))
(progn
(iflet ((tags [ix-tags var]))
(progn
(each ((tag tags))
(let ((old-ident tag.ident))
(set tag.ident sym)
(typecase tag
(fun-tag (update-ix-tags tag old-ident))
(var-tag (update-ix-tags tag sym))
(t
(put-line `@(struct-type-name tag): unexpected struct type`
*stderr*)))))
(del [ix-tags var]))
(set [ix-tags var] (list (new tag
ident sym
path file
line line))))
(set [var-syms var] sym))))
@ (or)
@ (cases)
@ / +/@regfun(@{var var_s}, @(get-sym-fun fun));
@ (or)
@ / +/@regfun(@{var var_s},
@ / +/@(get-sym-fun fun));
@ (end)
@ (do
(unless (op-error-fun-p fun)
(iflet ((sym [var-syms var])
(tag (new fun-tag
ident (or sym fun)
path file
line line))
((have sym)))
(update-ix-tags tag fun var)
(update-ix-tags tag var))))
@ (or)
@ (cases)
@ / +/@regfun(@(get-interned-sym sym), @(get-sym-fun fun));
@ (bind var nil)
@ (or)
@ / +/@regfun@lpar@(get-interned-sym sym),
@ (get-sym-fun fun)
@ (bind var nil)
@ (or)
@
@ / +/@regfun(@{var var_s} = @(get-interned-sym sym),
@ / +/@(get-sym-fun fun));
@ (or)
@ / +/@regfun(@{var var_s} = intern(lit("@lit"),
@ / +/@{pkg cident}), @(get-sym-fun fun));
@ (bind sym @(qualify-sym lit pkg))
@ (end)
@ (do (when var
(if [var-syms var]
(when verbose
(put-line `@var: reassigned variable` *stderr*))
(set [var-syms var] sym)))
(update-ix-tags (new fun-tag
ident sym
path file
line line)
fun var))
@ (or)
@ (cases)
@ / +/@regvar(@{var var_s}, @(skip));
@ (or)
@
@
@ / +/ffi_typedef@lpar@{var var_s}, @(skip)
@ (end)
@ (do (iflet ((sym [var-syms var])
(tag (new var-tag
ident (or sym var)
path file
line line))
((have sym)))
(update-ix-tags tag
sym var)
(update-ix-tags tag var)))
@ (or)
@ (cases)
@ / +/@regvar(@(get-interned-sym sym), @(skip));
@ (bind var nil)
@ (or)
@ / +/@regvar@lpar@(get-interned-sym sym),
@ (bind var nil)
@ (or)
@ / +/@regvar(@{var var_s} = @(get-interned-sym sym), @(skip));
@ (or)
@ / +/@regvar@lpar@{var var_s} = @(get-interned-sym sym),
@ (or)
@ / +/@regvar(@{var var_s} = intern(lit("@lit"),
@ / +/@{pkg cident}), @(skip));
@ (bind sym @(qualify-sym lit pkg))
@ (or)
@ / +/@regvar@lpar@{var var_s} = intern(lit("@lit"),
@ / +/@{pkg cident}),
@ (bind sym @(qualify-sym lit pkg))
@ (end)
@ (do (when var
(if [var-syms var]
(when verbose
(put-line `@var: reassigned variable` *stderr*))
(set [var-syms var] sym)))
(update-ix-tags (new var-tag
ident sym
path file
line line)
sym var))
@ (end)
@ (end)
@ (end)
@(end)
@
@
@
@
@
@
@(next :list (cons "lib.c" (remqual "lib.c"
(command-get-lines "git ls-files '*.c'"))))
@(repeat)
@ file.c
@ (get-file-ix-tags `@file.c`)
@(end)
@(do
(when nil
(dohash (ident tag ix-tags)
(when (mequal tag.ident
"flip" "slot-p")
(del [ix-tags ident]))))
(when verbose
(let ((alist (keep-if (op ends-with "_s" (first @1))
(hash-pairs ix-tags)))
(undefined nil)
(missing nil))
(each ((pair alist))
(tree-bind (key tags) pair
(each ((tag tags))
(typecase tag
(fun-tag (push (list key (join "(" tag.ident ")")) undefined))
(var-tag (push (list key) undefined))
(tag (push (list key (join "(" tag.ident ")")) missing))
(t (put-line `@(struct-type-name tag): unexpected struct type`
*stderr*))))))
(upd undefined (nsort @1 : car))
(upd missing (nsort @1 : car))
(mapdo (op put-line `@(cat-str @1 " "): undefined variable`
*stderr*)
undefined)
(mapdo (op put-line `@(cat-str @1 " "): no corresponding function or value`
*stderr*)
missing)
(put-line `@(len undefined) undefined variables` *stderr*)
(put-line `@(len missing) missing corresponding functions` *stderr*)
(let ((vals (flow ix-tags hash-values flatten (nsort @1 : .ident))))
(put-line `@(len vals) ix-tags` *stderr*))))
(defun merge-ix-tags (orig-tags)
(let ((tags orig-tags)
(orig-tags (group-by (op identity @1.ident) orig-tags))
(ix-tags (hash-pairs ix-tags)))
(each ((pair ix-tags))
(tree-bind (ident ix-tags) pair
(condlet
((((ends-with "_s" ident)))
(upd tags (revappend ix-tags)))
(((orig-tags-orig orig-tags)
(orig-tags (keep-if (andf (op mequal
(short-suffix @1.path)
".c" ".l" ".y")
(op find #\( @1.line))
[orig-tags ident])))
(when (and verbose (> (len orig-tags) 1))
(put-line `@ident: multiple occurrences in tags file` *stderr*))
(tree-bind (: var-tags other-tags) (separate (op equal @1.type "v")
ix-tags)
(upd tags (revappend var-tags))
(each-prod ((orig-tag orig-tags)
(ix-tag other-tags))
(unless (equal orig-tag.ident ix-tag.ident)
(let ((tag (copy-struct orig-tag)))
(set tag.ident ix-tag.ident)
(push tag tags))))))
(t
(when verbose
(whenlet ((orig-tags (keep-if (op mequal
(short-suffix @1.path)
".c" ".l" ".y")
[orig-tags-orig ident])))
(each-prod ((orig-tag orig-tags)
(ix-tag ix-tags))
(put-line `@ident | orig @{orig-tag.ident} @{orig-tag.path} | ix @{ix-tag.ident} @{ix-tag.path}`))))
(upd tags (revappend ix-tags))))))
(sort tags : .ident)))
(defun write-tagfile (ix-tags)
(let* ((orig-tags (read-tagfile output))
(ix-tags (merge-ix-tags orig-tags)))
(with-stream (stream (open-file output "w"))
(each ((tag ix-tags))
(put-line tag.(text) stream)))))
(defun write-etagfile (ix-tags)
(let ((orig-tags (read-etagfile output)))
(upd orig-tags (nsort @1 : car))
(file-put "tags.out" orig-tags)
(with-stream (stream (open-file output "w"))
(each ((pair orig-tags))
(tree-bind (path . etags) pair
(let ((str (with-out-string-stream (s)
(each ((etag etags))
(unless (ends-with "_s" etag.ident)
(each ((ix-tag [keep-if (op find etag.ident @1 : .ident)
ix-tags .ctags]))
(put-line `duping @ix-tag`)
))
(put-line etag.(etext) s)))))
(put-string `@{etag-sec-start}\n@{path},@(len str)\n@{str}`
stream)))))))
(if emacs
(write-etagfile ix-tags)
(write-tagfile ix-tags)))
|