From 4f13714aca90105a7905f35e7ce70686bc24e518 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Mon, 29 Dec 2014 15:35:00 -0500 Subject: [PATCH] astutil: add Unparen utility, eliminating 7 copies. Change-Id: I824328c275bd6198a57edd64bf72cb2cf0490a68 Reviewed-on: https://go-review.googlesource.com/2172 Reviewed-by: Chris Manghane Reviewed-by: Josh Bleecher Snyder --- astutil/util.go | 14 ++++++++++++++ cmd/vet/bool.go | 14 +++----------- go/ssa/util.go | 13 ++----------- go/types/builtins.go | 11 ++--------- oracle/oracle.go | 12 +----------- refactor/eg/match.go | 14 ++------------ refactor/rename/util.go | 13 ++----------- refactor/satisfy/find.go | 13 ++----------- 8 files changed, 28 insertions(+), 76 deletions(-) create mode 100644 astutil/util.go diff --git a/astutil/util.go b/astutil/util.go new file mode 100644 index 00000000..76306298 --- /dev/null +++ b/astutil/util.go @@ -0,0 +1,14 @@ +package astutil + +import "go/ast" + +// Unparen returns e with any enclosing parentheses stripped. +func Unparen(e ast.Expr) ast.Expr { + for { + p, ok := e.(*ast.ParenExpr) + if !ok { + return e + } + e = p.X + } +} diff --git a/cmd/vet/bool.go b/cmd/vet/bool.go index e28c2588..257c5c6f 100644 --- a/cmd/vet/bool.go +++ b/cmd/vet/bool.go @@ -9,6 +9,8 @@ package main import ( "go/ast" "go/token" + + "golang.org/x/tools/astutil" ) func init() { @@ -162,7 +164,7 @@ func hasSideEffects(e ast.Expr) bool { // split returns []{d, c, b, a}. func (op boolOp) split(e ast.Expr) (exprs []ast.Expr) { for { - e = unparen(e) + e = astutil.Unparen(e) if b, ok := e.(*ast.BinaryExpr); ok && b.Op == op.tok { exprs = append(exprs, op.split(b.Y)...) e = b.X @@ -173,13 +175,3 @@ func (op boolOp) split(e ast.Expr) (exprs []ast.Expr) { } return } - -func unparen(e ast.Expr) ast.Expr { - for { - p, ok := e.(*ast.ParenExpr) - if !ok { - return e - } - e = p.X - } -} diff --git a/go/ssa/util.go b/go/ssa/util.go index 1cf15e88..58f4e8d8 100644 --- a/go/ssa/util.go +++ b/go/ssa/util.go @@ -13,6 +13,7 @@ import ( "io" "os" + "golang.org/x/tools/astutil" "golang.org/x/tools/go/types" ) @@ -22,17 +23,7 @@ func unreachable() { //// AST utilities -// unparen returns e with any enclosing parentheses stripped. -func unparen(e ast.Expr) ast.Expr { - for { - p, ok := e.(*ast.ParenExpr) - if !ok { - break - } - e = p.X - } - return e -} +func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) } // isBlankIdent returns true iff e is an Ident with name "_". // They have no associated types.Object, and thus no type. diff --git a/go/types/builtins.go b/go/types/builtins.go index fc0b7f72..97ccf4c6 100644 --- a/go/types/builtins.go +++ b/go/types/builtins.go @@ -10,6 +10,7 @@ import ( "go/ast" "go/token" + "golang.org/x/tools/astutil" "golang.org/x/tools/go/exact" ) @@ -608,15 +609,7 @@ func implicitArrayDeref(typ Type) Type { return typ } -// unparen removes any parentheses surrounding an expression and returns -// the naked expression. -// -func unparen(x ast.Expr) ast.Expr { - if p, ok := x.(*ast.ParenExpr); ok { - return unparen(p.X) - } - return x -} +func unparen(x ast.Expr) ast.Expr { return astutil.Unparen(x) } func (check *Checker) complexArg(x *operand) bool { t, _ := x.typ.Underlying().(*Basic) diff --git a/oracle/oracle.go b/oracle/oracle.go index 34a521d0..e97d76e5 100644 --- a/oracle/oracle.go +++ b/oracle/oracle.go @@ -482,17 +482,7 @@ func ptrAnalysis(o *Oracle) *pointer.Result { return result } -// unparen returns e with any enclosing parentheses stripped. -func unparen(e ast.Expr) ast.Expr { - for { - p, ok := e.(*ast.ParenExpr) - if !ok { - break - } - e = p.X - } - return e -} +func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) } // deref returns a pointer's element type; otherwise it returns typ. func deref(typ types.Type) types.Type { diff --git a/refactor/eg/match.go b/refactor/eg/match.go index f5245058..6c1df4b8 100644 --- a/refactor/eg/match.go +++ b/refactor/eg/match.go @@ -8,6 +8,7 @@ import ( "os" "reflect" + "golang.org/x/tools/astutil" "golang.org/x/tools/go/exact" "golang.org/x/tools/go/loader" "golang.org/x/tools/go/types" @@ -216,18 +217,7 @@ func (tr *Transformer) matchWildcard(xobj *types.Var, y ast.Expr) bool { // -- utilities -------------------------------------------------------- -// unparen returns e with any enclosing parentheses stripped. -// TODO(adonovan): move to astutil package. -func unparen(e ast.Expr) ast.Expr { - for { - p, ok := e.(*ast.ParenExpr) - if !ok { - break - } - e = p.X - } - return e -} +func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) } // isRef returns the object referred to by this (possibly qualified) // identifier, or nil if the node is not a referring identifier. diff --git a/refactor/rename/util.go b/refactor/rename/util.go index bf9e3504..8f3c62ae 100644 --- a/refactor/rename/util.go +++ b/refactor/rename/util.go @@ -13,6 +13,7 @@ import ( "strings" "unicode" + "golang.org/x/tools/astutil" "golang.org/x/tools/go/types" ) @@ -100,14 +101,4 @@ func sameFile(x, y string) bool { return false } -// unparen returns e with any enclosing parentheses stripped. -func unparen(e ast.Expr) ast.Expr { - for { - p, ok := e.(*ast.ParenExpr) - if !ok { - break - } - e = p.X - } - return e -} +func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) } diff --git a/refactor/satisfy/find.go b/refactor/satisfy/find.go index 3972637e..2af37f1e 100644 --- a/refactor/satisfy/find.go +++ b/refactor/satisfy/find.go @@ -49,6 +49,7 @@ import ( "go/ast" "go/token" + "golang.org/x/tools/astutil" "golang.org/x/tools/go/types" ) @@ -698,17 +699,7 @@ func deref(typ types.Type) types.Type { return typ } -// unparen returns e with any enclosing parentheses stripped. -func unparen(e ast.Expr) ast.Expr { - for { - p, ok := e.(*ast.ParenExpr) - if !ok { - break - } - e = p.X - } - return e -} +func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) } func isInterface(T types.Type) bool { _, ok := T.Underlying().(*types.Interface)