~emersion/gyosu

ref: 872488ff784e0ef4d71258400c28624b4a7e8d1d gyosu/main.go -rw-r--r-- 15.9 KiB
872488ffSimon Ser Remove unnecessary shadowed variables a month ago
                                                                                
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package main

import (
	"fmt"
	"html/template"
	"io/fs"
	"log"
	"os"
	"path"
	"path/filepath"
	"sort"
	"strings"

	"git.sr.ht/~sircmpwn/getopt"
	"modernc.org/cc/v3"
)

func cutComment(s string) (string, string) {
	start := strings.Index(s, "/*")
	if start < 0 {
		return "", ""
	}

	j := strings.Index(s[start:], "*/")
	if j < 0 {
		return "", ""
	}
	end := start + j + len("*/")

	return s[start:end], s[end:]
}

func extractDocComment(tok cc.Token) []string {
	// Pick the last comment
	rest := tok.Sep.String()
	var comment string
	for {
		nextComment, nextRest := cutComment(rest)
		if nextComment == "" {
			break
		}
		comment, rest = nextComment, nextRest
	}

	if comment == "" || rest != "\n" {
		return nil
	}

	s := comment
	s = strings.TrimPrefix(strings.TrimPrefix(s, "/*"), "*")
	s = strings.TrimSuffix(s, "*/")
	s = strings.TrimSpace(s)

	lines := strings.Split(s, "\n")
	for i, l := range lines {
		l = strings.TrimPrefix(strings.TrimSpace(l), "* ")
		if l == "*" {
			l = ""
		}
		lines[i] = l
	}

	doc := strings.Join(lines, "\n")
	return strings.Split(doc, "\n\n")
}

func enumSpecifierPrototype(enumSpec *cc.EnumSpecifier) prototype {
	var proto prototype
	if enumSpec.Token2.Value != 0 {
		proto.Symbol(declEnum, enumSpec.Token2.Value.String())
	} else {
		proto.Keyword("enum")
	}
	if enumSpec.EnumeratorList != nil {
		proto.Raw(" {\n")
		for el := enumSpec.EnumeratorList; el != nil; el = el.EnumeratorList {
			proto.Raw("\t%v,\n", el.Enumerator.Token)
		}
		proto.Raw("}")
	}
	return proto
}

func structOrUnionSpecifierPrototype(structOrUnionSpec *cc.StructOrUnionSpecifier) prototype {
	kind := structOrUnionSpec.StructOrUnion.Token.Value.String()

	var proto prototype
	if structOrUnionSpec.Token.Value != 0 {
		proto.Symbol(declKind(kind), structOrUnionSpec.Token.Value.String())
	} else {
		proto.Keyword(kind)
	}

	if structOrUnionSpec.StructDeclarationList != nil {
		proto.Raw(" {\n")
		for sdl := structOrUnionSpec.StructDeclarationList; sdl != nil; sdl = sdl.StructDeclarationList {
			proto.Add(indent(structDeclarationPrototype(sdl.StructDeclaration))...).Raw(";\n")
		}
		proto.Raw("}")
	}

	return proto
}

func structDeclarationPrototype(structDecl *cc.StructDeclaration) prototype {
	var proto prototype
	for sql := structDecl.SpecifierQualifierList; sql != nil; sql = sql.SpecifierQualifierList {
		switch sql.Case {
		case cc.SpecifierQualifierListTypeSpec:
			proto.Add(typeSpecifierPrototype(sql.TypeSpecifier)...).Raw(nbsp)
		case cc.SpecifierQualifierListTypeQual:
			proto.Keyword(sql.TypeQualifier.Token.Value.String()).Raw(nbsp)
		default:
			panic(fmt.Sprintf("TODO: %v", sql.Case))
		}
	}
	first := true
	for sdl := structDecl.StructDeclaratorList; sdl != nil; sdl = sdl.StructDeclaratorList {
		if !first {
			proto.Raw(", ")
		}
		proto.Add(declaratorPrototype(sdl.StructDeclarator.Declarator)...)
		first = false
	}
	return proto
}

func declaratorPrototype(declarator *cc.Declarator) prototype {
	var proto prototype
	for ptr := declarator.Pointer; ptr != nil; ptr = ptr.Pointer {
		proto.Raw("*")
		// TODO: ptr.TypeQualifiers
	}
	proto.Add(directDeclaratorPrototype(declarator.DirectDeclarator)...)
	return proto
}

func directDeclaratorPrototype(directDeclarator *cc.DirectDeclarator) prototype {
	var proto prototype
	switch directDeclarator.Case {
	case cc.DirectDeclaratorIdent:
		return *proto.Raw(directDeclarator.Token.Value.String())
	case cc.DirectDeclaratorArr, cc.DirectDeclaratorStaticArr:
		// TODO: TypeQualifiers, AssignmentExpression
		return *proto.Add(directDeclaratorPrototype(directDeclarator.DirectDeclarator)...).Raw("[]")
	case cc.DirectDeclaratorFuncParam:
		ptl := directDeclarator.ParameterTypeList

		proto.Add(directDeclaratorPrototype(directDeclarator.DirectDeclarator)...).Raw("(" + zwsp)
		first := true
		for pl := ptl.ParameterList; pl != nil; pl = pl.ParameterList {
			// TODO: ParameterDeclaration.AttributeSpecifierList
			if !first {
				proto.Raw(", ")
			}
			pd := pl.ParameterDeclaration
			proto.Add(declarationSpecifiersPrototype(pd.DeclarationSpecifiers)...)
			switch pd.Case {
			case cc.ParameterDeclarationDecl:
				proto.Raw(nbsp).Add(declaratorPrototype(pd.Declarator)...)
			case cc.ParameterDeclarationAbstract:
				if pd.AbstractDeclarator != nil {
					panic("TODO: AbstractDeclarator")
				}
			default:
				panic(fmt.Sprintf("unsupported %v", pd.Case))
			}
			first = false
		}
		if ptl.Case == cc.ParameterTypeListVar {
			proto.Raw(", ...")
		}
		proto.Raw(")")
		return proto
	case cc.DirectDeclaratorDecl:
		// TODO: AttributeSpecifierList
		return *proto.Raw("(").Add(declaratorPrototype(directDeclarator.Declarator)...).Raw(")")
	default:
		panic(fmt.Sprintf("TODO: %v", directDeclarator.Case))
	}
}

func declarationSpecifiersPrototype(declSpec *cc.DeclarationSpecifiers) prototype {
	var proto prototype
	first := true
	for declSpec := declSpec; declSpec != nil; declSpec = declSpec.DeclarationSpecifiers {
		if !first {
			proto.Raw(nbsp)
		}
		switch declSpec.Case {
		case cc.DeclarationSpecifiersTypeSpec:
			proto.Add(typeSpecifierPrototype(declSpec.TypeSpecifier)...)
		case cc.DeclarationSpecifiersTypeQual:
			proto.Keyword(declSpec.TypeQualifier.Token.Value.String())
		default:
			panic(fmt.Sprintf("TODO: %v", declSpec.Case))
		}
		first = false
	}
	return proto
}

func typeSpecifierPrototype(typeSpec *cc.TypeSpecifier) prototype {
	var proto prototype
	switch typeSpec.Case {
	case cc.TypeSpecifierStructOrUnion:
		return structOrUnionSpecifierPrototype(typeSpec.StructOrUnionSpecifier)
	case cc.TypeSpecifierEnum:
		return enumSpecifierPrototype(typeSpec.EnumSpecifier)
	case cc.TypeSpecifierAtomic:
		panic("TODO")
	case cc.TypeSpecifierBool:
		return *proto.Keyword("bool")
	default:
		return *proto.Keyword(typeSpec.Token.Value.String())
	}
}

func typeSpecifierFirstToken(typeSpec *cc.TypeSpecifier) cc.Token {
	switch typeSpec.Case {
	case cc.TypeSpecifierStructOrUnion:
		return typeSpec.StructOrUnionSpecifier.StructOrUnion.Token
	case cc.TypeSpecifierEnum:
		return typeSpec.EnumSpecifier.Token
	case cc.TypeSpecifierAtomic:
		return typeSpec.AtomicTypeSpecifier.Token
	default:
		return typeSpec.Token
	}
}

func indent(in prototype) prototype {
	var out prototype
	out.Raw("\t")
	for _, tok := range in {
		if tok.Type == tokenRaw {
			tok.Value = strings.ReplaceAll(tok.Value, "\n", "\n\t")
		}
		out = append(out, tok)
	}
	return out
}

func isExportedSymbol(l []string, sym string) bool {
	if len(l) == 0 {
		return true
	}

	for _, pattern := range l {
		ok, err := path.Match(pattern, sym)
		if err != nil {
			log.Fatalf("failed to match exported symbol pattern %q: %v", pattern, err)
		} else if ok {
			return true
		}
	}

	return false
}

func applyFilePrefixMap(filename string, m [][2]string) string {
	for _, entry := range m {
		old, new := entry[0], entry[1]
		if strings.HasPrefix(filename, old) {
			return strings.Replace(filename, old, new, 1)
		}
	}
	return filename
}

func relPath(base, target string) string {
	baseElems := strings.Split(path.Clean(base), "/")
	targetElems := strings.Split(path.Clean(target), "/")

	// Skip common elements
	i := 0
	for i < len(baseElems) && i < len(targetElems) && baseElems[i] == targetElems[i] {
		i++
	}

	var out []string
	for j := i; j < len(baseElems)-1; j++ {
		out = append(out, "..")
	}
	for j := i; j < len(targetElems); j++ {
		out = append(out, targetElems[j])
	}
	return path.Join(out...)
}

// TODO: replace with strings.Cut once Go 1.18 is widespread
func cut(s, sep string) (before, after string, found bool) {
	split := strings.SplitN(s, sep, 2)
	if len(split) == 2 {
		return split[0], split[1], true
	} else {
		return s, "", false
	}
}

func renderTemplate(tpl *template.Template, outputFilename, name string, data interface{}) {
	if err := os.MkdirAll(filepath.Dir(outputFilename), 0755); err != nil {
		log.Fatalf("failed to create output directory: %v", err)
	}

	f, err := os.Create(outputFilename)
	if err != nil {
		log.Fatalf("failed to create output file: %v", err)
	}
	defer f.Close()

	if err := tpl.ExecuteTemplate(f, name, data); err != nil {
		log.Fatal(err)
	}
}

func main() {
	opts, optind, err := getopt.Getopts(os.Args, "I:D:U:f:o:")
	if err != nil {
		log.Fatal(err)
	}
	targets := os.Args[optind:]

	if len(targets) == 0 {
		log.Fatal("no input files specified")
	}

	var D, U, I, exportedSymbols []string
	outputDir := "out"
	var filePrefixMap [][2]string
	for _, opt := range opts {
		switch opt.Option {
		case 'I':
			I = append(I, opt.Value)
		case 'D':
			D = append(D, opt.Value)
		case 'U':
			U = append(U, opt.Value)
		case 'f':
			name, value, _ := cut(opt.Value, "=")
			switch name {
			case "exported-symbols":
				exportedSymbols = append(exportedSymbols, value)
			case "file-prefix-map":
				old, new, ok := cut(value, "=")
				if !ok {
					log.Fatal("invalid syntax in -ffile-prefix-map")
				}
				filePrefixMap = append(filePrefixMap, [2]string{old, new})
			default:
				log.Fatalf("unknown -f option: %v", name)
			}
		case 'o':
			outputDir = opt.Value
		}
	}

	hostPredefined, hostIncludePaths, hostSysIncludePaths, err := cc.HostConfig("")
	if err != nil {
		log.Fatalf("failed to get host config: %v", err)
	}

	sources := []cc.Source{
		{Name: "<predefined>", Value: hostPredefined},
		{Name: "<builtin>", Value: builtin},
	}

	if s := generateDefines(D); s != "" {
		sources = append(sources, cc.Source{Name: "<defines>", Value: s})
	}
	if s := generateUndefines(U); s != "" {
		sources = append(sources, cc.Source{Name: "<undefines>", Value: s})
	}

	targetMap := make(map[string]string, len(targets))
	for _, target := range targets {
		fi, err := os.Stat(target)
		if err != nil {
			log.Fatalf("failed to stat input file %q: %v", target, err)
		}

		var filenames []string
		if fi.IsDir() {
			err := filepath.WalkDir(target, func(path string, de fs.DirEntry, err error) error {
				if err != nil {
					return err
				}
				if !de.Type().IsRegular() || filepath.Ext(path) != ".h" {
					return nil
				}
				filenames = append(filenames, path)
				return nil
			})
			if err != nil {
				log.Fatalf("failed to walk dir %q: %v", target, err)
			}
		} else {
			filenames = []string{target}
		}

		for _, filename := range filenames {
			origFilename := applyFilePrefixMap(filename, filePrefixMap)

			// cc evaluates symlinks
			filename, err := filepath.EvalSymlinks(filename)
			if err != nil {
				log.Fatalf("failed to eval symlinks: %v", err)
			}

			// cc expects absolute paths
			filename, err = filepath.Abs(filename)
			if err != nil {
				log.Fatalf("failed to get absolute path: %v", err)
			}

			targetMap[filename] = origFilename
			sources = append(sources, cc.Source{Name: filename})
		}
	}

	// Priority order is relative (for #include "..." only), then -I, then the
	// usual places. See:
	// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/c99.html
	includePaths := append([]string{"@"}, I...)
	includePaths = append(includePaths, hostIncludePaths...)
	includePaths = append(includePaths, hostSysIncludePaths...)

	sysIncludePaths := append([]string(nil), I...)
	sysIncludePaths = append(sysIncludePaths, hostSysIncludePaths...)

	cfg := new(cc.Config)
	cfg.PreserveWhiteSpace = true
	ast, err := cc.Parse(cfg, includePaths, sysIncludePaths, sources)
	if err != nil {
		log.Fatalf("parse error: %v", err)
	}

	var decls []declData
	for tu := ast.TranslationUnit; tu != nil; tu = tu.TranslationUnit {
		if tu.ExternalDeclaration == nil {
			continue
		}
		if tu.ExternalDeclaration.Case != cc.ExternalDeclarationDecl {
			continue
		}
		decl := tu.ExternalDeclaration.Declaration

		origFilename, ok := targetMap[decl.Position().Filename]
		if !ok {
			continue
		}

		if decl.DeclarationSpecifiers.Case != cc.DeclarationSpecifiersTypeSpec {
			continue
		}
		typeSpec := decl.DeclarationSpecifiers.TypeSpecifier
		typeSpecProto := typeSpecifierPrototype(typeSpec)

		if decl.InitDeclaratorList == nil {
			proto := append(typeSpecProto, token{Value: ";"})
			switch typeSpec.Case {
			case cc.TypeSpecifierStructOrUnion:
				structOrUnionSpec := typeSpec.StructOrUnionSpecifier
				kind := structOrUnionSpec.StructOrUnion.Token.Value.String()
				decls = append(decls, declData{
					Kind:        declKind(kind),
					Name:        structOrUnionSpec.Token.Value.String(),
					Prototype:   proto,
					Description: extractDocComment(structOrUnionSpec.StructOrUnion.Token),
					Filename:    origFilename,
				})
			case cc.TypeSpecifierEnum:
				enumSpec := typeSpec.EnumSpecifier
				decls = append(decls, declData{
					Kind:        declEnum,
					Name:        enumSpec.Token2.Value.String(),
					Prototype:   proto,
					Description: extractDocComment(enumSpec.Token),
					Filename:    origFilename,
				})
			}
		} else {
			for idl := decl.InitDeclaratorList; idl != nil; idl = idl.InitDeclaratorList {
				var kind declKind
				switch idl.InitDeclarator.Declarator.DirectDeclarator.Case {
				case cc.DirectDeclaratorIdent:
					kind = declVar
				case cc.DirectDeclaratorFuncParam:
					kind = declFunc
				default:
					panic(fmt.Sprintf("TODO: %v", idl.InitDeclarator.Declarator.DirectDeclarator.Case))
				}
				var proto prototype
				proto.Add(typeSpecProto...).Raw(" ").Add(declaratorPrototype(idl.InitDeclarator.Declarator)...).Raw(";")
				decls = append(decls, declData{
					Kind:        kind,
					Name:        idl.InitDeclarator.Declarator.Name().String(),
					Prototype:   proto,
					Description: extractDocComment(typeSpecifierFirstToken(typeSpec)),
					Filename:    origFilename,
				})
			}
		}
	}

	declMap := make(map[declKey]declData)
	for _, decl := range decls {
		if prev, ok := declMap[decl.key()]; ok {
			// Prefer the declaration with a longer prototype. In case of a
			// tie (e.g. because both are opaque structs), prefer the
			// declaration with a longer description.
			if len(decl.Prototype) < len(prev.Prototype) {
				continue
			} else if decl.Prototype == nil && len(decl.Description) < len(prev.Description) {
				continue
			}
		}
		if !isExportedSymbol(exportedSymbols, decl.Name) {
			continue
		}

		declMap[decl.key()] = decl
	}

	declByFile := make(map[string][]declData)
	decls = nil
	for _, decl := range declMap {
		declByFile[decl.Filename] = append(declByFile[decl.Filename], decl)
		decls = append(decls, decl)
	}

	sort.Slice(decls, func(i, j int) bool {
		return decls[i].Name < decls[j].Name
	})

	tpl := template.New("gyosu")
	tpl.Funcs(template.FuncMap{
		"symhref": func(kind declKind, name string) string {
			return symHref(declMap, kind, name)
		},
		"unithref": unitHref,
	})
	template.Must(tpl.ParseGlob("template/*.html"))

	if err := os.RemoveAll(outputDir); err != nil {
		log.Fatalf("failed to remove %q: %v", outputDir, err)
	}

	data := indexData{
		Title: "Documentation",
		Decls: decls,
	}
	for filename, _ := range declByFile {
		data.Filenames = append(data.Filenames, filename)
	}
	sort.Strings(data.Filenames)

	indexFilename := filepath.Join(outputDir, "index.html")
	renderTemplate(tpl, indexFilename, "index.html", &data)

	for filename, decls := range declByFile {
		filename := filename // capture variable

		basePath := filepath.ToSlash(filename + ".html")

		data := unitData{
			Title:     "Documentation for <" + filename + ">",
			Decls:     decls,
			IndexHref: relPath(basePath, ""),
		}

		sort.Slice(data.Decls, func(i, j int) bool {
			return data.Decls[i].Name < data.Decls[j].Name
		})

		tpl.Funcs(template.FuncMap{
			"symhref": func(kind declKind, name string) string {
				target := symHref(declMap, kind, name)
				if target == "" {
					return ""
				}
				return relPath(basePath, target)
			},
			"unithref": func(name string) string {
				target := unitHref(name)
				return relPath(basePath, target)
			},
		})

		outputFilename := filepath.Join(outputDir, filename+".html")
		renderTemplate(tpl, outputFilename, "unit.html", &data)
	}
}

func unitHref(filename string) string {
	return filepath.ToSlash(filename + ".html")
}

func symHref(declMap map[declKey]declData, kind declKind, name string) string {
	k := declKey{Kind: kind, Name: name}
	decl, ok := declMap[k]
	if !ok {
		return ""
	}

	path := filepath.ToSlash(decl.Filename + ".html")
	anchor := fmt.Sprintf("#%v-%v", kind, name)
	return path + anchor
}