]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.ssa] cmd/compile/internal/ssa: add a String() method to Func
authorMichael Matloob <matloob@google.com>
Sat, 30 May 2015 17:17:12 +0000 (13:17 -0400)
committerKeith Randall <khr@golang.org>
Wed, 3 Jun 2015 04:35:55 +0000 (04:35 +0000)
The string method has the same output as printFunc.

Change-Id: Iab2ebc17a3d6418edfeb7b585e4f251e7a11f399
Reviewed-on: https://go-review.googlesource.com/10552
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/compile/internal/ssa/func_test.go
src/cmd/compile/internal/ssa/print.go

index 4839c1ee638cee25be1ba741b1680247e7564538..b66ab247787b3073f558a0d1878f82b011e1c60a 100644 (file)
@@ -324,11 +324,9 @@ func TestEquiv(t *testing.T) {
        }
        for _, c := range equivalentCases {
                if !Equiv(c.f.f, c.g.f) {
-                       t.Errorf("expected equivalence. Func definitions:")
-                       // TODO(matloob): Rewrite PrintFunc to output to a string or writer,
-                       // so the functions can be written to the error log.
-                       PrintFunc(c.f.f)
-                       PrintFunc(c.g.f)
+                       t.Error("expected equivalence. Func definitions:")
+                       t.Error(c.f.f)
+                       t.Error(c.g.f)
                }
        }
 
@@ -394,11 +392,9 @@ func TestEquiv(t *testing.T) {
        }
        for _, c := range differentCases {
                if Equiv(c.f.f, c.g.f) {
-                       t.Errorf("expected difference. Func definitions:")
-                       // TODO(matloob): Rewrite PrintFunc to output to a string or writer,
-                       // so the functions can be written to the error log.
-                       PrintFunc(c.f.f)
-                       PrintFunc(c.g.f)
+                       t.Error("expected difference. Func definitions:")
+                       t.Error(c.f.f)
+                       t.Error(c.g.f)
                }
        }
 }
index eeea30d970b53cb34c6d3a4e2e563bc307801800..b9a958c18eee1f51b50620d09c18f01dfd482345 100644 (file)
@@ -4,15 +4,30 @@
 
 package ssa
 
-import "fmt"
+import (
+       "bytes"
+       "fmt"
+       "io"
+       "os"
+)
 
 func printFunc(f *Func) {
-       fmt.Print(f.Name)
-       fmt.Print(" ")
-       fmt.Println(f.Type)
+       fprintFunc(os.Stdout, f)
+}
+
+func (f *Func) String() string {
+       var buf bytes.Buffer
+       fprintFunc(&buf, f)
+       return buf.String()
+}
+
+func fprintFunc(w io.Writer, f *Func) {
+       fmt.Fprint(w, f.Name)
+       fmt.Fprint(w, " ")
+       fmt.Fprintln(w, f.Type)
        printed := make([]bool, f.NumValues())
        for _, b := range f.Blocks {
-               fmt.Printf("  b%d:\n", b.ID)
+               fmt.Fprintf(w, "  b%d:\n", b.ID)
                n := 0
 
                // print phis first since all value cycles contain a phi
@@ -20,8 +35,8 @@ func printFunc(f *Func) {
                        if v.Op != OpPhi {
                                continue
                        }
-                       fmt.Print("    ")
-                       fmt.Println(v.LongString())
+                       fmt.Fprint(w, "    ")
+                       fmt.Fprintln(w, v.LongString())
                        printed[v.ID] = true
                        n++
                }
@@ -39,25 +54,25 @@ func printFunc(f *Func) {
                                                continue outer
                                        }
                                }
-                               fmt.Print("    ")
-                               fmt.Println(v.LongString())
+                               fmt.Fprint(w, "    ")
+                               fmt.Fprintln(w, v.LongString())
                                printed[v.ID] = true
                                n++
                        }
                        if m == n {
-                               fmt.Println("dependency cycle!")
+                               fmt.Fprintln(w, "dependency cycle!")
                                for _, v := range b.Values {
                                        if printed[v.ID] {
                                                continue
                                        }
-                                       fmt.Print("    ")
-                                       fmt.Println(v.LongString())
+                                       fmt.Fprint(w, "    ")
+                                       fmt.Fprintln(w, v.LongString())
                                        printed[v.ID] = true
                                        n++
                                }
                        }
                }
 
-               fmt.Println("    " + b.LongString())
+               fmt.Fprintln(w, "    "+b.LongString())
        }
 }