Skip to content

Commit

Permalink
allow disable fact
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvia7788 committed Aug 16, 2022
1 parent ca63215 commit 6227d90
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/contextcheck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

func main() {
singlechecker.Main(contextcheck.NewAnalyzer())
singlechecker.Main(contextcheck.NewAnalyzer(contextcheck.Configuration{}))
}
58 changes: 50 additions & 8 deletions contextcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/types"
"strconv"
"strings"
"sync"

"github.com/gostaticanalysis/analysisutil"
"golang.org/x/tools/go/analysis"
Expand All @@ -13,16 +14,25 @@ import (
"golang.org/x/tools/go/ssa"
)

func NewAnalyzer() *analysis.Analyzer {
return &analysis.Analyzer{
type Configuration struct {
DisableFact bool
}

func NewAnalyzer(cfg Configuration) *analysis.Analyzer {
analyzer := &analysis.Analyzer{
Name: "contextcheck",
Doc: "check the function whether use a non-inherited context",
Run: NewRun(nil),
Run: NewRun(nil, cfg.DisableFact),
Requires: []*analysis.Analyzer{
buildssa.Analyzer,
},
FactTypes: []analysis.Fact{(*ctxFact)(nil)},
}

if !cfg.DisableFact {
analyzer.FactTypes = append(analyzer.FactTypes, (*ctxFact)(nil))
}

return analyzer
}

const (
Expand All @@ -48,6 +58,11 @@ const (
EntryWithHttpHandler // is http handler
)

var (
pkgFactMap = make(map[*types.Package]ctxFact)
pkgFactMu sync.RWMutex
)

type resInfo struct {
Valid bool
Funcs []string
Expand All @@ -68,9 +83,10 @@ type runner struct {
httpReqTyps []types.Type

currentFact ctxFact
disableFact bool
}

func NewRun(pkgs []*packages.Package) func(pass *analysis.Pass) (interface{}, error) {
func NewRun(pkgs []*packages.Package, disableFact bool) func(pass *analysis.Pass) (interface{}, error) {
m := make(map[string]bool)
for _, pkg := range pkgs {
m[strings.Split(pkg.PkgPath, "/")[0]] = true
Expand All @@ -81,7 +97,8 @@ func NewRun(pkgs []*packages.Package) func(pass *analysis.Pass) (interface{}, er
return nil, nil
}

new(runner).run(pass)
r := &runner{disableFact: disableFact}
r.run(pass)
return nil, nil
}
}
Expand Down Expand Up @@ -132,7 +149,11 @@ func (r *runner) run(pass *analysis.Pass) {
}

if len(r.currentFact) > 0 {
pass.ExportPackageFact(&r.currentFact)
if r.disableFact {
setPkgFact(pass.Pkg, r.currentFact)
} else {
pass.ExportPackageFact(&r.currentFact)
}
}
}

Expand Down Expand Up @@ -664,7 +685,13 @@ func (r *runner) getValue(key string, f *ssa.Function) (res resInfo, ok bool) {
}

var fact ctxFact
if r.pass.ImportPackageFact(f.Pkg.Pkg, &fact) {
var got bool
if r.disableFact {
fact, got = getPkgFact(f.Pkg.Pkg)
} else {
got = r.pass.ImportPackageFact(f.Pkg.Pkg, &fact)
}
if got {
res, ok = fact[key]
}
return
Expand All @@ -677,6 +704,21 @@ func (r *runner) setFact(key string, valid bool, funcs ...string) {
}
}

// setPkgFact save fact to mem
func setPkgFact(pkg *types.Package, fact ctxFact) {
pkgFactMu.Lock()
pkgFactMap[pkg] = fact
pkgFactMu.Unlock()
}

// getPkgFact get fact from mem
func getPkgFact(pkg *types.Package) (fact ctxFact, ok bool) {
pkgFactMu.RLock()
fact, ok = pkgFactMap[pkg]
pkgFactMu.RUnlock()
return
}

func reverse(arr1 []string) (arr2 []string) {
l := len(arr1)
if l == 0 {
Expand Down
4 changes: 2 additions & 2 deletions contextcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func Test(t *testing.T) {
log.SetFlags(log.Lshortfile)
testdata := analysistest.TestData()
analyzer := contextcheck.NewAnalyzer()
analyzer.Run = contextcheck.NewRun([]*packages.Package{{PkgPath: "a"}})
analyzer := contextcheck.NewAnalyzer(contextcheck.Configuration{})
analyzer.Run = contextcheck.NewRun([]*packages.Package{{PkgPath: "a"}}, false)
analysistest.Run(t, testdata, analyzer, "a")
}

0 comments on commit 6227d90

Please sign in to comment.