Skip to content

fix(checklocks): enhance diagnostics and deduplication in failure reporting #11741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tools/checklocks/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,11 @@ func (pc *passContext) checkBasicBlock(fn *ssa.Function, block *ssa.BasicBlock,

// checkFunction checks a function invocation, typically starting with nil lockState.
func (pc *passContext) checkFunction(call callCommon, fn *ssa.Function, lff *lockFunctionFacts, parent *lockState, force bool) {
// Track current function for diagnostics when SSA positions are missing.
prevFn := pc.curFn
pc.curFn = fn
defer func() { pc.curFn = prevFn }()

defer func() {
// Mark this function as checked. This is used by the top-level
// loop to ensure that all anonymous functions are scanned, if
Expand Down
32 changes: 28 additions & 4 deletions tools/checklocks/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,41 @@ func (pc *passContext) addForce(pos token.Pos) {

// maybeFail checks a potential failure against a specific failure map.
func (pc *passContext) maybeFail(pos token.Pos, fmtStr string, args ...any) {
if fd, ok := pc.failures[pc.positionKey(pos)]; ok {
// Build the message string to allow deduplication.
msg := fmt.Sprintf(fmtStr, args...)

origPos := pos
reportPos := pos
if !pos.IsValid() && pc.curFn != nil {
reportPos = pc.curFn.Pos()
}

key := pc.positionKey(origPos)

// Deduplicate: if we already reported this exact message for the same
// position key, ignore subsequent identical diagnostics so they donʼt
// affect failure-count logic.
if seenMsgs, ok := pc.reported[key]; ok {
if _, dup := seenMsgs[msg]; dup {
return
}
} else {
pc.reported[key] = make(map[string]struct{})
}

pc.reported[key][msg] = struct{}{}

if fd, ok := pc.failures[key]; ok {
fd.seen++
return
}
if _, ok := pc.exemptions[pc.positionKey(pos)]; ok {
if _, ok := pc.exemptions[key]; ok {
return // Ignored, not counted.
}
if !enableWrappers && !pos.IsValid() {
if !enableWrappers && !origPos.IsValid() {
return // Ignored, implicit.
}
pc.pass.Reportf(pos, fmtStr, args...)
pc.pass.Reportf(reportPos, fmtStr, args...)
}

// checkFailure checks for the expected failure counts.
Expand Down
3 changes: 3 additions & 0 deletions tools/checklocks/checklocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ type passContext struct {
failures map[positionKey]*failData
exemptions map[positionKey]struct{}
forced map[positionKey]struct{}
reported map[positionKey]map[string]struct{}
functions map[*ssa.Function]struct{}
curFn *ssa.Function
observations map[types.Object]*objectObservations
}

Expand Down Expand Up @@ -143,6 +145,7 @@ func run(pass *analysis.Pass) (any, error) {
exemptions: make(map[positionKey]struct{}),
forced: make(map[positionKey]struct{}),
functions: make(map[*ssa.Function]struct{}),
reported: make(map[positionKey]map[string]struct{}),
}

// Find all line failure annotations.
Expand Down