-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo_parser.go
More file actions
342 lines (306 loc) · 8.65 KB
/
Copy pathgo_parser.go
File metadata and controls
342 lines (306 loc) · 8.65 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package parser
import (
"go/ast"
goparser "go/parser"
"go/token"
"strings"
"github.com/JSLEEKR/codegraph-go/graph"
)
// parseGo uses go/ast to extract structure from Go source files.
func parseGo(filePath string, source []byte) ([]graph.Node, []graph.Edge, error) {
fset := token.NewFileSet()
f, err := goparser.ParseFile(fset, filePath, source, goparser.ParseComments)
if err != nil {
return nil, nil, err
}
var nodes []graph.Node
var edges []graph.Edge
isTest := isTestFile(filePath, LangGo)
// Track imports for call resolution
importMap := make(map[string]string) // alias/pkgName -> import path
for _, imp := range f.Imports {
path := strings.Trim(imp.Path.Value, `"`)
var alias string
if imp.Name != nil {
alias = imp.Name.Name
} else {
parts := strings.Split(path, "/")
alias = parts[len(parts)-1]
}
importMap[alias] = path
// Add import edge
edges = append(edges, graph.Edge{
Kind: graph.EdgeImportsFrom,
SourceQualified: filePath,
TargetQualified: path,
FilePath: filePath,
Line: fset.Position(imp.Pos()).Line,
})
}
// Track defined names for call resolution
definedFuncs := make(map[string]string) // funcName -> qualifiedName
// Extract type declarations (structs, interfaces)
for _, decl := range f.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.TYPE {
continue
}
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
kind := graph.KindType
switch typeSpec.Type.(type) {
case *ast.StructType:
kind = graph.KindClass
case *ast.InterfaceType:
kind = graph.KindType
}
qn := Qualify(filePath, "", typeSpec.Name.Name)
node := graph.Node{
Kind: kind,
Name: typeSpec.Name.Name,
QualifiedName: qn,
FilePath: filePath,
LineStart: fset.Position(typeSpec.Pos()).Line,
LineEnd: fset.Position(typeSpec.End()).Line,
Language: "go",
}
nodes = append(nodes, node)
edges = append(edges, graph.Edge{
Kind: graph.EdgeContains,
SourceQualified: filePath,
TargetQualified: qn,
FilePath: filePath,
Line: node.LineStart,
})
// Extract interface embedding / struct embedding
if st, ok := typeSpec.Type.(*ast.StructType); ok {
for _, field := range st.Fields.List {
if len(field.Names) == 0 { // embedded field
if ident, ok := field.Type.(*ast.Ident); ok {
edges = append(edges, graph.Edge{
Kind: graph.EdgeInherits,
SourceQualified: qn,
TargetQualified: Qualify(filePath, "", ident.Name),
FilePath: filePath,
Line: fset.Position(field.Pos()).Line,
})
}
}
}
}
if iface, ok := typeSpec.Type.(*ast.InterfaceType); ok {
for _, method := range iface.Methods.List {
if len(method.Names) == 0 { // embedded interface
if ident, ok := method.Type.(*ast.Ident); ok {
edges = append(edges, graph.Edge{
Kind: graph.EdgeInherits,
SourceQualified: qn,
TargetQualified: Qualify(filePath, "", ident.Name),
FilePath: filePath,
Line: fset.Position(method.Pos()).Line,
})
}
}
}
}
}
}
// Extract function declarations
for _, decl := range f.Decls {
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
name := funcDecl.Name.Name
var parentName string
var receiver string
// Check for method receiver
if funcDecl.Recv != nil && len(funcDecl.Recv.List) > 0 {
recv := funcDecl.Recv.List[0]
receiver = extractReceiverType(recv.Type)
parentName = receiver
}
funcKind := graph.KindFunction
if isTest && (strings.HasPrefix(name, "Test") || strings.HasPrefix(name, "Benchmark") || strings.HasPrefix(name, "Example")) {
funcKind = graph.KindTest
}
qn := Qualify(filePath, parentName, name)
definedFuncs[name] = qn
params := extractGoParams(fset, funcDecl.Type.Params)
retType := extractGoResults(fset, funcDecl.Type.Results)
funcNode := graph.Node{
Kind: funcKind,
Name: name,
QualifiedName: qn,
FilePath: filePath,
LineStart: fset.Position(funcDecl.Pos()).Line,
LineEnd: fset.Position(funcDecl.End()).Line,
Language: "go",
ParentName: parentName,
Params: params,
ReturnType: retType,
IsTest: funcKind == graph.KindTest,
}
nodes = append(nodes, funcNode)
// CONTAINS edge
containerQN := filePath
if parentName != "" {
containerQN = Qualify(filePath, "", parentName)
}
edges = append(edges, graph.Edge{
Kind: graph.EdgeContains,
SourceQualified: containerQN,
TargetQualified: qn,
FilePath: filePath,
Line: funcNode.LineStart,
})
// Extract calls within the function body
if funcDecl.Body != nil {
calls := extractGoCalls(fset, funcDecl.Body, filePath, definedFuncs, importMap)
for i := range calls {
calls[i].SourceQualified = qn
}
edges = append(edges, calls...)
// TESTED_BY edges for test functions
if funcNode.IsTest {
for _, call := range calls {
if call.Kind == graph.EdgeCalls {
edges = append(edges, graph.Edge{
Kind: graph.EdgeTestedBy,
SourceQualified: call.TargetQualified,
TargetQualified: qn,
FilePath: filePath,
Line: call.Line,
})
}
}
}
}
}
return nodes, edges, nil
}
func extractReceiverType(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return extractReceiverType(t.X)
case *ast.IndexExpr:
return extractReceiverType(t.X)
case *ast.IndexListExpr:
return extractReceiverType(t.X)
}
return ""
}
func extractGoParams(fset *token.FileSet, fields *ast.FieldList) string {
if fields == nil || len(fields.List) == 0 {
return "()"
}
var parts []string
for _, f := range fields.List {
typeStr := exprToString(f.Type)
if len(f.Names) == 0 {
parts = append(parts, typeStr)
} else {
for _, name := range f.Names {
parts = append(parts, name.Name+" "+typeStr)
}
}
}
return "(" + strings.Join(parts, ", ") + ")"
}
func extractGoResults(fset *token.FileSet, fields *ast.FieldList) string {
if fields == nil || len(fields.List) == 0 {
return ""
}
var parts []string
for _, f := range fields.List {
typeStr := exprToString(f.Type)
if len(f.Names) > 0 {
for _, name := range f.Names {
parts = append(parts, name.Name+" "+typeStr)
}
} else {
parts = append(parts, typeStr)
}
}
if len(parts) == 1 {
return parts[0]
}
return "(" + strings.Join(parts, ", ") + ")"
}
func exprToString(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return "*" + exprToString(t.X)
case *ast.SelectorExpr:
return exprToString(t.X) + "." + t.Sel.Name
case *ast.ArrayType:
return "[]" + exprToString(t.Elt)
case *ast.MapType:
return "map[" + exprToString(t.Key) + "]" + exprToString(t.Value)
case *ast.InterfaceType:
return "interface{}"
case *ast.FuncType:
return "func"
case *ast.Ellipsis:
return "..." + exprToString(t.Elt)
case *ast.ChanType:
return "chan " + exprToString(t.Value)
case *ast.IndexExpr:
return exprToString(t.X) + "[" + exprToString(t.Index) + "]"
case *ast.IndexListExpr:
var indices []string
for _, idx := range t.Indices {
indices = append(indices, exprToString(idx))
}
return exprToString(t.X) + "[" + strings.Join(indices, ", ") + "]"
}
return "any"
}
func extractGoCalls(fset *token.FileSet, body *ast.BlockStmt, filePath string, definedFuncs map[string]string, importMap map[string]string) []graph.Edge {
var calls []graph.Edge
ast.Inspect(body, func(n ast.Node) bool {
callExpr, ok := n.(*ast.CallExpr)
if !ok {
return true
}
line := fset.Position(callExpr.Pos()).Line
var targetQN string
switch fn := callExpr.Fun.(type) {
case *ast.Ident:
// Direct function call: foo()
if qn, ok := definedFuncs[fn.Name]; ok {
targetQN = qn
} else {
targetQN = fn.Name
}
case *ast.SelectorExpr:
// Method or package call: pkg.Func() or obj.Method()
if ident, ok := fn.X.(*ast.Ident); ok {
if importPath, ok := importMap[ident.Name]; ok {
// Package function call
targetQN = importPath + "::" + fn.Sel.Name
} else {
// Method call on local variable
targetQN = Qualify(filePath, ident.Name, fn.Sel.Name)
}
}
}
if targetQN != "" {
calls = append(calls, graph.Edge{
Kind: graph.EdgeCalls,
TargetQualified: targetQN,
FilePath: filePath,
Line: line,
})
}
return true
})
return calls
}