-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncinfo.go
156 lines (132 loc) · 3.68 KB
/
funcinfo.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
package openapi2
import (
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
)
type funcInfo struct {
Pkg string `json:"pkg"`
Func string `json:"func"`
Comment string `json:"comment"`
File string `json:"file,omitempty"`
ASTFile *ast.File `json:"ast_file,omitempty"`
FSet *token.FileSet `json:"fset,omitempty"`
Line int `json:"line,omitempty"`
Anonymous bool `json:"anonymous,omitempty"`
Unresolvable bool `json:"unresolvable,omitempty"`
}
func getFuncInfo(fn any) funcInfo {
return getFuncInfoWithSrc(fn, nil)
}
func getFuncInfoWithSrc(fn any, src any) funcInfo {
fi := funcInfo{}
frame := getCallerFrame(fn)
goPathSrc := filepath.Join(os.Getenv("GOPATH"), "src")
if frame == nil {
fi.Unresolvable = true
return fi
}
pkgName := getPkgName(frame.File, src)
funcPath := frame.Func.Name()
idx := strings.Index(funcPath, "/"+pkgName)
if idx > 0 {
fi.Pkg = funcPath[:idx+1+len(pkgName)]
fi.Func = funcPath[idx+2+len(pkgName):]
} else {
fi.Func = funcPath
}
if strings.Index(fi.Func, ".func") > 0 {
fi.Anonymous = true
}
fi.File = frame.File
fi.Line = frame.Line
if filepath.HasPrefix(fi.File, goPathSrc) {
fi.File = fi.File[len(goPathSrc)+1:]
}
if !fi.Unresolvable {
fi.Comment, fi.ASTFile, fi.FSet = getFuncComment(frame.File, frame.Line, src)
}
return fi
}
func getCallerFrame(fn any) *runtime.Frame {
pc := reflect.ValueOf(fn).Pointer()
frames := runtime.CallersFrames([]uintptr{pc})
if frames == nil {
return nil
}
frame, _ := frames.Next()
if frame.Entry == 0 {
return nil
}
return &frame
}
func getPkgName(file string, src any) string {
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, file, src, parser.PackageClauseOnly)
if err != nil {
return ""
}
if astFile.Name == nil {
return ""
}
return astFile.Name.Name
}
func getFuncComment(file string, line int, src any) (string, *ast.File, *token.FileSet) {
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, file, src, parser.ParseComments)
if err != nil {
return "", nil, nil
}
if len(astFile.Comments) == 0 {
return "", astFile, fset
}
line = fixFuncLine(line, fset, astFile)
for _, cmt := range astFile.Comments {
if fset.Position(cmt.End()).Line+1 == line {
return cmt.Text(), astFile, fset
}
}
return "", astFile, fset
}
func pos(fset *token.FileSet, n ast.Node) int {
return fset.Position(n.Pos()).Line
}
// If the compiler inlined the function, we get the line of the return statement rather than the line of the function definition.
// fixFuncLine checks if the specified line contains any return statements
// and if so, returns the line of the function definition that the first return belongs to.
func fixFuncLine(line int, fset *token.FileSet, astFile *ast.File) int {
fixedFuncLine := line
var stack []ast.Node
ast.Inspect(astFile, func(n ast.Node) bool {
if n != nil {
stack = append(stack, n)
} else {
stack = stack[:len(stack)-1]
}
// Check if the current node is on the specified line.
if n == nil || fset.Position(n.Pos()).Line != line {
return true
}
// Check if the current node is a return statement.
_, ok := n.(*ast.ReturnStmt)
if !ok {
return true
}
// Starting at the return statement, go up the node stack until we find the first function definition
for i := len(stack) - 1; i >= 0; i-- {
switch parent := stack[i].(type) {
case *ast.FuncDecl, *ast.FuncLit:
fixedFuncLine = pos(fset, parent)
// Stop looking after the function definition of the first return statement
return false
}
}
return true
})
return fixedFuncLine
}