1 Star 0 Fork 0

Laomo. / delve

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
typecheckrpc.go 4.79 KB
一键复制 编辑 原始数据 按行查看 历史
aarzilli 提交于 2016-06-19 08:43 . Replaced net/rpc with custom version
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)
}
}
1
https://gitee.com/LaomoBK/delve.git
git@gitee.com:LaomoBK/delve.git
LaomoBK
delve
delve
v1.1.0

搜索帮助