]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: implement fmt.Formatter for Nodes formats %s, %v
authorRobert Griesemer <gri@golang.org>
Wed, 31 Aug 2016 23:19:50 +0000 (16:19 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 8 Sep 2016 21:36:08 +0000 (21:36 +0000)
Change-Id: Iac3a72cb6c5394f3c1a49f39125b0256d570e006
Reviewed-on: https://go-review.googlesource.com/28339
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/bexport.go
src/cmd/compile/internal/gc/fmt.go
src/cmd/compile/internal/gc/inl.go
src/cmd/compile/internal/gc/walk.go

index 5fb918fc1726c3738e7989942ad7de5d922f51d2..3c8fb9a5765440aa9275c499d35afc6760089f54 100644 (file)
@@ -396,7 +396,7 @@ func export(out *bufio.Writer, trace bool) int {
                        // function has inlineable body:
                        // write index and body
                        if p.trace {
-                               p.tracef("\n----\nfunc { %s }\n", hconv(f.Inl, FmtSharp))
+                               p.tracef("\n----\nfunc { %#s }\n", f.Inl)
                        }
                        p.int(i)
                        p.stmtList(f.Inl)
index 9b8bb65cc1ababe8df2db89b6e2377df32fe7f12..d58199b76f3c15fb5c70496df93d90bfb14b5d76 100644 (file)
@@ -893,16 +893,16 @@ func (n *Node) stmtfmt(s fmt.State) {
 
        case OAS2:
                if n.Colas && !complexinit {
-                       fmt.Fprintf(s, "%v := %v", hconv(n.List, FmtComma), hconv(n.Rlist, FmtComma))
+                       fmt.Fprintf(s, "%.v := %.v", n.List, n.Rlist)
                        break
                }
                fallthrough
 
        case OAS2DOTTYPE, OAS2FUNC, OAS2MAPR, OAS2RECV:
-               fmt.Fprintf(s, "%v = %v", hconv(n.List, FmtComma), hconv(n.Rlist, FmtComma))
+               fmt.Fprintf(s, "%.v = %.v", n.List, n.Rlist)
 
        case ORETURN:
-               fmt.Fprintf(s, "return %v", hconv(n.List, FmtComma))
+               fmt.Fprintf(s, "return %.v", n.List)
 
        case ORETJMP:
                fmt.Fprintf(s, "retjmp %v", n.Sym)
@@ -959,7 +959,7 @@ func (n *Node) stmtfmt(s fmt.State) {
                        break
                }
 
-               fmt.Fprintf(s, "for %v = range %v { %v }", hconv(n.List, FmtComma), n.Right, n.Nbody)
+               fmt.Fprintf(s, "for %.v = range %v { %v }", n.List, n.Right, n.Nbody)
 
        case OSELECT, OSWITCH:
                if fmtmode == FErr {
@@ -979,7 +979,7 @@ func (n *Node) stmtfmt(s fmt.State) {
 
        case OXCASE:
                if n.List.Len() != 0 {
-                       fmt.Fprintf(s, "case %v", hconv(n.List, FmtComma))
+                       fmt.Fprintf(s, "case %.v", n.List)
                } else {
                        fmt.Fprint(s, "default")
                }
@@ -1296,7 +1296,7 @@ func (n *Node) exprfmt(s fmt.State, prec int) {
                        return
                }
 
-               fmt.Fprintf(s, "(%v{ %v })", n.Right, hconv(n.List, FmtComma))
+               fmt.Fprintf(s, "(%v{ %.v })", n.Right, n.List)
                return
 
        case OPTRLIT:
@@ -1308,7 +1308,7 @@ func (n *Node) exprfmt(s fmt.State, prec int) {
                        fmt.Fprintf(s, "%v literal", n.Type)
                        return
                }
-               fmt.Fprintf(s, "(%v{ %v })", n.Type, hconv(n.List, FmtComma))
+               fmt.Fprintf(s, "(%v{ %.v })", n.Type, n.List)
                return
 
        case OKEY:
@@ -1400,7 +1400,7 @@ func (n *Node) exprfmt(s fmt.State, prec int) {
                        fmt.Fprintf(s, "%v(%v)", n.Type, n.Left)
                        return
                }
-               fmt.Fprintf(s, "%v(%v)", n.Type, hconv(n.List, FmtComma))
+               fmt.Fprintf(s, "%v(%.v)", n.Type, n.List)
                return
 
        case OREAL,
@@ -1421,24 +1421,24 @@ func (n *Node) exprfmt(s fmt.State, prec int) {
                        return
                }
                if n.Isddd {
-                       fmt.Fprintf(s, "%#v(%v...)", n.Op, hconv(n.List, FmtComma))
+                       fmt.Fprintf(s, "%#v(%.v...)", n.Op, n.List)
                        return
                }
-               fmt.Fprintf(s, "%#v(%v)", n.Op, hconv(n.List, FmtComma))
+               fmt.Fprintf(s, "%#v(%.v)", n.Op, n.List)
                return
 
        case OCALL, OCALLFUNC, OCALLINTER, OCALLMETH, OGETG:
                n.Left.exprfmt(s, nprec)
                if n.Isddd {
-                       fmt.Fprintf(s, "(%v...)", hconv(n.List, FmtComma))
+                       fmt.Fprintf(s, "(%.v...)", n.List)
                        return
                }
-               fmt.Fprintf(s, "(%v)", hconv(n.List, FmtComma))
+               fmt.Fprintf(s, "(%.v)", n.List)
                return
 
        case OMAKEMAP, OMAKECHAN, OMAKESLICE:
                if n.List.Len() != 0 { // pre-typecheck
-                       fmt.Fprintf(s, "make(%v, %v)", n.Type, hconv(n.List, FmtComma))
+                       fmt.Fprintf(s, "make(%v, %.v)", n.Type, n.List)
                        return
                }
                if n.Right != nil {
@@ -1831,25 +1831,28 @@ func (n *Node) Nconv(s fmt.State) {
        fmtmode = sm
 }
 
-func (n Nodes) Print(p *printer) {
-       p.hconv(n, 0)
-}
+func (l Nodes) Format(s fmt.State, format rune) {
+       switch format {
+       case 's', 'v':
+               l.hconv(s)
 
-var _ Printable = Nodes{} // verify that Nodes implements Printable
+       default:
+               fmt.Fprintf(s, "%%!%c(Nodes)", format)
+       }
+}
 
 func (n Nodes) String() string {
-       return hconv(n, 0)
+       return fmt.Sprint(n)
 }
 
 // Fmt '%H': Nodes.
 // Flags: all those of %N plus ',': separate with comma's instead of semicolons.
-func hconv(l Nodes, flag FmtFlag) string {
-       return new(printer).hconv(l, flag).String()
-}
+func (l Nodes) hconv(s fmt.State) {
+       flag := fmtFlag(s)
 
-func (p *printer) hconv(l Nodes, flag FmtFlag) *printer {
        if l.Len() == 0 && fmtmode == FDbg {
-               return p.s("<nil>")
+               fmt.Fprint(s, "<nil>")
+               return
        }
 
        sf := flag
@@ -1862,20 +1865,18 @@ func (p *printer) hconv(l Nodes, flag FmtFlag) *printer {
        }
 
        for i, n := range l.Slice() {
-               p.f("%v", n)
+               fmt.Fprint(s, n)
                if i+1 < l.Len() {
-                       p.s(sep)
+                       fmt.Fprint(s, sep)
                }
        }
 
        flag = sf
        fmtmode = sm
-
-       return p
 }
 
 func dumplist(s string, l Nodes) {
-       fmt.Printf("%s%v\n", s, hconv(l, FmtSign))
+       fmt.Printf("%s%+v\n", s, l)
 }
 
 func Dump(s string, n *Node) {
index 4b85fd35fc099adcddf01f4b7276430e4877f347..8043ac8444c9b8d805c864efeee86ad0cdb1ccd7 100644 (file)
@@ -65,7 +65,7 @@ func typecheckinl(fn *Node) {
        }
 
        if Debug['m'] > 2 || Debug_export != 0 {
-               fmt.Printf("typecheck import [%v] %2v { %v }\n", fn.Sym, fn, hconv(fn.Func.Inl, FmtSharp))
+               fmt.Printf("typecheck import [%v] %2v { %#v }\n", fn.Sym, fn, fn.Func.Inl)
        }
 
        save_safemode := safemode
@@ -165,7 +165,7 @@ func caninl(fn *Node) {
        fn.Type.SetNname(n)
 
        if Debug['m'] > 1 {
-               fmt.Printf("%v: can inline %#v as: %#v { %v }\n", fn.Line(), n, fn.Type, hconv(n.Func.Inl, FmtSharp))
+               fmt.Printf("%v: can inline %#v as: %#v { %#v }\n", fn.Line(), n, fn.Type, n.Func.Inl)
        } else if Debug['m'] != 0 {
                fmt.Printf("%v: can inline %v\n", fn.Line(), n)
        }
@@ -556,7 +556,7 @@ func mkinlcall1(n *Node, fn *Node, isddd bool) *Node {
 
        // Bingo, we have a function node, and it has an inlineable body
        if Debug['m'] > 1 {
-               fmt.Printf("%v: inlining call to %v %#v { %v }\n", n.Line(), fn.Sym, fn.Type, hconv(fn.Func.Inl, FmtSharp))
+               fmt.Printf("%v: inlining call to %v %#v { %#v }\n", n.Line(), fn.Sym, fn.Type, fn.Func.Inl)
        } else if Debug['m'] != 0 {
                fmt.Printf("%v: inlining call to %v\n", n.Line(), fn)
        }
@@ -752,7 +752,7 @@ func mkinlcall1(n *Node, fn *Node, isddd bool) *Node {
                }
 
                if li < n.List.Len() || t != nil {
-                       Fatalf("arg count mismatch: %#v vs %v\n", fn.Type.Params(), hconv(n.List, FmtComma))
+                       Fatalf("arg count mismatch: %#v vs %.v\n", fn.Type.Params(), n.List)
                }
        }
 
index 58ab4c65db625be6d546e038ab905de72a12d0fa..9a6b4265251bb27c0659a4b41036cd00734f56fa 100644 (file)
@@ -1745,7 +1745,7 @@ func ascompatee(op Op, nl, nr []*Node, init *Nodes) []*Node {
                var nln, nrn Nodes
                nln.Set(nl)
                nrn.Set(nr)
-               Yyerror("error in shape across %v %v %v / %d %d [%s]", hconv(nln, FmtSign), op, hconv(nrn, FmtSign), len(nl), len(nr), Curfn.Func.Nname.Sym.Name)
+               Yyerror("error in shape across %+v %v %+v / %d %d [%s]", nln, op, nrn, len(nl), len(nr), Curfn.Func.Nname.Sym.Name)
        }
        return nn
 }