-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.go
65 lines (58 loc) · 1.34 KB
/
ast.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
package main
import (
"bytes"
"go/ast"
"go/printer"
"go/token"
)
func hasTargetParam(params []*ast.Field) bool {
for _, field := range params {
switch fieldType := field.Type.(type) {
case *ast.Ident:
if fieldType.Name == "string" {
return true
}
case *ast.ArrayType:
if eltIdent, ok := fieldType.Elt.(*ast.Ident); ok && eltIdent.Name == "byte" {
return true
}
case *ast.SelectorExpr:
if xIdent, ok := fieldType.X.(*ast.Ident); ok {
if xIdent.Name == "io" && fieldType.Sel.Name == "Reader" {
return true
}
if xIdent.Name == "bytes" && fieldType.Sel.Name == "Buffer" {
return true
}
}
}
}
return false
}
func getHarnessFuncs(filesAST []*ast.File) []*ast.FuncDecl {
var harnessFuncs []*ast.FuncDecl
for _, fileAST := range filesAST {
for _, decl := range fileAST.Decls {
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
if hasTargetParam(funcDecl.Type.Params.List) {
harnessFuncs = append(harnessFuncs, funcDecl)
}
}
}
return harnessFuncs
}
func processAST(filesAST []*ast.File) {
harnessFuncs := getHarnessFuncs(filesAST)
if len(harnessFuncs) == 0 {
return
}
for _, funcDecl := range harnessFuncs {
var buf bytes.Buffer
printer.Fprint(&buf, token.NewFileSet(), funcDecl)
funcName := funcDecl.Name.Name
processGPT(funcName, buf.String())
}
}