forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/tools/go/loader/util.go

211 lines
3.0 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
// Copyright 2013 The Go Authors. All rights reserved.
2024-05-14 13:07:09 +00:00
// Use of this source code is governed by a BSD-style
2024-05-14 13:07:09 +00:00
// license that can be found in the LICENSE file.
package loader
import (
"go/ast"
"go/build"
"go/parser"
"go/token"
"io"
"os"
"strconv"
"sync"
"golang.org/x/tools/go/buildutil"
)
// We use a counting semaphore to limit
2024-05-14 13:07:09 +00:00
// the number of parallel I/O calls per process.
2024-05-14 13:07:09 +00:00
var ioLimit = make(chan bool, 10)
// parseFiles parses the Go source files within directory dir and
2024-05-14 13:07:09 +00:00
// returns the ASTs of the ones that could be at least partially parsed,
2024-05-14 13:07:09 +00:00
// along with a list of I/O and parse errors encountered.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// I/O is done via ctxt, which may specify a virtual file system.
2024-05-14 13:07:09 +00:00
// displayPath is used to transform the filenames attached to the ASTs.
2024-05-14 13:07:09 +00:00
func parseFiles(fset *token.FileSet, ctxt *build.Context, displayPath func(string) string, dir string, files []string, mode parser.Mode) ([]*ast.File, []error) {
2024-05-14 13:07:09 +00:00
if displayPath == nil {
2024-05-14 13:07:09 +00:00
displayPath = func(path string) string { return path }
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
var wg sync.WaitGroup
2024-05-14 13:07:09 +00:00
n := len(files)
2024-05-14 13:07:09 +00:00
parsed := make([]*ast.File, n)
2024-05-14 13:07:09 +00:00
errors := make([]error, n)
2024-05-14 13:07:09 +00:00
for i, file := range files {
2024-05-14 13:07:09 +00:00
if !buildutil.IsAbsPath(ctxt, file) {
2024-05-14 13:07:09 +00:00
file = buildutil.JoinPath(ctxt, dir, file)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
wg.Add(1)
2024-05-14 13:07:09 +00:00
go func(i int, file string) {
2024-05-14 13:07:09 +00:00
ioLimit <- true // wait
2024-05-14 13:07:09 +00:00
defer func() {
2024-05-14 13:07:09 +00:00
wg.Done()
2024-05-14 13:07:09 +00:00
<-ioLimit // signal
2024-05-14 13:07:09 +00:00
}()
2024-05-14 13:07:09 +00:00
var rd io.ReadCloser
2024-05-14 13:07:09 +00:00
var err error
2024-05-14 13:07:09 +00:00
if ctxt.OpenFile != nil {
2024-05-14 13:07:09 +00:00
rd, err = ctxt.OpenFile(file)
2024-05-14 13:07:09 +00:00
} else {
2024-05-14 13:07:09 +00:00
rd, err = os.Open(file)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
errors[i] = err // open failed
2024-05-14 13:07:09 +00:00
return
2024-05-14 13:07:09 +00:00
}
// ParseFile may return both an AST and an error.
2024-05-14 13:07:09 +00:00
parsed[i], errors[i] = parser.ParseFile(fset, displayPath(file), rd, mode)
2024-05-14 13:07:09 +00:00
rd.Close()
2024-05-14 13:07:09 +00:00
}(i, file)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
wg.Wait()
// Eliminate nils, preserving order.
2024-05-14 13:07:09 +00:00
var o int
2024-05-14 13:07:09 +00:00
for _, f := range parsed {
2024-05-14 13:07:09 +00:00
if f != nil {
2024-05-14 13:07:09 +00:00
parsed[o] = f
2024-05-14 13:07:09 +00:00
o++
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
parsed = parsed[:o]
o = 0
2024-05-14 13:07:09 +00:00
for _, err := range errors {
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
errors[o] = err
2024-05-14 13:07:09 +00:00
o++
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
errors = errors[:o]
return parsed, errors
2024-05-14 13:07:09 +00:00
}
// scanImports returns the set of all import paths from all
2024-05-14 13:07:09 +00:00
// import specs in the specified files.
2024-05-14 13:07:09 +00:00
func scanImports(files []*ast.File) map[string]bool {
2024-05-14 13:07:09 +00:00
imports := make(map[string]bool)
2024-05-14 13:07:09 +00:00
for _, f := range files {
2024-05-14 13:07:09 +00:00
for _, decl := range f.Decls {
2024-05-14 13:07:09 +00:00
if decl, ok := decl.(*ast.GenDecl); ok && decl.Tok == token.IMPORT {
2024-05-14 13:07:09 +00:00
for _, spec := range decl.Specs {
2024-05-14 13:07:09 +00:00
spec := spec.(*ast.ImportSpec)
// NB: do not assume the program is well-formed!
2024-05-14 13:07:09 +00:00
path, err := strconv.Unquote(spec.Path.Value)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
continue // quietly ignore the error
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if path == "C" {
2024-05-14 13:07:09 +00:00
continue // skip pseudopackage
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
imports[path] = true
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return imports
2024-05-14 13:07:09 +00:00
}
// ---------- Internal helpers ----------
// TODO(adonovan): make this a method: func (*token.File) Contains(token.Pos)
2024-05-14 13:07:09 +00:00
func tokenFileContainsPos(f *token.File, pos token.Pos) bool {
2024-05-14 13:07:09 +00:00
p := int(pos)
2024-05-14 13:07:09 +00:00
base := f.Base()
2024-05-14 13:07:09 +00:00
return base <= p && p < base+f.Size()
2024-05-14 13:07:09 +00:00
}