-
Notifications
You must be signed in to change notification settings - Fork 15
/
wraperr.go
165 lines (140 loc) · 3.78 KB
/
wraperr.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
package wraperr
import (
"go/ast"
"go/token"
"go/types"
"github.com/gcpug/zagane/zaganeutils"
"github.com/gostaticanalysis/analysisutil"
"github.com/gostaticanalysis/comment"
"github.com/gostaticanalysis/comment/passes/commentmap"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/buildssa"
"golang.org/x/tools/go/ssa"
)
var Analyzer = &analysis.Analyzer{
Name: "wraperr",
Doc: Doc,
Run: new(runner).run,
Requires: []*analysis.Analyzer{
buildssa.Analyzer,
commentmap.Analyzer,
},
}
const Doc = "wraperr finds ReadWriteTransaction calls which returns wrapped errors"
type runner struct {
pass *analysis.Pass
spannerError types.Type
grpcStatusInterface *types.Interface
}
func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
r.pass = pass
r.grpcStatusInterface = newGRPCStatusInterface(pass)
r.spannerError = zaganeutils.TypeOf(pass, "*Error")
cmaps := pass.ResultOf[commentmap.Analyzer].(comment.Maps)
funcs := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs
cliTyp := zaganeutils.TypeOf(pass, "*Client")
rwtx := analysisutil.MethodOf(cliTyp, "ReadWriteTransaction")
if rwtx == nil {
// skip checking
return nil, nil
}
skipFile := map[*ast.File]bool{}
for _, f := range funcs {
if zaganeutils.Unimported(pass, f, skipFile) {
// skip this
continue
}
for _, b := range f.Blocks {
for _, instr := range b.Instrs {
if !analysisutil.Called(instr, nil, rwtx) {
continue
}
if pos := r.wrapped(instr); pos.IsValid() {
l := pass.Fset.File(pos).Line(pos)
if !cmaps.IgnoreLine(pass.Fset, l, "zagane") &&
!cmaps.IgnoreLine(pass.Fset, l, "wraperr") {
pass.Reportf(pos, "must not be wrapped")
}
}
}
}
}
return nil, nil
}
func (r *runner) wrapped(instr ssa.Instruction) token.Pos {
call, ok := instr.(ssa.CallInstruction)
if !ok {
return token.NoPos
}
common := call.Common()
if common == nil {
return token.NoPos
}
if len(common.Args) != 3 {
return token.NoPos
}
switch fnc := common.Args[2].(type) {
case *ssa.MakeClosure:
return r.returnedWrappedErr(fnc.Fn)
}
return token.NoPos
}
func (r *runner) returnedWrappedErr(v ssa.Value) token.Pos {
for _, ret := range analysisutil.Returns(v) {
if len(ret.Results) == 0 {
continue
}
v := ret.Results[len(ret.Results)-1]
if !analysisutil.ImplementsError(v.Type()) {
continue
}
if c, isConst := v.(*ssa.Const); isConst && c.IsNil() {
continue
}
if r.implementsGRPCStatus(v) ||
r.isSpannerError(v) {
continue
}
if !zaganeutils.FromSpanner(v) {
switch v := v.(type) {
case *ssa.MakeInterface:
return v.X.Pos()
case *ssa.Call:
if r.returnedWrappedErr(v.Common().Value) != token.NoPos {
return v.Pos()
}
}
}
}
return token.NoPos
}
func (r *runner) implementsGRPCStatus(v ssa.Value) bool {
if r.grpcStatusInterface == nil {
return false
}
switch v := v.(type) {
case *ssa.MakeInterface:
return types.Implements(v.X.Type(), r.grpcStatusInterface)
}
return types.Implements(v.Type(), r.grpcStatusInterface)
}
func (r *runner) isSpannerError(v ssa.Value) bool {
if r.spannerError == nil {
return false
}
switch v := v.(type) {
case *ssa.MakeInterface:
return types.Identical(v.X.Type(), r.spannerError)
}
return types.Identical(v.Type(), r.spannerError)
}
func newGRPCStatusInterface(pass *analysis.Pass) *types.Interface {
typStatus := analysisutil.TypeOf(pass, "google.golang.org/grpc/status", "*Status")
if typStatus == nil {
return nil
}
ret := types.NewTuple(types.NewParam(token.NoPos, pass.Pkg, "", typStatus))
sig := types.NewSignature(nil, types.NewTuple(), ret, false)
grpcStatusFunc := types.NewFunc(token.NoPos, pass.Pkg, "GRPCStatus", sig)
return types.NewInterfaceType([]*types.Func{grpcStatusFunc}, nil).Complete()
}