forked from goretk/gore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.go
297 lines (248 loc) · 8.47 KB
/
package.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// This file is part of GoRE.
//
// Copyright (C) 2019-2021 GoRE Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package gore
import (
"fmt"
"path"
"runtime/debug"
"sort"
"strings"
)
//go:generate go run ./gen stdpkgs
var (
knownRepos = []string{"golang.org", "github.com", "gitlab.com"}
)
// Package is a representation of a Go package.
type Package struct {
// Name is the name of the package.
Name string `json:"name"`
// Filepath is the extracted file path for the package.
Filepath string `json:"path"`
// Functions is a list of functions that are part of the package.
Functions []*Function `json:"functions"`
// Methods a list of methods that are part of the package.
Methods []*Method `json:"methods"`
}
// GetSourceFiles returns a slice of source files within the package.
// The source files are a representations of the source code files in the package.
func (f *GoFile) GetSourceFiles(p *Package) []*SourceFile {
tmp := make(map[string]*SourceFile)
getSourceFile := func(fileName string) *SourceFile {
sf, ok := tmp[fileName]
if !ok {
return &SourceFile{Name: path.Base(fileName)}
}
return sf
}
// Sort functions and methods by source file.
for _, fn := range p.Functions {
fileName, _, _ := f.pclntab.PCToLine(fn.Offset)
start, end := findSourceLines(fn.Offset, fn.End, f.pclntab)
e := FileEntry{Name: fn.Name, Start: start, End: end}
sf := getSourceFile(fileName)
sf.entries = append(sf.entries, e)
tmp[fileName] = sf
}
for _, m := range p.Methods {
fileName, _, _ := f.pclntab.PCToLine(m.Offset)
start, end := findSourceLines(m.Offset, m.End, f.pclntab)
e := FileEntry{Name: fmt.Sprintf("%s%s", m.Receiver, m.Name), Start: start, End: end}
sf := getSourceFile(fileName)
sf.entries = append(sf.entries, e)
tmp[fileName] = sf
}
// Create final slice and populate it.
files := make([]*SourceFile, len(tmp))
i := 0
for _, sf := range tmp {
files[i] = sf
i++
}
// Sort the file list.
sort.Slice(files, func(i, j int) bool {
return files[i].Name < files[j].Name
})
return files
}
// PackageClass is a type used to indicate the package kind.
type PackageClass uint8
const (
// ClassUnknown is used for packages that could not be classified.
ClassUnknown PackageClass = iota
// ClassSTD is used for packages that are part of the standard library.
ClassSTD
// ClassMain is used for the main package and its subpackages.
ClassMain
// ClassVendor is used for vendor packages.
ClassVendor
// ClassGenerated are used for packages generated by the compiler.
ClassGenerated
)
// PackageClassifier classifies a package to the correct class type.
type PackageClassifier interface {
// Classify performs the classification.
Classify(pkg *Package) PackageClass
}
// NewPathPackageClassifier constructs a new classifier based on the main package's filepath.
func NewPathPackageClassifier(mainPkgFilepath string) *PathPackageClassifier {
return &PathPackageClassifier{
mainFilepath: mainPkgFilepath, mainFolders: []string{
path.Dir(mainPkgFilepath),
path.Clean(mainPkgFilepath),
},
}
}
// PathPackageClassifier can classify the class of a go package.
type PathPackageClassifier struct {
mainFilepath string
mainFolders []string
}
// Classify returns the package class for the package.
func (c *PathPackageClassifier) Classify(pkg *Package) PackageClass {
if pkg.Name == "type" || strings.HasPrefix(pkg.Name, "type..") {
return ClassGenerated
}
if IsStandardLibrary(pkg.Name) {
return ClassSTD
}
if isGeneratedPackage(pkg) {
return ClassGenerated
}
// Detect internal/golang.org/x/net/http2/hpack type/
tmp := strings.Split(pkg.Name, "/golang.org")[0]
if len(tmp) < len(pkg.Name) && IsStandardLibrary(tmp) {
return ClassSTD
}
// cgo packages.
if strings.HasPrefix(pkg.Name, "_cgo_") || strings.HasPrefix(pkg.Name, "x_cgo_") {
return ClassSTD
}
// If the file path contains "@v", it's a 3rd party package.
if strings.Contains(pkg.Filepath, "@v") {
return ClassVendor
}
parentFolder := path.Dir(pkg.Filepath)
if strings.HasPrefix(pkg.Filepath, c.mainFilepath+"/vendor/") ||
strings.HasPrefix(pkg.Filepath, path.Dir(c.mainFilepath)+"/vendor/") ||
strings.HasPrefix(pkg.Filepath, path.Dir(path.Dir(c.mainFilepath))+"/vendor/") {
return ClassVendor
}
for _, folder := range c.mainFolders {
if parentFolder == folder {
return ClassMain
}
}
// If the package name starts with "vendor/" assume it's a vendor package.
if strings.HasPrefix(pkg.Name, "vendor/") {
return ClassVendor
}
// Start with repo url.and has it in the path.
for _, url := range knownRepos {
if strings.HasPrefix(pkg.Name, url) && strings.Contains(pkg.Filepath, url) {
return ClassVendor
}
}
// If the path does not contain the "vendor" in a path but has the main package folder name, assume part of main.
if !strings.Contains(pkg.Filepath, "vendor/") &&
(path.Base(path.Dir(pkg.Filepath)) == path.Base(c.mainFilepath)) {
return ClassMain
}
// Special case for entry point package.
if pkg.Name == "" && path.Base(pkg.Filepath) == "runtime" {
return ClassSTD
}
// At this point, if it's a subpackage of the main assume main.
if strings.HasPrefix(pkg.Filepath, c.mainFilepath) {
return ClassMain
}
// Check if it's the main parent package.
if pkg.Name != "" && (!strings.Contains(pkg.Name, "/") && strings.Contains(c.mainFilepath, pkg.Name)) {
return ClassMain
}
// At this point, if the main package has a file path of "command-line-arguments" and we haven't figured out
// what class it is. We assume it is part of the main package.
if c.mainFilepath == "command-line-arguments" {
return ClassMain
}
return ClassUnknown
}
// IsStandardLibrary returns true if the package is from the standard library.
// Otherwise, false is retuned.
func IsStandardLibrary(pkg string) bool {
_, ok := stdPkgs[pkg]
return ok
}
func isGeneratedPackage(pkg *Package) bool {
if pkg.Filepath == "<autogenerated>" {
return true
}
// Special case for no package name and path of "".
if pkg.Name == "" && pkg.Filepath == "" {
return true
}
// Some internal stuff, classify it as Generated
if pkg.Filepath == "" && (pkg.Name == "__x86" || pkg.Name == "__i686") {
return true
}
return false
}
// NewModPackageClassifier creates a new mod based package classifier.
func NewModPackageClassifier(buildInfo *debug.BuildInfo) *ModPackageClassifier {
return &ModPackageClassifier{modInfo: buildInfo}
}
// ModPackageClassifier uses the mod info extracted from the binary to classify packages.
type ModPackageClassifier struct {
modInfo *debug.BuildInfo
}
// Classify performs the classification.
func (c *ModPackageClassifier) Classify(pkg *Package) PackageClass {
if IsStandardLibrary(pkg.Name) {
return ClassSTD
}
// Main package.
if pkg.Name == "main" {
return ClassMain
}
// If the build info path is not an empty string and the package has the path as a substring, it is part of the main module.
if c.modInfo.Path != "" && (strings.HasPrefix(pkg.Filepath, c.modInfo.Path) || strings.HasPrefix(pkg.Name, c.modInfo.Path)) {
return ClassMain
}
// If the main module path is not an empty string and the package has the path as a substring, it is part of the main module.
if c.modInfo.Main.Path != "" && (strings.HasPrefix(pkg.Filepath, c.modInfo.Main.Path) || strings.HasPrefix(pkg.Name, c.modInfo.Main.Path)) {
return ClassMain
}
// Check if the package is a direct dependency.
for _, dep := range c.modInfo.Deps {
if strings.HasPrefix(pkg.Filepath, dep.Path) || strings.HasPrefix(pkg.Name, dep.Path) {
// If the vendor it matched on has the version of "(devel)", it is treated as part of
// the main module.
if dep.Version == "(devel)" {
return ClassMain
}
return ClassVendor
}
}
if isGeneratedPackage(pkg) {
return ClassGenerated
}
// cgo packages.
if strings.HasPrefix(pkg.Name, "_cgo_") || strings.HasPrefix(pkg.Name, "x_cgo_") {
return ClassSTD
}
// Only indirect dependencies should be left.
return ClassVendor
}