forked from go-delve/delve
/
typecheckrpc.go
208 lines (180 loc) · 4.79 KB
/
typecheckrpc.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
package main
// This program checks the types of the arguments of calls to
// the API in service/rpc2/client.go (done using rpc2.(*Client).call)
// against the declared types of API methods in srvice/rpc2/server.go
import (
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
func findRPCDir() string {
const parent = ".."
RPCDir := "service/rpc2"
for depth := 0; depth < 10; depth++ {
if _, err := os.Stat(RPCDir); err == nil {
break
}
RPCDir = filepath.Join(parent, RPCDir)
}
return RPCDir
}
func parseFiles(path string) (*token.FileSet, *types.Package, types.Info, *ast.File) {
fset := token.NewFileSet()
files := []*ast.File{}
for _, name := range []string{"server.go", "client.go"} {
f, err := parser.ParseFile(fset, filepath.Join(path, name), nil, 0)
if err != nil {
log.Fatal(err)
}
files = append(files, f)
}
info := types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
}
var conf types.Config
conf.Importer = importer.Default()
pkg, err := conf.Check(path, fset, files, &info)
if err != nil {
log.Fatal(err)
}
return fset, pkg, info, files[1]
}
func getMethods(pkg *types.Package, typename string) map[string]*types.Func {
r := make(map[string]*types.Func)
mset := types.NewMethodSet(types.NewPointer(pkg.Scope().Lookup(typename).Type()))
for i := 0; i < mset.Len(); i++ {
fn := mset.At(i).Obj().(*types.Func)
r[fn.Name()] = fn
}
return r
}
func publicMethodOf(decl ast.Decl, receiver string) *ast.FuncDecl {
fndecl, isfunc := decl.(*ast.FuncDecl)
if !isfunc {
return nil
}
if fndecl.Name.Name[0] >= 'a' && fndecl.Name.Name[0] <= 'z' {
return nil
}
if fndecl.Recv == nil || len(fndecl.Recv.List) != 1 {
return nil
}
starexpr, isstar := fndecl.Recv.List[0].Type.(*ast.StarExpr)
if !isstar {
return nil
}
identexpr, isident := starexpr.X.(*ast.Ident)
if !isident || identexpr.Name != receiver {
return nil
}
if fndecl.Body == nil {
return nil
}
return fndecl
}
func findCallCall(fndecl *ast.FuncDecl) *ast.CallExpr {
for _, stmt := range fndecl.Body.List {
var x ast.Expr = nil
switch s := stmt.(type) {
case *ast.AssignStmt:
if len(s.Rhs) == 1 {
x = s.Rhs[0]
}
case *ast.ReturnStmt:
if len(s.Results) == 1 {
x = s.Results[0]
}
case *ast.ExprStmt:
x = s.X
}
callx, iscall := x.(*ast.CallExpr)
if !iscall {
continue
}
fun, issel := callx.Fun.(*ast.SelectorExpr)
if !issel || fun.Sel.Name != "call" {
continue
}
return callx
}
return nil
}
func qf(*types.Package) string {
return ""
}
func main() {
RPCDir := findRPCDir()
fset, pkg, info, clientAst := parseFiles(RPCDir)
serverMethods := getMethods(pkg, "RPCServer")
_ = serverMethods
errcount := 0
for _, decl := range clientAst.Decls {
fndecl := publicMethodOf(decl, "RPCClient")
if fndecl == nil {
continue
}
if fndecl.Name.Name == "Continue" {
// complex function, skip check
continue
}
callx := findCallCall(fndecl)
if callx == nil {
log.Printf("%s: could not find RPC call", fset.Position(fndecl.Pos()))
errcount++
continue
}
if len(callx.Args) != 3 {
log.Printf("%s: wrong number of arguments for RPC call", fset.Position(callx.Pos()))
errcount++
continue
}
arg0, arg0islit := callx.Args[0].(*ast.BasicLit)
arg1 := callx.Args[1]
arg2 := callx.Args[2]
if !arg0islit || arg0.Kind != token.STRING {
continue
}
name, _ := strconv.Unquote(arg0.Value)
serverMethod := serverMethods[name]
if serverMethod == nil {
log.Printf("%s: could not find RPC method %q", fset.Position(callx.Pos()), name)
errcount++
continue
}
params := serverMethod.Type().(*types.Signature).Params()
if a, e := info.TypeOf(arg1), params.At(0).Type(); !types.AssignableTo(a, e) {
log.Printf("%s: wrong type of first argument %s, expected %s", fset.Position(callx.Pos()), types.TypeString(a, qf), types.TypeString(e, qf))
errcount++
continue
}
if !strings.HasSuffix(params.At(1).Type().String(), "/service.RPCCallback") {
if a, e := info.TypeOf(arg2), params.At(1).Type(); !types.AssignableTo(a, e) {
log.Printf("%s: wrong type of second argument %s, expected %s", fset.Position(callx.Pos()), types.TypeString(a, qf), types.TypeString(e, qf))
errcount++
continue
}
}
if clit, ok := arg1.(*ast.CompositeLit); ok {
typ := params.At(0).Type()
st := typ.Underlying().(*types.Struct)
if len(clit.Elts) != st.NumFields() && types.TypeString(typ, qf) != "DebuggerCommand" {
log.Printf("%s: wrong number of fields in first argument's literal %d, expected %d", fset.Position(callx.Pos()), len(clit.Elts), st.NumFields())
errcount++
continue
}
}
}
if errcount > 0 {
log.Printf("%d errors", errcount)
os.Exit(1)
}
}