gvisor icon indicating copy to clipboard operation
gvisor copied to clipboard

tools/checklocks: support field comments for guard specifications

Open andrew-d opened this issue 1 year ago • 2 comments
trafficstars

This allows specifying guards in the format:

type foo struct {
    mu  sync.Mutex
    bar int // +checklocks:mu
}

This syntax, while more limited, makes writing lock guards less verbose for structures where field documentation isn't attached to each field. For example, being able to write something like the following with field comments reduces the number of lines in the struct by >25% and is, in my opinion, easier to read.

type widget struct {
    mu sync.Mutex // guards following

    // The following counters track [...]

    success  int // +checklocks:mu
    failure  int // +checklocks:mu
    retries  int // +checklocks:mu
    timeouts int // +checklocks:mu
}

andrew-d avatar Jun 26 '24 17:06 andrew-d

We already support field comments for checklocks, for example: https://github.com/google/gvisor/blob/344f19745f01cb407c714d2a8c95f43022a49ebd/pkg/tcpip/stack/stack.go#L95-L96

ayushr2 avatar Jun 26 '24 18:06 ayushr2

Ah, ^ are considered field.Doc and not field comments. LGTM.

ayushr2 avatar Jun 26 '24 18:06 ayushr2

@ayushr2 Thanks!

andrew-d avatar Jul 02 '24 17:07 andrew-d