From 9dfa65ebdb74f35c3bfbdebf6ee87af6073733c0 Mon Sep 17 00:00:00 2001 From: Adin Scannell Date: Thu, 16 Jun 2022 08:56:19 -0700 Subject: [PATCH] checklocks: make behavior configurable. Defaults are the same, but certain behaviors can now be disabled to more easily use the analyzer on its own. Now wrappers, lock inferrence and atomic default analysis can be disabled. For example: ``` go vet -vettool=$HOME/go/bin/checklocks -wrappers=false -inferred=false -atomic=false ./... ``` Fixes #7721 PiperOrigin-RevId: 455393957 --- tools/checklocks/analysis.go | 4 ++++ tools/checklocks/annotations.go | 3 +++ tools/checklocks/checklocks.go | 16 +++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/tools/checklocks/analysis.go b/tools/checklocks/analysis.go index 8a8f18ea5..f1ab91c47 100644 --- a/tools/checklocks/analysis.go +++ b/tools/checklocks/analysis.go @@ -268,6 +268,10 @@ func (pc *passContext) checkGuards(inst almostInst, from ssa.Value, accessObj ty pc.maybeFail(inst.Pos(), "non-atomic write of field %s, writes must still be atomic with locks held (locks: %s)", accessObj.Name(), ls.String()) } case atomicDisallow: + // If atomic analysis is not enabled, skip. + if !enableAtomic { + break + } // Check that this is *not* used atomically. if refs := inst.Referrers(); refs != nil { for _, otherInst := range *refs { diff --git a/tools/checklocks/annotations.go b/tools/checklocks/annotations.go index 950168ee1..9e328edbb 100644 --- a/tools/checklocks/annotations.go +++ b/tools/checklocks/annotations.go @@ -91,6 +91,9 @@ func (pc *passContext) maybeFail(pos token.Pos, fmtStr string, args ...interface if _, ok := pc.exemptions[pc.positionKey(pos)]; ok { return // Ignored, not counted. } + if !enableWrappers && !pos.IsValid() { + return // Ignored, implicit. + } pc.pass.Reportf(pos, fmtStr, args...) } diff --git a/tools/checklocks/checklocks.go b/tools/checklocks/checklocks.go index 939af4239..70921a15c 100644 --- a/tools/checklocks/checklocks.go +++ b/tools/checklocks/checklocks.go @@ -41,6 +41,18 @@ var Analyzer = &analysis.Analyzer{ }, } +var ( + enableInferred = true + enableAtomic = true + enableWrappers = true +) + +func init() { + Analyzer.Flags.BoolVar(&enableInferred, "inferred", true, "enable inferred locks") + Analyzer.Flags.BoolVar(&enableAtomic, "atomic", true, "enable atomic checks") + Analyzer.Flags.BoolVar(&enableWrappers, "wrappers", true, "enable analysis of wrappers") +} + // objectObservations tracks lock correlations. type objectObservations struct { counts map[types.Object]int @@ -187,7 +199,9 @@ func run(pass *analysis.Pass) (interface{}, error) { } // Check for inferred checklocks annotations. - pc.checkInferred() + if enableInferred { + pc.checkInferred() + } // Check for expected failures. pc.checkFailures()