1 // Package analysisutil defines various helper functions
2 // used by two or more packages beneath go/analysis.
14 // Format returns a string representation of the expression.
15 func Format(fset *token.FileSet, x ast.Expr) string {
17 printer.Fprint(&b, fset, x)
21 // HasSideEffects reports whether evaluation of e has side effects.
22 func HasSideEffects(info *types.Info, e ast.Expr) bool {
24 ast.Inspect(e, func(node ast.Node) bool {
25 switch n := node.(type) {
27 typVal := info.Types[n.Fun]
30 // Type conversion, which is safe.
31 case typVal.IsBuiltin():
32 // Builtin func, conservatively assumed to not
37 // A non-builtin func or method call.
38 // Conservatively assume that all of them have
39 // side effects for now.
44 if n.Op == token.ARROW {
54 // Unparen returns e with any enclosing parentheses stripped.
55 func Unparen(e ast.Expr) ast.Expr {
57 p, ok := e.(*ast.ParenExpr)
65 // ReadFile reads a file and adds it to the FileSet
66 // so that we can report errors against it using lineStart.
67 func ReadFile(fset *token.FileSet, filename string) ([]byte, *token.File, error) {
68 content, err := ioutil.ReadFile(filename)
72 tf := fset.AddFile(filename, -1, len(content))
73 tf.SetLinesForContent(content)
74 return content, tf, nil
77 // LineStart returns the position of the start of the specified line
78 // within file f, or NoPos if there is no line of that number.
79 func LineStart(f *token.File, line int) token.Pos {
80 // Use binary search to find the start offset of this line.
82 // TODO(adonovan): eventually replace this function with the
83 // simpler and more efficient (*go/token.File).LineStart, added
87 max := f.Size() // exclusive
89 offset := (min + max) / 2
91 posn := f.Position(pos)
92 if posn.Line == line {
93 return pos - (token.Pos(posn.Column) - 1)
100 if posn.Line < line {
108 // Imports returns true if path is imported by pkg.
109 func Imports(pkg *types.Package, path string) bool {
110 for _, imp := range pkg.Imports() {
111 if imp.Path() == path {