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