Source file src/cmd/vendor/golang.org/x/tools/go/analysis/passes/internal/analysisutil/util.go

     1  // Copyright 2018 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 analysisutil defines various helper functions
     6  // used by two or more packages beneath go/analysis.
     7  package analysisutil
     8  
     9  import (
    10  	"go/ast"
    11  	"go/token"
    12  	"go/types"
    13  	"os"
    14  
    15  	"golang.org/x/tools/go/analysis"
    16  	"golang.org/x/tools/internal/analysisinternal"
    17  )
    18  
    19  // HasSideEffects reports whether evaluation of e has side effects.
    20  func HasSideEffects(info *types.Info, e ast.Expr) bool {
    21  	safe := true
    22  	ast.Inspect(e, func(node ast.Node) bool {
    23  		switch n := node.(type) {
    24  		case *ast.CallExpr:
    25  			typVal := info.Types[n.Fun]
    26  			switch {
    27  			case typVal.IsType():
    28  				// Type conversion, which is safe.
    29  			case typVal.IsBuiltin():
    30  				// Builtin func, conservatively assumed to not
    31  				// be safe for now.
    32  				safe = false
    33  				return false
    34  			default:
    35  				// A non-builtin func or method call.
    36  				// Conservatively assume that all of them have
    37  				// side effects for now.
    38  				safe = false
    39  				return false
    40  			}
    41  		case *ast.UnaryExpr:
    42  			if n.Op == token.ARROW {
    43  				safe = false
    44  				return false
    45  			}
    46  		}
    47  		return true
    48  	})
    49  	return !safe
    50  }
    51  
    52  // ReadFile reads a file and adds it to the FileSet
    53  // so that we can report errors against it using lineStart.
    54  func ReadFile(pass *analysis.Pass, filename string) ([]byte, *token.File, error) {
    55  	readFile := pass.ReadFile
    56  	if readFile == nil {
    57  		readFile = os.ReadFile
    58  	}
    59  	content, err := readFile(filename)
    60  	if err != nil {
    61  		return nil, nil, err
    62  	}
    63  	tf := pass.Fset.AddFile(filename, -1, len(content))
    64  	tf.SetLinesForContent(content)
    65  	return content, tf, nil
    66  }
    67  
    68  // LineStart returns the position of the start of the specified line
    69  // within file f, or NoPos if there is no line of that number.
    70  func LineStart(f *token.File, line int) token.Pos {
    71  	// Use binary search to find the start offset of this line.
    72  	//
    73  	// TODO(adonovan): eventually replace this function with the
    74  	// simpler and more efficient (*go/token.File).LineStart, added
    75  	// in go1.12.
    76  
    77  	min := 0        // inclusive
    78  	max := f.Size() // exclusive
    79  	for {
    80  		offset := (min + max) / 2
    81  		pos := f.Pos(offset)
    82  		posn := f.Position(pos)
    83  		if posn.Line == line {
    84  			return pos - (token.Pos(posn.Column) - 1)
    85  		}
    86  
    87  		if min+1 >= max {
    88  			return token.NoPos
    89  		}
    90  
    91  		if posn.Line < line {
    92  			min = offset
    93  		} else {
    94  			max = offset
    95  		}
    96  	}
    97  }
    98  
    99  var MustExtractDoc = analysisinternal.MustExtractDoc
   100  

View as plain text