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
This commit is contained in:
Adin Scannell
2022-06-16 08:58:04 -07:00
committed by gVisor bot
parent 8011b8d6d2
commit 9dfa65ebdb
3 changed files with 22 additions and 1 deletions
+4
View File
@@ -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 {
+3
View File
@@ -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...)
}
+15 -1
View File
@@ -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()