Skip to content

Commit e5c7ac2

Browse files
committed
gopls: add CompiledAsmFiles in cache.Package
The CompiledAsmFiles field supports storing assembly files. Fixes golang/go#71754
1 parent fd68572 commit e5c7ac2

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

gopls/internal/cache/check.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,12 +1456,12 @@ type typeCheckInputs struct {
14561456
id PackageID
14571457

14581458
// Used for type checking:
1459-
pkgPath PackagePath
1460-
name PackageName
1461-
goFiles, compiledGoFiles []file.Handle
1462-
sizes types.Sizes
1463-
depsByImpPath map[ImportPath]PackageID
1464-
goVersion string // packages.Module.GoVersion, e.g. "1.18"
1459+
pkgPath PackagePath
1460+
name PackageName
1461+
goFiles, compiledGoFiles, asmFiles []file.Handle
1462+
sizes types.Sizes
1463+
depsByImpPath map[ImportPath]PackageID
1464+
goVersion string // packages.Module.GoVersion, e.g. "1.18"
14651465

14661466
// Used for type check diagnostics:
14671467
// TODO(rfindley): consider storing less data in gobDiagnostics, and
@@ -1491,6 +1491,10 @@ func (s *Snapshot) typeCheckInputs(ctx context.Context, mp *metadata.Package) (*
14911491
if err != nil {
14921492
return nil, err
14931493
}
1494+
asmFiles, err := readFiles(ctx, s, mp.AsmFiles)
1495+
if err != nil {
1496+
return nil, err
1497+
}
14941498

14951499
goVersion := ""
14961500
if mp.Module != nil && mp.Module.GoVersion != "" {
@@ -1503,6 +1507,7 @@ func (s *Snapshot) typeCheckInputs(ctx context.Context, mp *metadata.Package) (*
15031507
name: mp.Name,
15041508
goFiles: goFiles,
15051509
compiledGoFiles: compiledGoFiles,
1510+
asmFiles: asmFiles,
15061511
sizes: mp.TypesSizes,
15071512
depsByImpPath: mp.DepsByImpPath,
15081513
goVersion: goVersion,
@@ -1555,6 +1560,10 @@ func localPackageKey(inputs *typeCheckInputs) file.Hash {
15551560
for _, fh := range inputs.goFiles {
15561561
fmt.Fprintln(hasher, fh.Identity())
15571562
}
1563+
fmt.Fprintf(hasher, "asmFiles:%d\n", len(inputs.asmFiles))
1564+
for _, fh := range inputs.asmFiles {
1565+
fmt.Fprintln(hasher, fh.Identity())
1566+
}
15581567

15591568
// types sizes
15601569
wordSize := inputs.sizes.Sizeof(types.Typ[types.Int])
@@ -1611,6 +1620,10 @@ func (b *typeCheckBatch) checkPackage(ctx context.Context, fset *token.FileSet,
16111620
pkg.parseErrors = append(pkg.parseErrors, pgf.ParseErr)
16121621
}
16131622
}
1623+
pkg.asmFiles, err = parseAsmFiles(ctx, inputs.asmFiles...)
1624+
if err != nil {
1625+
return nil, err
1626+
}
16141627

16151628
// Use the default type information for the unsafe package.
16161629
if inputs.pkgPath == "unsafe" {

gopls/internal/cache/load.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,15 @@ func buildMetadata(updates map[PackageID]*metadata.Package, loadDir string, stan
454454
*dst = append(*dst, protocol.URIFromPath(filename))
455455
}
456456
}
457+
copyAsmURIs := func(dst *[]protocol.DocumentURI, src []string) {
458+
for _, filename := range src {
459+
if !strings.HasSuffix(filename, ".s") {
460+
continue
461+
}
462+
*dst = append(*dst, protocol.URIFromPath(filename))
463+
}
464+
}
465+
copyAsmURIs(&mp.AsmFiles, pkg.OtherFiles)
457466
copyURIs(&mp.CompiledGoFiles, pkg.CompiledGoFiles)
458467
copyURIs(&mp.GoFiles, pkg.GoFiles)
459468
copyURIs(&mp.IgnoredFiles, pkg.IgnoredFiles)

gopls/internal/cache/metadata/metadata.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Package struct {
4848
// These fields are as defined by go/packages.Package
4949
GoFiles []protocol.DocumentURI
5050
CompiledGoFiles []protocol.DocumentURI
51+
AsmFiles []protocol.DocumentURI
5152
IgnoredFiles []protocol.DocumentURI
5253
OtherFiles []protocol.DocumentURI
5354

gopls/internal/cache/package.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"golang.org/x/tools/gopls/internal/cache/testfuncs"
2020
"golang.org/x/tools/gopls/internal/cache/xrefs"
2121
"golang.org/x/tools/gopls/internal/protocol"
22+
"golang.org/x/tools/gopls/internal/util/asm"
2223
)
2324

2425
// Convenient aliases for very heavily used types.
@@ -49,6 +50,7 @@ type syntaxPackage struct {
4950
fset *token.FileSet // for now, same as the snapshot's FileSet
5051
goFiles []*parsego.File
5152
compiledGoFiles []*parsego.File
53+
asmFiles []*asm.File
5254
diagnostics []*Diagnostic
5355
parseErrors []scanner.ErrorList
5456
typeErrors []types.Error

gopls/internal/cache/parse.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"golang.org/x/tools/gopls/internal/cache/parsego"
1515
"golang.org/x/tools/gopls/internal/file"
16+
"golang.org/x/tools/gopls/internal/util/asm"
1617
)
1718

1819
// ParseGo parses the file whose contents are provided by fh.
@@ -43,3 +44,23 @@ func parseGoImpl(ctx context.Context, fset *token.FileSet, fh file.Handle, mode
4344
pgf, _ := parsego.Parse(ctx, fset, fh.URI(), content, mode, purgeFuncBodies) // ignore 'fixes'
4445
return pgf, nil
4546
}
47+
48+
// parseAsmFiles pases the assembly files whose contents are provided by fhs.
49+
func parseAsmFiles(ctx context.Context, fhs ...file.Handle) ([]*asm.File, error) {
50+
pgfs := make([]*asm.File, len(fhs))
51+
52+
for i, fh := range fhs {
53+
var err error
54+
content, err := fh.Content()
55+
if err != nil {
56+
return nil, err
57+
}
58+
// Check for context cancellation before actually doing the parse.
59+
if ctx.Err() != nil {
60+
return nil, ctx.Err()
61+
}
62+
pgfs[i] = asm.Parse(content)
63+
}
64+
return pgfs, nil
65+
66+
}

0 commit comments

Comments
 (0)