github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/cmd/vet/copylock.go (about)

     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  // This file contains the code to check that locks are not passed by value.
     6  
     7  package main
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"go/ast"
    13  	"go/token"
    14  	"go/types"
    15  )
    16  
    17  func init() {
    18  	register("copylocks",
    19  		"check that locks are not passed by value",
    20  		checkCopyLocks,
    21  		funcDecl, rangeStmt)
    22  }
    23  
    24  // checkCopyLocks checks whether node might
    25  // inadvertently copy a lock.
    26  func checkCopyLocks(f *File, node ast.Node) {
    27  	switch node := node.(type) {
    28  	case *ast.RangeStmt:
    29  		checkCopyLocksRange(f, node)
    30  	case *ast.FuncDecl:
    31  		checkCopyLocksFunc(f, node)
    32  	}
    33  }
    34  
    35  // checkCopyLocksFunc checks whether a function might
    36  // inadvertently copy a lock, by checking whether
    37  // its receiver, parameters, or return values
    38  // are locks.
    39  func checkCopyLocksFunc(f *File, d *ast.FuncDecl) {
    40  	if d.Recv != nil && len(d.Recv.List) > 0 {
    41  		expr := d.Recv.List[0].Type
    42  		if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil {
    43  			f.Badf(expr.Pos(), "%s passes Lock by value: %v", d.Name.Name, path)
    44  		}
    45  	}
    46  
    47  	if d.Type.Params != nil {
    48  		for _, field := range d.Type.Params.List {
    49  			expr := field.Type
    50  			if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil {
    51  				f.Badf(expr.Pos(), "%s passes Lock by value: %v", d.Name.Name, path)
    52  			}
    53  		}
    54  	}
    55  
    56  	if d.Type.Results != nil {
    57  		for _, field := range d.Type.Results.List {
    58  			expr := field.Type
    59  			if path := lockPath(f.pkg.typesPkg, f.pkg.types[expr].Type); path != nil {
    60  				f.Badf(expr.Pos(), "%s returns Lock by value: %v", d.Name.Name, path)
    61  			}
    62  		}
    63  	}
    64  }
    65  
    66  // checkCopyLocksRange checks whether a range statement
    67  // might inadvertently copy a lock by checking whether
    68  // any of the range variables are locks.
    69  func checkCopyLocksRange(f *File, r *ast.RangeStmt) {
    70  	checkCopyLocksRangeVar(f, r.Tok, r.Key)
    71  	checkCopyLocksRangeVar(f, r.Tok, r.Value)
    72  }
    73  
    74  func checkCopyLocksRangeVar(f *File, rtok token.Token, e ast.Expr) {
    75  	if e == nil {
    76  		return
    77  	}
    78  	id, isId := e.(*ast.Ident)
    79  	if isId && id.Name == "_" {
    80  		return
    81  	}
    82  
    83  	var typ types.Type
    84  	if rtok == token.DEFINE {
    85  		if !isId {
    86  			return
    87  		}
    88  		obj := f.pkg.defs[id]
    89  		if obj == nil {
    90  			return
    91  		}
    92  		typ = obj.Type()
    93  	} else {
    94  		typ = f.pkg.types[e].Type
    95  	}
    96  
    97  	if typ == nil {
    98  		return
    99  	}
   100  	if path := lockPath(f.pkg.typesPkg, typ); path != nil {
   101  		f.Badf(e.Pos(), "range var %s copies Lock: %v", f.gofmt(e), path)
   102  	}
   103  }
   104  
   105  type typePath []types.Type
   106  
   107  // String pretty-prints a typePath.
   108  func (path typePath) String() string {
   109  	n := len(path)
   110  	var buf bytes.Buffer
   111  	for i := range path {
   112  		if i > 0 {
   113  			fmt.Fprint(&buf, " contains ")
   114  		}
   115  		// The human-readable path is in reverse order, outermost to innermost.
   116  		fmt.Fprint(&buf, path[n-i-1].String())
   117  	}
   118  	return buf.String()
   119  }
   120  
   121  // lockPath returns a typePath describing the location of a lock value
   122  // contained in typ. If there is no contained lock, it returns nil.
   123  func lockPath(tpkg *types.Package, typ types.Type) typePath {
   124  	if typ == nil {
   125  		return nil
   126  	}
   127  
   128  	// We're only interested in the case in which the underlying
   129  	// type is a struct. (Interfaces and pointers are safe to copy.)
   130  	styp, ok := typ.Underlying().(*types.Struct)
   131  	if !ok {
   132  		return nil
   133  	}
   134  
   135  	// We're looking for cases in which a reference to this type
   136  	// can be locked, but a value cannot. This differentiates
   137  	// embedded interfaces from embedded values.
   138  	if plock := types.NewMethodSet(types.NewPointer(typ)).Lookup(tpkg, "Lock"); plock != nil {
   139  		if lock := types.NewMethodSet(typ).Lookup(tpkg, "Lock"); lock == nil {
   140  			return []types.Type{typ}
   141  		}
   142  	}
   143  
   144  	nfields := styp.NumFields()
   145  	for i := 0; i < nfields; i++ {
   146  		ftyp := styp.Field(i).Type()
   147  		subpath := lockPath(tpkg, ftyp)
   148  		if subpath != nil {
   149  			return append(subpath, typ)
   150  		}
   151  	}
   152  
   153  	return nil
   154  }