Debian Codegraph @

gopls/internal/golang/linkname.go

raw symbols SCIP
1 // Copyright 2023 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 golang
6
7 import (
8 "context"
9 "errors"
10 "fmt"
11 "go/token"
12 "strings"
13
14 "golang.org/x/tools/gopls/internal/cache"
15 "golang.org/x/tools/gopls/internal/cache/metadata"
16 "golang.org/x/tools/gopls/internal/cache/parsego"
17 "golang.org/x/tools/gopls/internal/protocol"
18 )
19
20 // ErrNoLinkname is returned by LinknameDefinition when no linkname
21 // directive is found at a particular position.
22 // As such it indicates that other definitions could be worth checking.
23 var ErrNoLinkname = errors.New("no linkname directive found")
24
25 // linknameDefinition finds the definition of the linkname directive in m at pos.
26 // If there is no linkname directive at pos, returns ErrNoLinkname.
27 func linknameDefinition(ctx context.Context, snapshot *cache.Snapshot, m *protocol.Mapper, from protocol.Range) ([]protocol.Location, error) {
28 pkgPath, name, _ := parseLinkname(m, from)
29 if pkgPath == "" {
30 return nil, ErrNoLinkname
31 }
32
33 _, pgf, pos, err := findLinkname(ctx, snapshot, PackagePath(pkgPath), name)
34 if err != nil {
35 return nil, fmt.Errorf("find linkname: %w", err)
36 }
37 loc, err := pgf.PosLocation(pos, pos+token.Pos(len(name)))
38 if err != nil {
39 return nil, fmt.Errorf("location of linkname: %w", err)
40 }
41 return []protocol.Location{loc}, nil
42 }
43
44 // parseLinkname attempts to parse a go:linkname declaration at the given range.
45 // If successful, it returns
46 // - package path referenced
47 // - object name referenced
48 // - byte offset in mapped file of the start of the link target
49 // of the linkname directives 2nd argument.
50 //
51 // If the range is not in the second argument of a go:linkname directive, or
52 // parsing fails, it returns "", "", 0.
53 func parseLinkname(m *protocol.Mapper, rng protocol.Range) (pkgPath, name string, targetOffset int) {
54 if rng.Start.Line != rng.End.Line {
55 return "", "", 0
56 }
57
58 lineStart, err := m.PositionOffset(protocol.Position{Line: rng.Start.Line, Character: 0})
59 if err != nil {
60 return "", "", 0
61 }
62 lineEnd, err := m.PositionOffset(protocol.Position{Line: rng.Start.Line + 1, Character: 0})
63 if err != nil {
64 return "", "", 0
65 }
66
67 directive := string(m.Content[lineStart:lineEnd])
68 // (Assumes no leading spaces.)
69 if !strings.HasPrefix(directive, "//go:linkname") {
70 return "", "", 0
71 }
72 // Sometimes source code (typically tests) has another
73 // comment after the directive, trim that away.
74 if i := strings.LastIndex(directive, "//"); i != 0 {
75 directive = strings.TrimSpace(directive[:i])
76 }
77
78 // Looking for pkgpath in '//go:linkname f pkgpath.g'.
79 // (We ignore 1-arg linkname directives.)
80 parts := strings.Fields(directive)
81 if len(parts) != 3 {
82 return "", "", 0
83 }
84
85 // Inside 2nd arg [start, end]?
86 // (Assumes no trailing spaces.)
87 startOffset, endOffset, err := m.RangeOffsets(rng)
88 if err != nil {
89 return "", "", 0
90 }
91 end := lineStart + len(directive)
92 start := end - len(parts[2])
93 if !(start <= startOffset && endOffset <= end) {
94 return "", "", 0
95 }
96 linkname := parts[2]
97
98 // Split the pkg path from the name.
99 dot := strings.LastIndexByte(linkname, '.')
100 if dot < 0 {
101 return "", "", 0
102 }
103
104 return linkname[:dot], linkname[dot+1:], start
105 }
106
107 // findLinkname searches dependencies of packages containing fh for an object
108 // with linker name matching the given package path and name.
109 func findLinkname(ctx context.Context, snapshot *cache.Snapshot, pkgPath PackagePath, name string) (*cache.Package, *parsego.File, token.Pos, error) {
110 // Typically the linkname refers to a forward dependency
111 // or a reverse dependency, but in general it may refer
112 // to any package that is linked with this one.
113 var pkgMeta *metadata.Package
114 metas, err := snapshot.AllMetadata(ctx)
115 if err != nil {
116 return nil, nil, token.NoPos, err
117 }
118 metadata.RemoveIntermediateTestVariants(&metas)
119 for _, meta := range metas {
120 if meta.PkgPath == pkgPath {
121 pkgMeta = meta
122 break
123 }
124 }
125 if pkgMeta == nil {
126 return nil, nil, token.NoPos, fmt.Errorf("cannot find package %q", pkgPath)
127 }
128
129 // When found, type check the desired package (snapshot.TypeCheck in TypecheckFull mode),
130 pkgs, err := snapshot.TypeCheck(ctx, pkgMeta.ID)
131 if err != nil {
132 return nil, nil, token.NoPos, err
133 }
134 pkg := pkgs[0]
135
136 obj := pkg.Types().Scope().Lookup(name)
137 if obj == nil {
138 return nil, nil, token.NoPos, fmt.Errorf("package %q does not define %s", pkgPath, name)
139 }
140
141 pos := obj.Pos()
142 pgf, err := pkg.FileEnclosing(pos)
143 return pkg, pgf, pos, err
144 }