Skip to content

Commit 7240af8

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/cache: remove parsego.* aliases
Also, remove redundant "Parse" prefix from Full, Header. Change-Id: Iba9e1d0f128438232e5afb8b9e2a979a61136a83 Reviewed-on: https://go-review.googlesource.com/c/tools/+/564336 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent babbbed commit 7240af8

39 files changed

+155
-149
lines changed

gopls/internal/cache/analysis.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ import (
3232

3333
"golang.org/x/sync/errgroup"
3434
"golang.org/x/tools/go/analysis"
35+
"golang.org/x/tools/gopls/internal/cache/metadata"
36+
"golang.org/x/tools/gopls/internal/cache/parsego"
3537
"golang.org/x/tools/gopls/internal/file"
3638
"golang.org/x/tools/gopls/internal/filecache"
37-
"golang.org/x/tools/gopls/internal/cache/metadata"
38-
"golang.org/x/tools/gopls/internal/protocol"
3939
"golang.org/x/tools/gopls/internal/progress"
40+
"golang.org/x/tools/gopls/internal/protocol"
4041
"golang.org/x/tools/gopls/internal/settings"
4142
"golang.org/x/tools/gopls/internal/util/astutil"
4243
"golang.org/x/tools/gopls/internal/util/bug"
@@ -785,7 +786,7 @@ func (an *analysisNode) cacheKey() [sha256.Size]byte {
785786
func (an *analysisNode) run(ctx context.Context) (*analyzeSummary, error) {
786787
// Parse only the "compiled" Go files.
787788
// Do the computation in parallel.
788-
parsed := make([]*ParsedGoFile, len(an.files))
789+
parsed := make([]*parsego.File, len(an.files))
789790
{
790791
var group errgroup.Group
791792
group.SetLimit(4) // not too much: run itself is already called in parallel
@@ -796,7 +797,7 @@ func (an *analysisNode) run(ctx context.Context) (*analyzeSummary, error) {
796797
// as cached ASTs require the global FileSet.
797798
// ast.Object resolution is unfortunately an implied part of the
798799
// go/analysis contract.
799-
pgf, err := parseGoImpl(ctx, an.fset, fh, ParseFull&^parser.SkipObjectResolution, false)
800+
pgf, err := parseGoImpl(ctx, an.fset, fh, parsego.Full&^parser.SkipObjectResolution, false)
800801
parsed[i] = pgf
801802
return err
802803
})
@@ -910,7 +911,7 @@ func (an *analysisNode) run(ctx context.Context) (*analyzeSummary, error) {
910911
}
911912

912913
// Postcondition: analysisPackage.types and an.exportDeps are populated.
913-
func (an *analysisNode) typeCheck(parsed []*ParsedGoFile) *analysisPackage {
914+
func (an *analysisNode) typeCheck(parsed []*parsego.File) *analysisPackage {
914915
mp := an.mp
915916

916917
if false { // debugging
@@ -1101,7 +1102,7 @@ func readShallowManifest(export []byte) ([]PackagePath, error) {
11011102
type analysisPackage struct {
11021103
mp *metadata.Package
11031104
fset *token.FileSet // local to this package
1104-
parsed []*ParsedGoFile
1105+
parsed []*parsego.File
11051106
files []*ast.File // same as parsed[i].File
11061107
types *types.Package
11071108
compiles bool // package is transitively free of list/parse/type errors

gopls/internal/cache/check.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"golang.org/x/sync/errgroup"
2424
"golang.org/x/tools/go/ast/astutil"
2525
"golang.org/x/tools/gopls/internal/cache/metadata"
26+
"golang.org/x/tools/gopls/internal/cache/parsego"
2627
"golang.org/x/tools/gopls/internal/cache/typerefs"
2728
"golang.org/x/tools/gopls/internal/file"
2829
"golang.org/x/tools/gopls/internal/filecache"
@@ -675,7 +676,7 @@ func (b *typeCheckBatch) checkPackageForImport(ctx context.Context, ph *packageH
675676
// Parse the compiled go files, bypassing the parse cache as packages checked
676677
// for import are unlikely to get cache hits. Additionally, we can optimize
677678
// parsing slightly by not passing parser.ParseComments.
678-
pgfs := make([]*ParsedGoFile, len(ph.localInputs.compiledGoFiles))
679+
pgfs := make([]*parsego.File, len(ph.localInputs.compiledGoFiles))
679680
{
680681
var group errgroup.Group
681682
// Set an arbitrary concurrency limit; we want some parallelism but don't
@@ -1271,7 +1272,7 @@ func (s *Snapshot) typerefData(ctx context.Context, id PackageID, imports map[Im
12711272
bug.Reportf("internal error reading typerefs data: %v", err)
12721273
}
12731274

1274-
pgfs, err := s.view.parseCache.parseFiles(ctx, token.NewFileSet(), ParseFull&^parser.ParseComments, true, cgfs...)
1275+
pgfs, err := s.view.parseCache.parseFiles(ctx, token.NewFileSet(), parsego.Full&^parser.ParseComments, true, cgfs...)
12751276
if err != nil {
12761277
return nil, err
12771278
}
@@ -1471,11 +1472,11 @@ func (b *typeCheckBatch) checkPackage(ctx context.Context, ph *packageHandle) (*
14711472
// Collect parsed files from the type check pass, capturing parse errors from
14721473
// compiled files.
14731474
var err error
1474-
pkg.goFiles, err = b.parseCache.parseFiles(ctx, b.fset, ParseFull, false, inputs.goFiles...)
1475+
pkg.goFiles, err = b.parseCache.parseFiles(ctx, b.fset, parsego.Full, false, inputs.goFiles...)
14751476
if err != nil {
14761477
return nil, err
14771478
}
1478-
pkg.compiledGoFiles, err = b.parseCache.parseFiles(ctx, b.fset, ParseFull, false, inputs.compiledGoFiles...)
1479+
pkg.compiledGoFiles, err = b.parseCache.parseFiles(ctx, b.fset, parsego.Full, false, inputs.compiledGoFiles...)
14791480
if err != nil {
14801481
return nil, err
14811482
}
@@ -1670,12 +1671,12 @@ func depsErrors(ctx context.Context, snapshot *Snapshot, mp *metadata.Package) (
16701671

16711672
// Build an index of all imports in the package.
16721673
type fileImport struct {
1673-
cgf *ParsedGoFile
1674+
cgf *parsego.File
16741675
imp *ast.ImportSpec
16751676
}
16761677
allImports := map[string][]fileImport{}
16771678
for _, uri := range mp.CompiledGoFiles {
1678-
pgf, err := parseGoURI(ctx, snapshot, uri, ParseHeader)
1679+
pgf, err := parseGoURI(ctx, snapshot, uri, parsego.Header)
16791680
if err != nil {
16801681
return nil, err
16811682
}

gopls/internal/cache/errors.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"golang.org/x/tools/go/packages"
2424
"golang.org/x/tools/gopls/internal/cache/metadata"
25+
"golang.org/x/tools/gopls/internal/cache/parsego"
2526
"golang.org/x/tools/gopls/internal/file"
2627
"golang.org/x/tools/gopls/internal/protocol"
2728
"golang.org/x/tools/gopls/internal/protocol/command"
@@ -464,7 +465,7 @@ func parseGoListImportCycleError(ctx context.Context, e packages.Error, mp *meta
464465
// Imports have quotation marks around them.
465466
circImp := strconv.Quote(importList[1])
466467
for _, uri := range mp.CompiledGoFiles {
467-
pgf, err := parseGoURI(ctx, fs, uri, ParseHeader)
468+
pgf, err := parseGoURI(ctx, fs, uri, parsego.Header)
468469
if err != nil {
469470
return nil, err
470471
}
@@ -497,7 +498,7 @@ func parseGoListImportCycleError(ctx context.Context, e packages.Error, mp *meta
497498
// It returns an error if the file could not be read.
498499
//
499500
// TODO(rfindley): eliminate this helper.
500-
func parseGoURI(ctx context.Context, fs file.Source, uri protocol.DocumentURI, mode parser.Mode) (*ParsedGoFile, error) {
501+
func parseGoURI(ctx context.Context, fs file.Source, uri protocol.DocumentURI, mode parser.Mode) (*parsego.File, error) {
501502
fh, err := fs.ReadFile(ctx, uri)
502503
if err != nil {
503504
return nil, err

gopls/internal/cache/mod_tidy.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import (
1616
"strings"
1717

1818
"golang.org/x/mod/modfile"
19+
"golang.org/x/tools/gopls/internal/cache/parsego"
1920
"golang.org/x/tools/gopls/internal/file"
20-
"golang.org/x/tools/gopls/internal/protocol/command"
2121
"golang.org/x/tools/gopls/internal/protocol"
22+
"golang.org/x/tools/gopls/internal/protocol/command"
2223
"golang.org/x/tools/internal/diff"
2324
"golang.org/x/tools/internal/event"
2425
"golang.org/x/tools/internal/event/tag"
@@ -281,7 +282,7 @@ func missingModuleDiagnostics(ctx context.Context, snapshot *Snapshot, pm *Parse
281282
continue
282283
}
283284
for _, goFile := range compiledGoFiles {
284-
pgf, err := snapshot.ParseGo(ctx, goFile, ParseHeader)
285+
pgf, err := snapshot.ParseGo(ctx, goFile, parsego.Header)
285286
if err != nil {
286287
continue
287288
}
@@ -461,7 +462,7 @@ func switchDirectness(req *modfile.Require, m *protocol.Mapper) ([]protocol.Text
461462

462463
// missingModuleForImport creates an error for a given import path that comes
463464
// from a missing module.
464-
func missingModuleForImport(pgf *ParsedGoFile, imp *ast.ImportSpec, req *modfile.Require, fixes []SuggestedFix) (*Diagnostic, error) {
465+
func missingModuleForImport(pgf *parsego.File, imp *ast.ImportSpec, req *modfile.Require, fixes []SuggestedFix) (*Diagnostic, error) {
465466
if req.Syntax == nil {
466467
return nil, fmt.Errorf("no syntax for %v", req)
467468
}
@@ -488,7 +489,7 @@ func missingModuleForImport(pgf *ParsedGoFile, imp *ast.ImportSpec, req *modfile
488489
//
489490
// TODO(rfindley): this should key off ImportPath.
490491
func parseImports(ctx context.Context, s *Snapshot, files []file.Handle) (map[string]bool, error) {
491-
pgfs, err := s.view.parseCache.parseFiles(ctx, token.NewFileSet(), ParseHeader, false, files...)
492+
pgfs, err := s.view.parseCache.parseFiles(ctx, token.NewFileSet(), parsego.Header, false, files...)
492493
if err != nil { // e.g. context cancellation
493494
return nil, err
494495
}

gopls/internal/cache/parse.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111
"go/token"
1212
"path/filepath"
1313

14-
"golang.org/x/tools/gopls/internal/file"
1514
"golang.org/x/tools/gopls/internal/cache/parsego"
15+
"golang.org/x/tools/gopls/internal/file"
1616
)
1717

1818
// ParseGo parses the file whose contents are provided by fh.
1919
// The resulting tree may have been fixed up.
2020
// If the file is not available, returns nil and an error.
21-
func (s *Snapshot) ParseGo(ctx context.Context, fh file.Handle, mode parser.Mode) (*ParsedGoFile, error) {
21+
func (s *Snapshot) ParseGo(ctx context.Context, fh file.Handle, mode parser.Mode) (*parsego.File, error) {
2222
pgfs, err := s.view.parseCache.parseFiles(ctx, token.NewFileSet(), mode, false, fh)
2323
if err != nil {
2424
return nil, err
@@ -27,7 +27,7 @@ func (s *Snapshot) ParseGo(ctx context.Context, fh file.Handle, mode parser.Mode
2727
}
2828

2929
// parseGoImpl parses the Go source file whose content is provided by fh.
30-
func parseGoImpl(ctx context.Context, fset *token.FileSet, fh file.Handle, mode parser.Mode, purgeFuncBodies bool) (*ParsedGoFile, error) {
30+
func parseGoImpl(ctx context.Context, fset *token.FileSet, fh file.Handle, mode parser.Mode, purgeFuncBodies bool) (*parsego.File, error) {
3131
ext := filepath.Ext(fh.URI().Path())
3232
if ext != ".go" && ext != "" { // files generated by cgo have no extension
3333
return nil, fmt.Errorf("cannot parse non-Go file %s", fh.URI())

gopls/internal/cache/parse_cache.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"time"
1818

1919
"golang.org/x/sync/errgroup"
20-
"golang.org/x/tools/gopls/internal/file"
2120
"golang.org/x/tools/gopls/internal/cache/parsego"
21+
"golang.org/x/tools/gopls/internal/file"
2222
"golang.org/x/tools/gopls/internal/protocol"
2323
"golang.org/x/tools/internal/memoize"
2424
"golang.org/x/tools/internal/tokeninternal"
@@ -135,7 +135,7 @@ type parseKey struct {
135135
type parseCacheEntry struct {
136136
key parseKey
137137
hash file.Hash
138-
promise *memoize.Promise // memoize.Promise[*ParsedGoFile]
138+
promise *memoize.Promise // memoize.Promise[*parsego.File]
139139
atime uint64 // clock time of last access, for use in LRU sorting
140140
walltime time.Time // actual time of last access, for use in time-based eviction; too coarse for LRU on some systems
141141
lruIndex int // owned by the queue implementation
@@ -303,7 +303,7 @@ func (c *parseCache) allocateSpace(size int) (int, int) {
303303
return base, c.nextBase
304304
}
305305

306-
// parseFiles returns a ParsedGoFile for each file handle in fhs, in the
306+
// parseFiles returns a parsego.File for each file handle in fhs, in the
307307
// requested parse mode.
308308
//
309309
// For parsed files that already exists in the cache, access time will be
@@ -317,8 +317,8 @@ func (c *parseCache) allocateSpace(size int) (int, int) {
317317
//
318318
// If parseFiles returns an error, it still returns a slice,
319319
// but with a nil entry for each file that could not be parsed.
320-
func (c *parseCache) parseFiles(ctx context.Context, fset *token.FileSet, mode parser.Mode, purgeFuncBodies bool, fhs ...file.Handle) ([]*ParsedGoFile, error) {
321-
pgfs := make([]*ParsedGoFile, len(fhs))
320+
func (c *parseCache) parseFiles(ctx context.Context, fset *token.FileSet, mode parser.Mode, purgeFuncBodies bool, fhs ...file.Handle) ([]*parsego.File, error) {
321+
pgfs := make([]*parsego.File, len(fhs))
322322

323323
// Temporary fall-back for 32-bit systems, where reservedForParsing is too
324324
// small to be viable. We don't actually support 32-bit systems, so this
@@ -351,7 +351,7 @@ func (c *parseCache) parseFiles(ctx context.Context, fset *token.FileSet, mode p
351351
if err != nil {
352352
return err
353353
}
354-
pgfs[i] = result.(*ParsedGoFile)
354+
pgfs[i] = result.(*parsego.File)
355355
return nil
356356
})
357357
}

gopls/internal/cache/parse_cache_test.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313
"time"
1414

15+
"golang.org/x/tools/gopls/internal/cache/parsego"
1516
"golang.org/x/tools/gopls/internal/file"
1617
"golang.org/x/tools/gopls/internal/protocol"
1718
)
@@ -31,12 +32,12 @@ func TestParseCache(t *testing.T) {
3132
fset := token.NewFileSet()
3233

3334
cache := newParseCache(0)
34-
pgfs1, err := cache.parseFiles(ctx, fset, ParseFull, false, fh)
35+
pgfs1, err := cache.parseFiles(ctx, fset, parsego.Full, false, fh)
3536
if err != nil {
3637
t.Fatal(err)
3738
}
3839
pgf1 := pgfs1[0]
39-
pgfs2, err := cache.parseFiles(ctx, fset, ParseFull, false, fh)
40+
pgfs2, err := cache.parseFiles(ctx, fset, parsego.Full, false, fh)
4041
pgf2 := pgfs2[0]
4142
if err != nil {
4243
t.Fatal(err)
@@ -50,7 +51,7 @@ func TestParseCache(t *testing.T) {
5051
files := []file.Handle{fh}
5152
files = append(files, dummyFileHandles(parseCacheMinFiles-1)...)
5253

53-
pgfs3, err := cache.parseFiles(ctx, fset, ParseFull, false, files...)
54+
pgfs3, err := cache.parseFiles(ctx, fset, parsego.Full, false, files...)
5455
if err != nil {
5556
t.Fatal(err)
5657
}
@@ -68,13 +69,13 @@ func TestParseCache(t *testing.T) {
6869
// Now overwrite the cache, after which we should get new results.
6970
cache.gcOnce()
7071
files = dummyFileHandles(parseCacheMinFiles)
71-
_, err = cache.parseFiles(ctx, fset, ParseFull, false, files...)
72+
_, err = cache.parseFiles(ctx, fset, parsego.Full, false, files...)
7273
if err != nil {
7374
t.Fatal(err)
7475
}
7576
// force a GC, which should collect the recently parsed files
7677
cache.gcOnce()
77-
pgfs4, err := cache.parseFiles(ctx, fset, ParseFull, false, fh)
78+
pgfs4, err := cache.parseFiles(ctx, fset, parsego.Full, false, fh)
7879
if err != nil {
7980
t.Fatal(err)
8081
}
@@ -98,7 +99,7 @@ func TestParseCache_Reparsing(t *testing.T) {
9899

99100
// Parsing should succeed even though we overflow the padding.
100101
cache := newParseCache(0)
101-
_, err := cache.parseFiles(context.Background(), token.NewFileSet(), ParseFull, false, files...)
102+
_, err := cache.parseFiles(context.Background(), token.NewFileSet(), parsego.Full, false, files...)
102103
if err != nil {
103104
t.Fatal(err)
104105
}
@@ -118,7 +119,7 @@ func TestParseCache_Issue59097(t *testing.T) {
118119

119120
// Parsing should succeed even though we overflow the padding.
120121
cache := newParseCache(0)
121-
_, err := cache.parseFiles(context.Background(), token.NewFileSet(), ParseFull, false, files...)
122+
_, err := cache.parseFiles(context.Background(), token.NewFileSet(), parsego.Full, false, files...)
122123
if err != nil {
123124
t.Fatal(err)
124125
}
@@ -136,19 +137,19 @@ func TestParseCache_TimeEviction(t *testing.T) {
136137
cache := newParseCache(gcDuration)
137138
cache.stop() // we'll manage GC manually, for testing.
138139

139-
pgfs0, err := cache.parseFiles(ctx, fset, ParseFull, false, fh, fh)
140+
pgfs0, err := cache.parseFiles(ctx, fset, parsego.Full, false, fh, fh)
140141
if err != nil {
141142
t.Fatal(err)
142143
}
143144

144145
files := dummyFileHandles(parseCacheMinFiles)
145-
_, err = cache.parseFiles(ctx, fset, ParseFull, false, files...)
146+
_, err = cache.parseFiles(ctx, fset, parsego.Full, false, files...)
146147
if err != nil {
147148
t.Fatal(err)
148149
}
149150

150151
// Even after filling up the 'min' files, we get a cache hit for our original file.
151-
pgfs1, err := cache.parseFiles(ctx, fset, ParseFull, false, fh, fh)
152+
pgfs1, err := cache.parseFiles(ctx, fset, parsego.Full, false, fh, fh)
152153
if err != nil {
153154
t.Fatal(err)
154155
}
@@ -158,14 +159,14 @@ func TestParseCache_TimeEviction(t *testing.T) {
158159
}
159160

160161
// But after GC, we get a cache miss.
161-
_, err = cache.parseFiles(ctx, fset, ParseFull, false, files...) // mark dummy files as newer
162+
_, err = cache.parseFiles(ctx, fset, parsego.Full, false, files...) // mark dummy files as newer
162163
if err != nil {
163164
t.Fatal(err)
164165
}
165166
time.Sleep(gcDuration)
166167
cache.gcOnce()
167168

168-
pgfs2, err := cache.parseFiles(ctx, fset, ParseFull, false, fh, fh)
169+
pgfs2, err := cache.parseFiles(ctx, fset, parsego.Full, false, fh, fh)
169170
if err != nil {
170171
t.Fatal(err)
171172
}
@@ -183,7 +184,7 @@ func TestParseCache_Duplicates(t *testing.T) {
183184
fh := makeFakeFileHandle(uri, []byte("package p\n\nconst _ = \"foo\""))
184185

185186
cache := newParseCache(0)
186-
pgfs, err := cache.parseFiles(ctx, token.NewFileSet(), ParseFull, false, fh, fh)
187+
pgfs, err := cache.parseFiles(ctx, token.NewFileSet(), parsego.Full, false, fh, fh)
187188
if err != nil {
188189
t.Fatal(err)
189190
}

gopls/internal/cache/parsego/parse.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ import (
2525
// Common parse modes; these should be reused wherever possible to increase
2626
// cache hits.
2727
const (
28-
// ParseHeader specifies that the main package declaration and imports are needed.
28+
// Header specifies that the main package declaration and imports are needed.
2929
// This is the mode used when attempting to examine the package graph structure.
30-
ParseHeader = parser.AllErrors | parser.ParseComments | parser.ImportsOnly | parser.SkipObjectResolution
30+
Header = parser.AllErrors | parser.ParseComments | parser.ImportsOnly | parser.SkipObjectResolution
3131

32-
// ParseFull specifies the full AST is needed.
32+
// Full specifies the full AST is needed.
3333
// This is used for files of direct interest where the entire contents must
3434
// be considered.
35-
ParseFull = parser.AllErrors | parser.ParseComments | parser.SkipObjectResolution
35+
Full = parser.AllErrors | parser.ParseComments | parser.SkipObjectResolution
3636
)
3737

3838
// Parse parses a buffer of Go source, repairing the tree if necessary.

gopls/internal/cache/parsego/parse_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func _() {
3232
}
3333
`
3434

35-
pgf, _ := parsego.Parse(context.Background(), token.NewFileSet(), "file://foo.go", []byte(src), parsego.ParseFull, false)
35+
pgf, _ := parsego.Parse(context.Background(), token.NewFileSet(), "file://foo.go", []byte(src), parsego.Full, false)
3636
fset := tokeninternal.FileSetFor(pgf.Tok)
3737
ast.Inspect(pgf.File, func(n ast.Node) bool {
3838
if n != nil {

0 commit comments

Comments
 (0)