Skip to content

Commit 6227d90

Browse files
committed
allow disable fact
1 parent ca63215 commit 6227d90

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

cmd/contextcheck/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import (
66
)
77

88
func main() {
9-
singlechecker.Main(contextcheck.NewAnalyzer())
9+
singlechecker.Main(contextcheck.NewAnalyzer(contextcheck.Configuration{}))
1010
}

contextcheck.go

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"go/types"
66
"strconv"
77
"strings"
8+
"sync"
89

910
"github.com/gostaticanalysis/analysisutil"
1011
"golang.org/x/tools/go/analysis"
@@ -13,16 +14,25 @@ import (
1314
"golang.org/x/tools/go/ssa"
1415
)
1516

16-
func NewAnalyzer() *analysis.Analyzer {
17-
return &analysis.Analyzer{
17+
type Configuration struct {
18+
DisableFact bool
19+
}
20+
21+
func NewAnalyzer(cfg Configuration) *analysis.Analyzer {
22+
analyzer := &analysis.Analyzer{
1823
Name: "contextcheck",
1924
Doc: "check the function whether use a non-inherited context",
20-
Run: NewRun(nil),
25+
Run: NewRun(nil, cfg.DisableFact),
2126
Requires: []*analysis.Analyzer{
2227
buildssa.Analyzer,
2328
},
24-
FactTypes: []analysis.Fact{(*ctxFact)(nil)},
2529
}
30+
31+
if !cfg.DisableFact {
32+
analyzer.FactTypes = append(analyzer.FactTypes, (*ctxFact)(nil))
33+
}
34+
35+
return analyzer
2636
}
2737

2838
const (
@@ -48,6 +58,11 @@ const (
4858
EntryWithHttpHandler // is http handler
4959
)
5060

61+
var (
62+
pkgFactMap = make(map[*types.Package]ctxFact)
63+
pkgFactMu sync.RWMutex
64+
)
65+
5166
type resInfo struct {
5267
Valid bool
5368
Funcs []string
@@ -68,9 +83,10 @@ type runner struct {
6883
httpReqTyps []types.Type
6984

7085
currentFact ctxFact
86+
disableFact bool
7187
}
7288

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

84-
new(runner).run(pass)
100+
r := &runner{disableFact: disableFact}
101+
r.run(pass)
85102
return nil, nil
86103
}
87104
}
@@ -132,7 +149,11 @@ func (r *runner) run(pass *analysis.Pass) {
132149
}
133150

134151
if len(r.currentFact) > 0 {
135-
pass.ExportPackageFact(&r.currentFact)
152+
if r.disableFact {
153+
setPkgFact(pass.Pkg, r.currentFact)
154+
} else {
155+
pass.ExportPackageFact(&r.currentFact)
156+
}
136157
}
137158
}
138159

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

666687
var fact ctxFact
667-
if r.pass.ImportPackageFact(f.Pkg.Pkg, &fact) {
688+
var got bool
689+
if r.disableFact {
690+
fact, got = getPkgFact(f.Pkg.Pkg)
691+
} else {
692+
got = r.pass.ImportPackageFact(f.Pkg.Pkg, &fact)
693+
}
694+
if got {
668695
res, ok = fact[key]
669696
}
670697
return
@@ -677,6 +704,21 @@ func (r *runner) setFact(key string, valid bool, funcs ...string) {
677704
}
678705
}
679706

707+
// setPkgFact save fact to mem
708+
func setPkgFact(pkg *types.Package, fact ctxFact) {
709+
pkgFactMu.Lock()
710+
pkgFactMap[pkg] = fact
711+
pkgFactMu.Unlock()
712+
}
713+
714+
// getPkgFact get fact from mem
715+
func getPkgFact(pkg *types.Package) (fact ctxFact, ok bool) {
716+
pkgFactMu.RLock()
717+
fact, ok = pkgFactMap[pkg]
718+
pkgFactMu.RUnlock()
719+
return
720+
}
721+
680722
func reverse(arr1 []string) (arr2 []string) {
681723
l := len(arr1)
682724
if l == 0 {

contextcheck_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func Test(t *testing.T) {
1313
log.SetFlags(log.Lshortfile)
1414
testdata := analysistest.TestData()
15-
analyzer := contextcheck.NewAnalyzer()
16-
analyzer.Run = contextcheck.NewRun([]*packages.Package{{PkgPath: "a"}})
15+
analyzer := contextcheck.NewAnalyzer(contextcheck.Configuration{})
16+
analyzer.Run = contextcheck.NewRun([]*packages.Package{{PkgPath: "a"}}, false)
1717
analysistest.Run(t, testdata, analyzer, "a")
1818
}

0 commit comments

Comments
 (0)