-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlint.go
419 lines (365 loc) · 9.71 KB
/
lint.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright (c) 2013 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd.
// Package lint provides the foundation for tools like gosimple.
package lint // import "honnef.co/go/lint"
import (
"bytes"
"fmt"
"go/ast"
"go/constant"
"go/printer"
"go/token"
"go/types"
"io/ioutil"
"path/filepath"
"sort"
"strings"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/ssa"
)
type Ignore struct {
Pattern string
Checks []string
}
type Program struct {
Prog *loader.Program
Packages []*Pkg
}
type Func func(*File)
// Problem represents a problem in some source code.
type Problem struct {
Position token.Position // position in source file
Text string // the prose that describes the problem
// If the problem has a suggested fix (the minority case),
// ReplacementLine is a full replacement for the relevant line of the source file.
ReplacementLine string
}
func (p *Problem) String() string {
return p.Text
}
type ByPosition []Problem
func (p ByPosition) Len() int { return len(p) }
func (p ByPosition) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p ByPosition) Less(i, j int) bool {
pi, pj := p[i].Position, p[j].Position
if pi.Filename != pj.Filename {
return pi.Filename < pj.Filename
}
if pi.Line != pj.Line {
return pi.Line < pj.Line
}
if pi.Column != pj.Column {
return pi.Column < pj.Column
}
return p[i].Text < p[j].Text
}
type Checker interface {
Init(*Program)
Funcs() map[string]Func
}
// A Linter lints Go source code.
type Linter struct {
Checker Checker
Ignores []Ignore
}
func buildPackage(pkg *types.Package, files []*ast.File, info *types.Info, fset *token.FileSet, mode ssa.BuilderMode) *ssa.Package {
prog := ssa.NewProgram(fset, mode)
// Create SSA packages for all imports.
// Order is not significant.
created := make(map[*types.Package]bool)
var createAll func(pkgs []*types.Package)
createAll = func(pkgs []*types.Package) {
for _, p := range pkgs {
if !created[p] {
created[p] = true
prog.CreatePackage(p, nil, nil, true)
createAll(p.Imports())
}
}
}
createAll(pkg.Imports())
// Create and build the primary package.
ssapkg := prog.CreatePackage(pkg, files, info, false)
ssapkg.Build()
return ssapkg
}
func (l *Linter) ignore(f *File, check string) bool {
for _, ig := range l.Ignores {
pkg := f.Pkg.TypesPkg.Path()
if strings.HasSuffix(pkg, "_test") {
pkg = pkg[:len(pkg)-len("_test")]
}
name := filepath.Join(pkg, filepath.Base(f.Filename))
if m, _ := filepath.Match(ig.Pattern, name); !m {
continue
}
for _, c := range ig.Checks {
if m, _ := filepath.Match(c, check); m {
return true
}
}
}
return false
}
func (l *Linter) Lint(lprog *loader.Program) map[string][]Problem {
var pkgs []*Pkg
for _, pkginfo := range lprog.InitialPackages() {
ssapkg := buildPackage(pkginfo.Pkg, pkginfo.Files, &pkginfo.Info, lprog.Fset, ssa.GlobalDebug)
pkg := &Pkg{
TypesPkg: pkginfo.Pkg,
TypesInfo: pkginfo.Info,
SSAPkg: ssapkg,
PkgInfo: pkginfo,
}
pkgs = append(pkgs, pkg)
}
prog := &Program{
Prog: lprog,
Packages: pkgs,
}
l.Checker.Init(prog)
funcs := l.Checker.Funcs()
var keys []string
for k := range funcs {
keys = append(keys, k)
}
sort.Strings(keys)
out := map[string][]Problem{}
for _, pkg := range pkgs {
pkginfo := pkg.PkgInfo
for _, file := range pkginfo.Files {
path := lprog.Fset.Position(file.Pos()).Filename
for _, k := range keys {
f := &File{
Pkg: pkg,
File: file,
Filename: path,
Fset: lprog.Fset,
Program: lprog,
check: k,
}
fn := funcs[k]
if fn == nil {
continue
}
if l.ignore(f, k) {
continue
}
fn(f)
}
}
sort.Sort(ByPosition(pkg.problems))
out[pkginfo.Pkg.Path()] = pkg.problems
}
return out
}
func (f *File) Source() []byte {
if f.src != nil {
return f.src
}
path := f.Fset.Position(f.File.Pos()).Filename
if path != "" {
f.src, _ = ioutil.ReadFile(path)
}
return f.src
}
// pkg represents a package being linted.
type Pkg struct {
TypesPkg *types.Package
TypesInfo types.Info
SSAPkg *ssa.Package
PkgInfo *loader.PackageInfo
problems []Problem
}
// file represents a file being linted.
type File struct {
Pkg *Pkg
File *ast.File
Filename string
Fset *token.FileSet
Program *loader.Program
src []byte
check string
}
func (f *File) IsTest() bool { return strings.HasSuffix(f.Filename, "_test.go") }
type Positioner interface {
Pos() token.Pos
}
func (f *File) Errorf(n Positioner, format string, args ...interface{}) *Problem {
pos := f.Fset.Position(n.Pos())
return f.Pkg.errorfAt(pos, f.check, format, args...)
}
func (p *Pkg) errorfAt(pos token.Position, check string, format string, args ...interface{}) *Problem {
problem := Problem{
Position: pos,
}
problem.Text = fmt.Sprintf(format, args...) + fmt.Sprintf(" (%s)", check)
p.problems = append(p.problems, problem)
return &p.problems[len(p.problems)-1]
}
func (p *Pkg) IsNamedType(typ types.Type, importPath, name string) bool {
n, ok := typ.(*types.Named)
if !ok {
return false
}
tn := n.Obj()
return tn != nil && tn.Pkg() != nil && tn.Pkg().Path() == importPath && tn.Name() == name
}
func (f *File) IsMain() bool {
return f.File.Name.Name == "main"
}
// exportedType reports whether typ is an exported type.
// It is imprecise, and will err on the side of returning true,
// such as for composite types.
func ExportedType(typ types.Type) bool {
switch T := typ.(type) {
case *types.Named:
// Builtin types have no package.
return T.Obj().Pkg() == nil || T.Obj().Exported()
case *types.Map:
return ExportedType(T.Key()) && ExportedType(T.Elem())
case interface {
Elem() types.Type
}: // array, slice, pointer, chan
return ExportedType(T.Elem())
}
// Be conservative about other types, such as struct, interface, etc.
return true
}
func ReceiverType(fn *ast.FuncDecl) string {
switch e := fn.Recv.List[0].Type.(type) {
case *ast.Ident:
return e.Name
case *ast.StarExpr:
return e.X.(*ast.Ident).Name
}
panic(fmt.Sprintf("unknown method receiver AST node type %T", fn.Recv.List[0].Type))
}
func (f *File) Walk(fn func(ast.Node) bool) {
ast.Inspect(f.File, fn)
}
func (f *File) Render(x interface{}) string {
var buf bytes.Buffer
if err := printer.Fprint(&buf, f.Fset, x); err != nil {
panic(err)
}
return buf.String()
}
func (f *File) RenderArgs(args []ast.Expr) string {
var ss []string
for _, arg := range args {
ss = append(ss, f.Render(arg))
}
return strings.Join(ss, ", ")
}
func IsIdent(expr ast.Expr, ident string) bool {
id, ok := expr.(*ast.Ident)
return ok && id.Name == ident
}
// isBlank returns whether id is the blank identifier "_".
// If id == nil, the answer is false.
func IsBlank(id ast.Expr) bool {
ident, ok := id.(*ast.Ident)
return ok && ident.Name == "_"
}
func IsPkgDot(expr ast.Expr, pkg, name string) bool {
sel, ok := expr.(*ast.SelectorExpr)
return ok && IsIdent(sel.X, pkg) && IsIdent(sel.Sel, name)
}
func IsZero(expr ast.Expr) bool {
lit, ok := expr.(*ast.BasicLit)
return ok && lit.Kind == token.INT && lit.Value == "0"
}
func IsOne(expr ast.Expr) bool {
lit, ok := expr.(*ast.BasicLit)
return ok && lit.Kind == token.INT && lit.Value == "1"
}
func IsNil(expr ast.Expr) bool {
// FIXME(dominikh): use type information
id, ok := expr.(*ast.Ident)
return ok && id.Name == "nil"
}
var basicTypeKinds = map[types.BasicKind]string{
types.UntypedBool: "bool",
types.UntypedInt: "int",
types.UntypedRune: "rune",
types.UntypedFloat: "float64",
types.UntypedComplex: "complex128",
types.UntypedString: "string",
}
// isUntypedConst reports whether expr is an untyped constant,
// and indicates what its default type is.
// scope may be nil.
func (f *File) IsUntypedConst(expr ast.Expr) (defType string, ok bool) {
// Re-evaluate expr outside of its context to see if it's untyped.
// (An expr evaluated within, for example, an assignment context will get the type of the LHS.)
exprStr := f.Render(expr)
tv, err := types.Eval(f.Fset, f.Pkg.TypesPkg, expr.Pos(), exprStr)
if err != nil {
return "", false
}
if b, ok := tv.Type.(*types.Basic); ok {
if dt, ok := basicTypeKinds[b.Kind()]; ok {
return dt, true
}
}
return "", false
}
func (f *File) BoolConst(expr ast.Expr) bool {
val := f.Pkg.TypesInfo.ObjectOf(expr.(*ast.Ident)).(*types.Const).Val()
return constant.BoolVal(val)
}
func (f *File) IsBoolConst(expr ast.Expr) bool {
// We explicitly don't support typed bools because more often than
// not, custom bool types are used as binary enums and the
// explicit comparison is desired.
ident, ok := expr.(*ast.Ident)
if !ok {
return false
}
obj := f.Pkg.TypesInfo.ObjectOf(ident)
c, ok := obj.(*types.Const)
if !ok {
return false
}
basic, ok := c.Type().(*types.Basic)
if !ok {
return false
}
if basic.Kind() != types.UntypedBool && basic.Kind() != types.Bool {
return false
}
return true
}
func ExprToInt(expr ast.Expr) (string, bool) {
switch y := expr.(type) {
case *ast.BasicLit:
if y.Kind != token.INT {
return "", false
}
return y.Value, true
case *ast.UnaryExpr:
if y.Op != token.SUB && y.Op != token.ADD {
return "", false
}
x, ok := y.X.(*ast.BasicLit)
if !ok {
return "", false
}
if x.Kind != token.INT {
return "", false
}
v := constant.MakeFromLiteral(x.Value, x.Kind, 0)
return constant.UnaryOp(y.Op, v, 0).String(), true
default:
return "", false
}
}
func (f *File) EnclosingSSAFunction(node Positioner) *ssa.Function {
path, _ := astutil.PathEnclosingInterval(f.File, node.Pos(), node.Pos())
return ssa.EnclosingFunction(f.Pkg.SSAPkg, path)
}