/
locations.go
207 lines (195 loc) · 6.22 KB
/
locations.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package sourceinfo
import (
"math"
"sync"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
"github.com/jhump/protoreflect/desc/internal"
)
// NB: forked from google.golang.org/protobuf/internal/filedesc
type sourceLocations struct {
protoreflect.SourceLocations
orig []*descriptorpb.SourceCodeInfo_Location
// locs is a list of sourceLocations.
// The SourceLocation.Next field does not need to be populated
// as it will be lazily populated upon first need.
locs []protoreflect.SourceLocation
// fd is the parent file descriptor that these locations are relative to.
// If non-nil, ByDescriptor verifies that the provided descriptor
// is a child of this file descriptor.
fd protoreflect.FileDescriptor
once sync.Once
byPath map[pathKey]int
}
func (p *sourceLocations) Len() int { return len(p.orig) }
func (p *sourceLocations) Get(i int) protoreflect.SourceLocation {
return p.lazyInit().locs[i]
}
func (p *sourceLocations) byKey(k pathKey) protoreflect.SourceLocation {
if i, ok := p.lazyInit().byPath[k]; ok {
return p.locs[i]
}
return protoreflect.SourceLocation{}
}
func (p *sourceLocations) ByPath(path protoreflect.SourcePath) protoreflect.SourceLocation {
return p.byKey(newPathKey(path))
}
func (p *sourceLocations) ByDescriptor(desc protoreflect.Descriptor) protoreflect.SourceLocation {
if p.fd != nil && desc != nil && p.fd != desc.ParentFile() {
return protoreflect.SourceLocation{} // mismatching parent imports
}
var pathArr [16]int32
path := pathArr[:0]
for {
switch desc.(type) {
case protoreflect.FileDescriptor:
// Reverse the path since it was constructed in reverse.
for i, j := 0, len(path)-1; i < j; i, j = i+1, j-1 {
path[i], path[j] = path[j], path[i]
}
return p.byKey(newPathKey(path))
case protoreflect.MessageDescriptor:
path = append(path, int32(desc.Index()))
desc = desc.Parent()
switch desc.(type) {
case protoreflect.FileDescriptor:
path = append(path, int32(internal.File_messagesTag))
case protoreflect.MessageDescriptor:
path = append(path, int32(internal.Message_nestedMessagesTag))
default:
return protoreflect.SourceLocation{}
}
case protoreflect.FieldDescriptor:
isExtension := desc.(protoreflect.FieldDescriptor).IsExtension()
path = append(path, int32(desc.Index()))
desc = desc.Parent()
if isExtension {
switch desc.(type) {
case protoreflect.FileDescriptor:
path = append(path, int32(internal.File_extensionsTag))
case protoreflect.MessageDescriptor:
path = append(path, int32(internal.Message_extensionsTag))
default:
return protoreflect.SourceLocation{}
}
} else {
switch desc.(type) {
case protoreflect.MessageDescriptor:
path = append(path, int32(internal.Message_fieldsTag))
default:
return protoreflect.SourceLocation{}
}
}
case protoreflect.OneofDescriptor:
path = append(path, int32(desc.Index()))
desc = desc.Parent()
switch desc.(type) {
case protoreflect.MessageDescriptor:
path = append(path, int32(internal.Message_oneOfsTag))
default:
return protoreflect.SourceLocation{}
}
case protoreflect.EnumDescriptor:
path = append(path, int32(desc.Index()))
desc = desc.Parent()
switch desc.(type) {
case protoreflect.FileDescriptor:
path = append(path, int32(internal.File_enumsTag))
case protoreflect.MessageDescriptor:
path = append(path, int32(internal.Message_enumsTag))
default:
return protoreflect.SourceLocation{}
}
case protoreflect.EnumValueDescriptor:
path = append(path, int32(desc.Index()))
desc = desc.Parent()
switch desc.(type) {
case protoreflect.EnumDescriptor:
path = append(path, int32(internal.Enum_valuesTag))
default:
return protoreflect.SourceLocation{}
}
case protoreflect.ServiceDescriptor:
path = append(path, int32(desc.Index()))
desc = desc.Parent()
switch desc.(type) {
case protoreflect.FileDescriptor:
path = append(path, int32(internal.File_servicesTag))
default:
return protoreflect.SourceLocation{}
}
case protoreflect.MethodDescriptor:
path = append(path, int32(desc.Index()))
desc = desc.Parent()
switch desc.(type) {
case protoreflect.ServiceDescriptor:
path = append(path, int32(internal.Service_methodsTag))
default:
return protoreflect.SourceLocation{}
}
default:
return protoreflect.SourceLocation{}
}
}
}
func (p *sourceLocations) lazyInit() *sourceLocations {
p.once.Do(func() {
if len(p.orig) > 0 {
p.locs = make([]protoreflect.SourceLocation, len(p.orig))
// Collect all the indexes for a given path.
pathIdxs := make(map[pathKey][]int, len(p.locs))
for i := range p.orig {
l := asSourceLocation(p.orig[i])
p.locs[i] = l
k := newPathKey(l.Path)
pathIdxs[k] = append(pathIdxs[k], i)
}
// Update the next index for all locations.
p.byPath = make(map[pathKey]int, len(p.locs))
for k, idxs := range pathIdxs {
for i := 0; i < len(idxs)-1; i++ {
p.locs[idxs[i]].Next = idxs[i+1]
}
p.locs[idxs[len(idxs)-1]].Next = 0
p.byPath[k] = idxs[0] // record the first location for this path
}
}
})
return p
}
func asSourceLocation(l *descriptorpb.SourceCodeInfo_Location) protoreflect.SourceLocation {
endLine := l.Span[0]
endCol := l.Span[2]
if len(l.Span) > 3 {
endLine = l.Span[2]
endCol = l.Span[3]
}
return protoreflect.SourceLocation{
Path: l.Path,
StartLine: int(l.Span[0]),
StartColumn: int(l.Span[1]),
EndLine: int(endLine),
EndColumn: int(endCol),
LeadingDetachedComments: l.LeadingDetachedComments,
LeadingComments: l.GetLeadingComments(),
TrailingComments: l.GetTrailingComments(),
}
}
// pathKey is a comparable representation of protoreflect.SourcePath.
type pathKey struct {
arr [16]uint8 // first n-1 path segments; last element is the length
str string // used if the path does not fit in arr
}
func newPathKey(p protoreflect.SourcePath) (k pathKey) {
if len(p) < len(k.arr) {
for i, ps := range p {
if ps < 0 || math.MaxUint8 <= ps {
return pathKey{str: p.String()}
}
k.arr[i] = uint8(ps)
}
k.arr[len(k.arr)-1] = uint8(len(p))
return k
}
return pathKey{str: p.String()}
}