From ae3c9992ae57a67629e884b8536aac17a56db414 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Mon, 26 Oct 2009 19:21:13 -0700 Subject: [PATCH] - gofmt-ify walk.go - fixed several bugs R=rsc http://go/go-review/1015015 --- src/pkg/go/ast/walk.go | 111 +++++++++++++++++++++++++---------------- 1 file changed, 67 insertions(+), 44 deletions(-) diff --git a/src/pkg/go/ast/walk.go b/src/pkg/go/ast/walk.go index 472e6ea17b1..379cc901248 100644 --- a/src/pkg/go/ast/walk.go +++ b/src/pkg/go/ast/walk.go @@ -69,18 +69,28 @@ func walkBlockStmt(v Visitor, b *BlockStmt) { // Walk visits each of the children of n. // func Walk(v Visitor, node interface{}) { - if node != nil && !v.Visit(node) { + if node == nil || !v.Visit(node) { return; } // walk children + // (the order of the cases matches the order + // of the corresponding declaration in ast.go) switch n := node.(type) { // Comments and fields + case *Comment: + // nothing to do + case *CommentGroup: for _, c := range n.List { Walk(v, c); } - + // TODO(gri): Keep comments in a list/vector instead + // of linking them via Next. Following next will lead + // to multiple visits and potentially n^2 behavior + // since Doc and Comments fields point into the global + // comments list. + case *Field: walkCommentGroup(v, n.Doc); walkIdentList(v, n.Names); @@ -91,51 +101,54 @@ func Walk(v Visitor, node interface{}) { walkCommentGroup(v, n.Comment); // Expressions + case *BadExpr, *Ident, *Ellipsis, *BasicLit: + // nothing to do + case *StringList: for _, x := range n.Strings { Walk(v, x); } - + case *FuncLit: - Walk(v, n.Type); + if n != nil { + Walk(v, n.Type); + } walkBlockStmt(v, n.Body); - + case *CompositeLit: Walk(v, n.Type); walkExprList(v, n.Elts); - + case *ParenExpr: Walk(v, n.X); - + case *SelectorExpr: Walk(v, n.X); - if n.Sel != nil { - Walk(v, n.Sel); - } - + walkIdent(v, n.Sel); + case *IndexExpr: Walk(v, n.X); Walk(v, n.Index); Walk(v, n.End); - + case *TypeAssertExpr: Walk(v, n.X); Walk(v, n.Type); - + case *CallExpr: Walk(v, n.Fun); walkExprList(v, n.Args); - + case *StarExpr: Walk(v, n.X); - + case *UnaryExpr: Walk(v, n.X); - + case *BinaryExpr: Walk(v, n.X); Walk(v, n.Y); - + case *KeyValueExpr: Walk(v, n.Key); Walk(v, n.Value); @@ -144,76 +157,82 @@ func Walk(v Visitor, node interface{}) { case *ArrayType: Walk(v, n.Len); Walk(v, n.Elt); - + case *StructType: walkFieldList(v, n.Fields); - + case *FuncType: walkFieldList(v, n.Params); walkFieldList(v, n.Results); - + case *InterfaceType: walkFieldList(v, n.Methods); - + case *MapType: Walk(v, n.Key); Walk(v, n.Value); - + case *ChanType: Walk(v, n.Value); // Statements + case *BadStmt: + // nothing to do + case *DeclStmt: Walk(v, n.Decl); - + + case *EmptyStmt: + // nothing to do + case *LabeledStmt: walkIdent(v, n.Label); Walk(v, n.Stmt); - + case *ExprStmt: Walk(v, n.X); - + case *IncDecStmt: Walk(v, n.X); - + case *AssignStmt: walkExprList(v, n.Lhs); walkExprList(v, n.Rhs); - + case *GoStmt: if n.Call != nil { Walk(v, n.Call); } - + case *DeferStmt: if n.Call != nil { Walk(v, n.Call); } - + case *ReturnStmt: walkExprList(v, n.Results); - + case *BranchStmt: walkIdent(v, n.Label); - + case *BlockStmt: walkStmtList(v, n.List); - + case *IfStmt: Walk(v, n.Init); Walk(v, n.Cond); walkBlockStmt(v, n.Body); Walk(v, n.Else); - + case *CaseClause: walkExprList(v, n.Values); walkStmtList(v, n.Body); - + case *SwitchStmt: Walk(v, n.Init); Walk(v, n.Tag); walkBlockStmt(v, n.Body); - + case *TypeCaseClause: walkExprList(v, n.Types); walkStmtList(v, n.Body); @@ -222,12 +241,12 @@ func Walk(v Visitor, node interface{}) { Walk(v, n.Init); Walk(v, n.Assign); walkBlockStmt(v, n.Body); - + case *CommClause: Walk(v, n.Lhs); Walk(v, n.Rhs); walkStmtList(v, n.Body); - + case *SelectStmt: walkBlockStmt(v, n.Body); @@ -236,13 +255,13 @@ func Walk(v Visitor, node interface{}) { Walk(v, n.Cond); Walk(v, n.Post); walkBlockStmt(v, n.Body); - + case *RangeStmt: Walk(v, n.Key); Walk(v, n.Value); Walk(v, n.X); walkBlockStmt(v, n.Body); - + // Declarations case *ImportSpec: walkCommentGroup(v, n.Doc); @@ -251,34 +270,38 @@ func Walk(v Visitor, node interface{}) { Walk(v, x); } walkCommentGroup(v, n.Comment); - - + case *ValueSpec: walkCommentGroup(v, n.Doc); walkIdentList(v, n.Names); Walk(v, n.Type); walkExprList(v, n.Values); walkCommentGroup(v, n.Comment); - + case *TypeSpec: walkCommentGroup(v, n.Doc); walkIdent(v, n.Name); Walk(v, n.Type); walkCommentGroup(v, n.Comment); + case *BadDecl: + // nothing to do + case *GenDecl: walkCommentGroup(v, n.Doc); for _, s := range n.Specs { Walk(v, s); } - + case *FuncDecl: walkCommentGroup(v, n.Doc); if n.Recv != nil { Walk(v, n.Recv); } walkIdent(v, n.Name); - Walk(v, n.Type); + if n.Type != nil { + Walk(v, n.Type); + } walkBlockStmt(v, n.Body); // Files and packages