Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / mvdan.cc / gofumpt@v0.0.0-20200802201014-ab5a8192947d / gofumports / internal / imports / mkstdlib.go
1 // +build ignore
2
3 // mkstdlib generates the zstdlib.go file, containing the Go standard
4 // library API symbols. It's baked into the binary to avoid scanning
5 // GOPATH in the common case.
6 package main
7
8 import (
9         "bufio"
10         "bytes"
11         "fmt"
12         "go/format"
13         "io"
14         "io/ioutil"
15         "log"
16         "os"
17         "os/exec"
18         "path/filepath"
19         "regexp"
20         "runtime"
21         "sort"
22 )
23
24 func mustOpen(name string) io.Reader {
25         f, err := os.Open(name)
26         if err != nil {
27                 log.Fatal(err)
28         }
29         return f
30 }
31
32 func api(base string) string {
33         return filepath.Join(runtime.GOROOT(), "api", base)
34 }
35
36 var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
37
38 var unsafeSyms = map[string]bool{"Alignof": true, "ArbitraryType": true, "Offsetof": true, "Pointer": true, "Sizeof": true}
39
40 func main() {
41         var buf bytes.Buffer
42         outf := func(format string, args ...interface{}) {
43                 fmt.Fprintf(&buf, format, args...)
44         }
45         outf("// Code generated by mkstdlib.go. DO NOT EDIT.\n\n")
46         outf("package imports\n")
47         outf("var stdlib = map[string][]string{\n")
48         f := io.MultiReader(
49                 mustOpen(api("go1.txt")),
50                 mustOpen(api("go1.1.txt")),
51                 mustOpen(api("go1.2.txt")),
52                 mustOpen(api("go1.3.txt")),
53                 mustOpen(api("go1.4.txt")),
54                 mustOpen(api("go1.5.txt")),
55                 mustOpen(api("go1.6.txt")),
56                 mustOpen(api("go1.7.txt")),
57                 mustOpen(api("go1.8.txt")),
58                 mustOpen(api("go1.9.txt")),
59                 mustOpen(api("go1.10.txt")),
60                 mustOpen(api("go1.11.txt")),
61                 mustOpen(api("go1.12.txt")),
62                 mustOpen(api("go1.13.txt")),
63                 mustOpen(api("go1.14.txt")),
64
65                 // The API of the syscall/js package needs to be computed explicitly,
66                 // because it's not included in the GOROOT/api/go1.*.txt files at this time.
67                 syscallJSAPI(),
68         )
69         sc := bufio.NewScanner(f)
70
71         pkgs := map[string]map[string]bool{
72                 "unsafe": unsafeSyms,
73         }
74         paths := []string{"unsafe"}
75
76         for sc.Scan() {
77                 l := sc.Text()
78                 if m := sym.FindStringSubmatch(l); m != nil {
79                         path, sym := m[1], m[2]
80
81                         if _, ok := pkgs[path]; !ok {
82                                 pkgs[path] = map[string]bool{}
83                                 paths = append(paths, path)
84                         }
85                         pkgs[path][sym] = true
86                 }
87         }
88         if err := sc.Err(); err != nil {
89                 log.Fatal(err)
90         }
91         sort.Strings(paths)
92         for _, path := range paths {
93                 outf("\t%q: []string{\n", path)
94                 pkg := pkgs[path]
95                 var syms []string
96                 for sym := range pkg {
97                         syms = append(syms, sym)
98                 }
99                 sort.Strings(syms)
100                 for _, sym := range syms {
101                         outf("\t\t%q,\n", sym)
102                 }
103                 outf("},\n")
104         }
105         outf("}\n")
106         fmtbuf, err := format.Source(buf.Bytes())
107         if err != nil {
108                 log.Fatal(err)
109         }
110         err = ioutil.WriteFile("zstdlib.go", fmtbuf, 0o666)
111         if err != nil {
112                 log.Fatal(err)
113         }
114 }
115
116 // syscallJSAPI returns the API of the syscall/js package.
117 // It's computed from the contents of $(go env GOROOT)/src/syscall/js.
118 func syscallJSAPI() io.Reader {
119         var exeSuffix string
120         if runtime.GOOS == "windows" {
121                 exeSuffix = ".exe"
122         }
123         cmd := exec.Command("go"+exeSuffix, "run", "cmd/api", "-contexts", "js-wasm", "syscall/js")
124         out, err := cmd.Output()
125         if err != nil {
126                 log.Fatalln(err)
127         }
128         return bytes.NewReader(out)
129 }