|
| 1 | +package contextcheck |
| 2 | + |
| 3 | +import ( |
| 4 | + "go/types" |
| 5 | + "sync/atomic" |
| 6 | + |
| 7 | + "golang.org/x/tools/go/analysis" |
| 8 | + "golang.org/x/tools/go/analysis/passes/buildssa" |
| 9 | + "golang.org/x/tools/go/packages" |
| 10 | + "golang.org/x/tools/go/ssa" |
| 11 | +) |
| 12 | + |
| 13 | +type pkgInfo struct { |
| 14 | + pkgPkg *packages.Package // to find references later |
| 15 | + ssaPkg *ssa.Package // to find func which has been built |
| 16 | + refCnt int32 // reference count |
| 17 | +} |
| 18 | + |
| 19 | +type collector struct { |
| 20 | + m map[string]*pkgInfo |
| 21 | +} |
| 22 | + |
| 23 | +func newCollector(pkgs []*packages.Package) (c *collector) { |
| 24 | + c = &collector{ |
| 25 | + m: make(map[string]*pkgInfo), |
| 26 | + } |
| 27 | + |
| 28 | + // self-reference |
| 29 | + for _, pkg := range pkgs { |
| 30 | + c.m[pkg.PkgPath] = &pkgInfo{ |
| 31 | + pkgPkg: pkg, |
| 32 | + refCnt: 1, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // import reference |
| 37 | + for _, pkg := range pkgs { |
| 38 | + for _, imp := range pkg.Imports { |
| 39 | + if val, ok := c.m[imp.PkgPath]; ok { |
| 40 | + val.refCnt++ |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return |
| 46 | +} |
| 47 | + |
| 48 | +func (c *collector) DecUse(pass *analysis.Pass) { |
| 49 | + curPkg, ok := c.m[pass.Pkg.Path()] |
| 50 | + if !ok { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + if atomic.AddInt32(&curPkg.refCnt, -1) != 0 { |
| 55 | + curPkg.ssaPkg = pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).Pkg |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + var release func(info *pkgInfo) |
| 60 | + release = func(info *pkgInfo) { |
| 61 | + for _, pkg := range info.pkgPkg.Imports { |
| 62 | + if val, ok := c.m[pkg.PkgPath]; ok { |
| 63 | + if atomic.AddInt32(&val.refCnt, -1) == 0 { |
| 64 | + release(val) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + info.pkgPkg = nil |
| 70 | + info.ssaPkg = nil |
| 71 | + } |
| 72 | + release(curPkg) |
| 73 | +} |
| 74 | + |
| 75 | +func (c *collector) GetFunction(f *ssa.Function) (ff *ssa.Function) { |
| 76 | + info, ok := c.m[f.Pkg.Pkg.Path()] |
| 77 | + if !ok { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + // without recv => get by Func |
| 82 | + recv := f.Signature.Recv() |
| 83 | + if recv == nil { |
| 84 | + ff = info.ssaPkg.Func(f.Name()) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + // with recv => find in prog according to type |
| 89 | + ntp, ptp := getNamedType(recv.Type()) |
| 90 | + if ntp == nil { |
| 91 | + return |
| 92 | + } |
| 93 | + sel := info.ssaPkg.Prog.MethodSets.MethodSet(ntp).Lookup(ntp.Obj().Pkg(), f.Name()) |
| 94 | + if sel == nil { |
| 95 | + sel = info.ssaPkg.Prog.MethodSets.MethodSet(ptp).Lookup(ntp.Obj().Pkg(), f.Name()) |
| 96 | + } |
| 97 | + if sel == nil { |
| 98 | + return |
| 99 | + } |
| 100 | + ff = info.ssaPkg.Prog.MethodValue(sel) |
| 101 | + return |
| 102 | +} |
| 103 | + |
| 104 | +func getNamedType(tp types.Type) (ntp *types.Named, ptp *types.Pointer) { |
| 105 | + switch t := tp.(type) { |
| 106 | + case *types.Named: |
| 107 | + ntp = t |
| 108 | + ptp = types.NewPointer(tp) |
| 109 | + case *types.Pointer: |
| 110 | + if n, ok := t.Elem().(*types.Named); ok { |
| 111 | + ntp = n |
| 112 | + ptp = t |
| 113 | + } |
| 114 | + } |
| 115 | + return |
| 116 | +} |
0 commit comments