modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/stress/parsego.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "go/ast" 9 "go/parser" 10 "go/token" 11 "os" 12 "path" 13 "runtime" 14 "strings" 15 ) 16 17 func isGoFile(dir os.FileInfo) bool { 18 return !dir.IsDir() && 19 !strings.HasPrefix(dir.Name(), ".") && // ignore .files 20 path.Ext(dir.Name()) == ".go" 21 } 22 23 func isPkgFile(dir os.FileInfo) bool { 24 return isGoFile(dir) && 25 !strings.HasSuffix(dir.Name(), "_test.go") // ignore test files 26 } 27 28 func pkgName(filename string) string { 29 file, err := parser.ParseFile(token.NewFileSet(), filename, nil, parser.PackageClauseOnly) 30 if err != nil || file == nil { 31 return "" 32 } 33 return file.Name.Name 34 } 35 36 func parseDir(dirpath string) map[string]*ast.Package { 37 // the package name is the directory name within its parent. 38 // (use dirname instead of path because dirname is clean; it 39 // has no trailing '/') 40 _, pkgname := path.Split(dirpath) 41 42 // filter function to select the desired .go files 43 filter := func(d os.FileInfo) bool { 44 if isPkgFile(d) { 45 // Some directories contain main packages: Only accept 46 // files that belong to the expected package so that 47 // parser.ParsePackage doesn't return "multiple packages 48 // found" errors. 49 // Additionally, accept the special package name 50 // fakePkgName if we are looking at cmd documentation. 51 name := pkgName(dirpath + "/" + d.Name()) 52 return name == pkgname 53 } 54 return false 55 } 56 57 // get package AST 58 pkgs, err := parser.ParseDir(token.NewFileSet(), dirpath, filter, parser.ParseComments) 59 if err != nil { 60 println("parse", dirpath, err.Error()) 61 panic("go ParseDir fail: " + err.Error()) 62 } 63 return pkgs 64 } 65 66 func stressParseGo() { 67 pkgroot := runtime.GOROOT() + "/src/" 68 for { 69 m := make(map[string]map[string]*ast.Package) 70 for _, pkg := range packages { 71 m[pkg] = parseDir(pkgroot + pkg) 72 Println("parsed go package", pkg) 73 } 74 } 75 } 76 77 // find . -type d -not -path "./exp" -not -path "./exp/*" -printf "\t\"%p\",\n" | sort | sed "s/\.\///" | grep -v testdata 78 var packages = []string{ 79 "archive", 80 "archive/tar", 81 "archive/zip", 82 "bufio", 83 "builtin", 84 "bytes", 85 "compress", 86 "compress/bzip2", 87 "compress/flate", 88 "compress/gzip", 89 "compress/lzw", 90 "compress/zlib", 91 "container", 92 "container/heap", 93 "container/list", 94 "container/ring", 95 "crypto", 96 "crypto/aes", 97 "crypto/cipher", 98 "crypto/des", 99 "crypto/dsa", 100 "crypto/ecdsa", 101 "crypto/elliptic", 102 "crypto/hmac", 103 "crypto/md5", 104 "crypto/rand", 105 "crypto/rc4", 106 "crypto/rsa", 107 "crypto/sha1", 108 "crypto/sha256", 109 "crypto/sha512", 110 "crypto/subtle", 111 "crypto/tls", 112 "crypto/x509", 113 "crypto/x509/pkix", 114 "database", 115 "database/sql", 116 "database/sql/driver", 117 "debug", 118 "debug/dwarf", 119 "debug/elf", 120 "debug/gosym", 121 "debug/macho", 122 "debug/pe", 123 "encoding", 124 "encoding/ascii85", 125 "encoding/asn1", 126 "encoding/base32", 127 "encoding/base64", 128 "encoding/binary", 129 "encoding/csv", 130 "encoding/gob", 131 "encoding/hex", 132 "encoding/json", 133 "encoding/pem", 134 "encoding/xml", 135 "errors", 136 "expvar", 137 "flag", 138 "fmt", 139 "go", 140 "go/ast", 141 "go/build", 142 "go/doc", 143 "go/format", 144 "go/parser", 145 "go/printer", 146 "go/scanner", 147 "go/token", 148 "hash", 149 "hash/adler32", 150 "hash/crc32", 151 "hash/crc64", 152 "hash/fnv", 153 "html", 154 "html/template", 155 "image", 156 "image/color", 157 "image/draw", 158 "image/gif", 159 "image/jpeg", 160 "image/png", 161 "index", 162 "index/suffixarray", 163 "io", 164 "io/ioutil", 165 "log", 166 "log/syslog", 167 "math", 168 "math/big", 169 "math/cmplx", 170 "math/rand", 171 "mime", 172 "mime/multipart", 173 "net", 174 "net/http", 175 "net/http/cgi", 176 "net/http/cookiejar", 177 "net/http/fcgi", 178 "net/http/httptest", 179 "net/http/httputil", 180 "net/http/pprof", 181 "net/mail", 182 "net/rpc", 183 "net/rpc/jsonrpc", 184 "net/smtp", 185 "net/textproto", 186 "net/url", 187 "os", 188 "os/exec", 189 "os/signal", 190 "os/user", 191 "path", 192 "path/filepath", 193 "reflect", 194 "regexp", 195 "regexp/syntax", 196 "runtime", 197 "runtime/cgo", 198 "runtime/debug", 199 "runtime/pprof", 200 "runtime/race", 201 "sort", 202 "strconv", 203 "strings", 204 "sync", 205 "sync/atomic", 206 "syscall", 207 "testing", 208 "testing/iotest", 209 "testing/quick", 210 "text", 211 "text/scanner", 212 "text/tabwriter", 213 "text/template", 214 "text/template/parse", 215 "time", 216 "unicode", 217 "unicode/utf16", 218 "unicode/utf8", 219 "unsafe", 220 }