GoPLS Viewer

Home|gopls/go/analysis/passes/atomic/atomic.go
1// Copyright 2013 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Package atomic defines an Analyzer that checks for common mistakes
6// using the sync/atomic package.
7package atomic
8
9import (
10    "go/ast"
11    "go/token"
12    "go/types"
13
14    "golang.org/x/tools/go/analysis"
15    "golang.org/x/tools/go/analysis/passes/inspect"
16    "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
17    "golang.org/x/tools/go/ast/inspector"
18)
19
20const Doc = `check for common mistakes using the sync/atomic package
21
22The atomic checker looks for assignment statements of the form:
23
24    x = atomic.AddUint64(&x, 1)
25
26which are not atomic.`
27
28var Analyzer = &analysis.Analyzer{
29    Name:             "atomic",
30    Doc:              Doc,
31    Requires:         []*analysis.Analyzer{inspect.Analyzer},
32    RunDespiteErrorstrue,
33    Run:              run,
34}
35
36func run(pass *analysis.Pass) (interface{}, error) {
37    inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
38
39    nodeFilter := []ast.Node{
40        (*ast.AssignStmt)(nil),
41    }
42    inspect.Preorder(nodeFilter, func(node ast.Node) {
43        n := node.(*ast.AssignStmt)
44        if len(n.Lhs) != len(n.Rhs) {
45            return
46        }
47        if len(n.Lhs) == 1 && n.Tok == token.DEFINE {
48            return
49        }
50
51        for iright := range n.Rhs {
52            callok := right.(*ast.CallExpr)
53            if !ok {
54                continue
55            }
56            selok := call.Fun.(*ast.SelectorExpr)
57            if !ok {
58                continue
59            }
60            pkgIdent_ := sel.X.(*ast.Ident)
61            pkgNameok := pass.TypesInfo.Uses[pkgIdent].(*types.PkgName)
62            if !ok || pkgName.Imported().Path() != "sync/atomic" {
63                continue
64            }
65
66            switch sel.Sel.Name {
67            case "AddInt32""AddInt64""AddUint32""AddUint64""AddUintptr":
68                checkAtomicAddAssignment(passn.Lhs[i], call)
69            }
70        }
71    })
72    return nilnil
73}
74
75// checkAtomicAddAssignment walks the atomic.Add* method calls checking
76// for assigning the return value to the same variable being used in the
77// operation
78func checkAtomicAddAssignment(pass *analysis.Passleft ast.Exprcall *ast.CallExpr) {
79    if len(call.Args) != 2 {
80        return
81    }
82    arg := call.Args[0]
83    broken := false
84
85    gofmt := func(e ast.Exprstring { return analysisutil.Format(pass.Fsete) }
86
87    if uargok := arg.(*ast.UnaryExpr); ok && uarg.Op == token.AND {
88        broken = gofmt(left) == gofmt(uarg.X)
89    } else if starok := left.(*ast.StarExpr); ok {
90        broken = gofmt(star.X) == gofmt(arg)
91    }
92
93    if broken {
94        pass.ReportRangef(left"direct assignment to atomic value")
95    }
96}
97
MembersX
inspector
checkAtomicAddAssignment.broken
ast
token
types
analysis
inspect
run.BlockStmt.RangeStmt_1271.right
checkAtomicAddAssignment.pass
checkAtomicAddAssignment.left
checkAtomicAddAssignment.call
analysisutil
Doc
run
run.pass
run.nodeFilter
run.BlockStmt.RangeStmt_1271.i
checkAtomicAddAssignment
Members
X