forked from qax-os/goreporter
/
structcheck.go
207 lines (187 loc) · 4.77 KB
/
structcheck.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
// structcheck
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package structcheck
import (
"flag"
"fmt"
"go/ast"
"go/build"
"os"
"go/types"
"golang.org/x/tools/go/loader"
)
var (
assignmentsOnly = false
loadTestFiles = false
reportExported = false
)
type visitor struct {
prog *loader.Program
pkg *loader.PackageInfo
m map[types.Type]map[string]int
skip map[types.Type]struct{}
}
func (v *visitor) decl(t types.Type, fieldName string) {
if _, ok := v.m[t]; !ok {
v.m[t] = make(map[string]int)
}
if _, ok := v.m[t][fieldName]; !ok {
v.m[t][fieldName] = 0
}
}
func (v *visitor) assignment(t types.Type, fieldName string) {
if _, ok := v.m[t]; !ok {
v.m[t] = make(map[string]int)
}
if _, ok := v.m[t][fieldName]; ok {
v.m[t][fieldName]++
} else {
v.m[t][fieldName] = 1
}
}
func (v *visitor) typeSpec(node *ast.TypeSpec) {
if strukt, ok := node.Type.(*ast.StructType); ok {
t := v.pkg.Info.Defs[node.Name].Type()
for _, f := range strukt.Fields.List {
if len(f.Names) > 0 {
fieldName := f.Names[0].Name
v.decl(t, fieldName)
}
}
}
}
func (v *visitor) typeAndFieldName(expr *ast.SelectorExpr) (types.Type, string, bool) {
selection := v.pkg.Info.Selections[expr]
if selection == nil {
return nil, "", false
}
recv := selection.Recv()
if ptr, ok := recv.(*types.Pointer); ok {
recv = ptr.Elem()
}
return recv, selection.Obj().Name(), true
}
func (v *visitor) assignStmt(node *ast.AssignStmt) {
for _, lhs := range node.Lhs {
var selector *ast.SelectorExpr
switch expr := lhs.(type) {
case *ast.SelectorExpr:
selector = expr
case *ast.IndexExpr:
if expr, ok := expr.X.(*ast.SelectorExpr); ok {
selector = expr
}
}
if selector != nil {
if t, fn, ok := v.typeAndFieldName(selector); ok {
v.assignment(t, fn)
}
}
}
}
func (v *visitor) compositeLiteral(node *ast.CompositeLit) {
t := v.pkg.Info.Types[node.Type].Type
for _, expr := range node.Elts {
if kv, ok := expr.(*ast.KeyValueExpr); ok {
if ident, ok := kv.Key.(*ast.Ident); ok {
v.assignment(t, ident.Name)
}
} else {
// Struct literal with positional values.
// All the fields are assigned.
v.skip[t] = struct{}{}
break
}
}
}
func (v *visitor) Visit(node ast.Node) ast.Visitor {
switch node := node.(type) {
case *ast.TypeSpec:
v.typeSpec(node)
case *ast.AssignStmt:
if assignmentsOnly {
v.assignStmt(node)
}
case *ast.SelectorExpr:
if !assignmentsOnly {
if t, fn, ok := v.typeAndFieldName(node); ok {
v.assignment(t, fn)
}
}
case *ast.CompositeLit:
v.compositeLiteral(node)
}
return v
}
func StructCheck(projectPath string) []string {
flag.Parse()
structChecks := make([]string, 0)
importPaths := []string{projectPath}
if len(importPaths) == 0 {
importPaths = []string{"."}
}
ctx := build.Default
loadcfg := loader.Config{
Build: &ctx,
}
rest, err := loadcfg.FromArgs(importPaths, loadTestFiles)
if err != nil {
fmt.Fprintf(os.Stderr, "could not parse arguments: %s", err)
return structChecks
}
if len(rest) > 0 {
fmt.Fprintf(os.Stderr, "unhandled extra arguments: %v", rest)
return structChecks
}
program, err := loadcfg.Load()
if err != nil {
fmt.Fprintf(os.Stderr, "could not type check: %s", err)
return structChecks
}
for _, pkg := range program.InitialPackages() {
visitor := &visitor{
m: make(map[types.Type]map[string]int),
skip: make(map[types.Type]struct{}),
prog: program,
pkg: pkg,
}
for _, f := range pkg.Files {
ast.Walk(visitor, f)
}
for t := range visitor.m {
if _, skip := visitor.skip[t]; skip {
continue
}
for fieldName, v := range visitor.m[t] {
if !reportExported && ast.IsExported(fieldName) {
continue
}
if v == 0 {
field, _, _ := types.LookupFieldOrMethod(t, false, pkg.Pkg, fieldName)
if fieldName == "XMLName" {
if named, ok := field.Type().(*types.Named); ok && named.Obj().Pkg().Path() == "encoding/xml" {
continue
}
}
pos := program.Fset.Position(field.Pos())
structChecks = append(structChecks, fmt.Sprintf("%s: %s:%d:%d: %s.%s",
pkg.Pkg.Path(), pos.Filename, pos.Line, pos.Column,
types.TypeString(t, nil), fieldName,
))
}
}
}
}
return structChecks
}