-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconsensuswarn.go
267 lines (252 loc) · 6.1 KB
/
consensuswarn.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
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
package main
import (
"errors"
"fmt"
"go/ast"
"go/token"
"go/types"
"io"
"path/filepath"
"sort"
"strings"
"github.com/sourcegraph/go-diff/diff"
"golang.org/x/tools/go/packages"
)
// rootFunction is a representation of a method such as
//
// example.com/pkg/path.Type.Method
//
// or a function such as
//
// example.com/pkg/path.Function
type rootFunction struct {
typ string
fun string
}
// runCheck reports the patch hunks that touches any method or function reachable from
// roots.
func runCheck(fset *token.FileSet, dir string, patch io.Reader, roots []string) ([]Hunk, error) {
cfg := &packages.Config{
Fset: fset,
Mode: packages.NeedImports | packages.NeedSyntax | packages.NeedDeps | packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo,
}
var pkgPatterns []string
rootMap := make(map[rootFunction]bool)
for _, root := range roots {
var f rootFunction
lastSlash := strings.LastIndex(root, "/")
idx := strings.LastIndex(root, ".")
if idx <= lastSlash {
return nil, fmt.Errorf("malformed function or method: %s", root)
}
f.fun = root[idx+1:]
root = root[:idx]
f.typ = root
if idx := strings.LastIndex(root, "."); idx > lastSlash {
root = root[:idx]
}
pkgPatterns = append(pkgPatterns, "pattern="+root)
rootMap[f] = true
}
pkgs, err := packages.Load(cfg, pkgPatterns...)
if err != nil {
return nil, err
}
state := &analyzerState{
fset: fset,
funcs: make(map[*types.Func]BodyInfo),
}
imported := make(map[*packages.Package]bool)
var rootFuncs []*types.Func
var addPkg func(pkg *packages.Package)
addPkg = func(pkg *packages.Package) {
if imported[pkg] {
return
}
imported[pkg] = true
for _, f := range pkg.Syntax {
for _, decl := range f.Decls {
switch decl := decl.(type) {
case *ast.FuncDecl:
td := pkg.TypesInfo.Defs[decl.Name].(*types.Func)
inf := BodyInfo{decl, pkg.TypesInfo}
state.funcs[td] = inf
rf := rootFunction{fun: td.Name()}
if recv := td.Type().(*types.Signature).Recv(); recv != nil {
t := recv.Type()
if pt, isPointer := t.(*types.Pointer); isPointer {
t = pt.Elem()
}
rf.typ = types.TypeString(t, nil)
} else {
rf.typ = pkg.PkgPath
}
if rootMap[rf] {
delete(rootMap, rf)
rootFuncs = append(rootFuncs, td)
}
}
}
}
for _, pkg := range pkg.Imports {
addPkg(pkg)
}
}
for _, pkg := range pkgs {
if len(pkg.Errors) > 0 {
packages.PrintErrors(pkgs)
return nil, errors.New("failed to load packages")
}
addPkg(pkg)
}
var missing []string
for n := range rootMap {
missing = append(missing, n.typ+"."+n.fun)
}
if len(missing) > 0 {
return nil, fmt.Errorf("missing roots: %v", strings.Join(missing, ","))
}
p, err := parsePatch(dir, patch)
if err != nil {
return nil, err
}
for _, root := range rootFuncs {
inspect(state, p, root, nil)
}
var stateHunks []Hunk
for _, hunk := range p {
if len(hunk.stack) > 0 {
stateHunks = append(stateHunks, hunk)
}
}
return stateHunks, nil
}
func parsePatch(dir string, r io.Reader) (Patch, error) {
diffs := diff.NewMultiFileDiffReader(r)
var p Patch
for {
d, err := diffs.ReadFile()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return Patch{}, fmt.Errorf("failed to read diff: %v", err)
}
// The original filename without the prefix
origName := strings.TrimPrefix(d.OrigName, "a/")
// Make it absolute.
absName := filepath.Join(dir, origName)
for _, hunk := range d.Hunks {
startLine := int(hunk.OrigStartLine)
p = append(p, Hunk{
hunk: hunk,
relFile: origName,
file: absName,
startLine: startLine,
endLine: startLine + int(hunk.OrigLines),
})
}
}
sort.Slice(p, func(i, j int) bool {
h1, h2 := p[i], p[j]
switch strings.Compare(h1.file, h2.file) {
case -1:
return true
case +1:
return false
default:
return h1.startLine <= h2.startLine
}
})
return p, nil
}
// Patch is a slice of Hunks, sorted by path then starting line.
type Patch []Hunk
type Hunk struct {
file string
relFile string
startLine int
endLine int
hunk *diff.Hunk
stack []stackEntry
}
type stackEntry struct {
fun *types.Func
pos token.Pos
}
// Mark any hunk that overlaps the specified range of lines in file. The stack argument
// is used when printing out clashes.
func (p Patch) Mark(stack []stackEntry, file string, startLine, endLine int) {
idx := sort.Search(len(p), func(i int) bool {
h := p[i]
switch strings.Compare(file, h.file) {
case -1:
return true
case +1:
return false
default:
return startLine <= h.endLine
}
})
for i := idx; i < len(p); i++ {
h := p[i]
if h.file != file || h.startLine > endLine {
break
}
// Record the stack, but only if it is shorter than any previous stack.
if len(p[i].stack) == 0 || len(p[i].stack) > len(stack) {
p[i].stack = append(p[i].stack[:0], stack...)
}
}
}
type BodyInfo struct {
fun *ast.FuncDecl
info *types.Info
}
type analyzerState struct {
fset *token.FileSet
funcs map[*types.Func]BodyInfo
}
func inspect(state *analyzerState, patch Patch, def *types.Func, stack []stackEntry) {
inf, ok := state.funcs[def]
if !ok || inf.fun.Body == nil {
return
}
delete(state.funcs, def)
stack = append(stack, stackEntry{fun: def, pos: inf.fun.Pos()})
start := state.fset.PositionFor(inf.fun.Body.Pos(), false)
end := state.fset.PositionFor(inf.fun.Body.End(), false)
if start.IsValid() && end.IsValid() {
patch.Mark(stack, start.Filename, start.Line, end.Line)
}
ast.Inspect(inf.fun.Body, func(n ast.Node) bool {
switch n := n.(type) {
case *ast.CallExpr:
var id *ast.Ident
switch fun := n.Fun.(type) {
case *ast.Ident:
id = fun
case *ast.SelectorExpr:
id = fun.Sel
}
switch t := inf.info.Uses[id].(type) {
case *types.Func:
inspect(state, patch, t, stack)
}
}
return true
})
}
type stringSlice []string
func (ss *stringSlice) String() string {
return strings.Join(*ss, ",")
}
func (ss *stringSlice) Set(flag string) error {
for _, name := range strings.Split(flag, ",") {
if len(name) == 0 {
return errors.New("empty string")
}
*ss = append(*ss, name)
}
return nil
}