4 Star 2 Fork 0

Gitee 极速下载/goreporter

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/360EntSecGroup-Skylar/goreporter
克隆/下载
structcheck.go 4.77 KB
一键复制 编辑 原始数据 按行查看 历史
wangguoliang 提交于 2017-08-17 11:50 +08:00 . -Replace all os.Exit() with return
// 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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
HTML/CSS
1
https://gitee.com/mirrors/goreporter.git
git@gitee.com:mirrors/goreporter.git
mirrors
goreporter
goreporter
df1b20f7c5d0

搜索帮助