]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: log rules to a file for rule coverage tool
authorKeith Randall <khr@golang.org>
Tue, 24 May 2016 22:43:25 +0000 (15:43 -0700)
committerKeith Randall <khr@golang.org>
Thu, 26 May 2016 20:37:05 +0000 (20:37 +0000)
When rules are generated with -log, log rule application to a file.

The file is opened in append mode so multiple calls to the compiler
union their logs.

Change-Id: Ib35c7c85bf58e5909ea9231043f8cbaa6bf278b7
Reviewed-on: https://go-review.googlesource.com/23406
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
src/cmd/compile/internal/ssa/gen/rulegen.go
src/cmd/compile/internal/ssa/rewrite.go

index fcabdb1dd908d94b2b9bc935fbffd47ae78add89..0fc5749f1d5aa61e6063f3c792106b591d6ffcf8 100644 (file)
@@ -150,9 +150,6 @@ func genRules(arch arch) {
        fmt.Fprintln(w, "// generated with: cd gen; go run *.go")
        fmt.Fprintln(w)
        fmt.Fprintln(w, "package ssa")
-       if *genLog {
-               fmt.Fprintln(w, "import \"fmt\"")
-       }
        fmt.Fprintln(w, "import \"math\"")
        fmt.Fprintln(w, "var _ = math.MinInt8 // in case not otherwise used")
 
@@ -196,7 +193,7 @@ func genRules(arch arch) {
 
                        genResult(w, arch, result, rule.loc)
                        if *genLog {
-                               fmt.Fprintf(w, "fmt.Println(\"rewrite %s\")\n", rule.loc)
+                               fmt.Fprintf(w, "logRule(\"%s\")\n", rule.loc)
                        }
                        fmt.Fprintf(w, "return true\n")
 
@@ -300,7 +297,7 @@ func genRules(arch arch) {
                        }
 
                        if *genLog {
-                               fmt.Fprintf(w, "fmt.Println(\"rewrite %s\")\n", rule.loc)
+                               fmt.Fprintf(w, "logRule(\"%s\")\n", rule.loc)
                        }
                        fmt.Fprintf(w, "return true\n")
 
index 7d6d2179f7642592ff024a83827d90fcb2aebbf2..03c38827cc433ea9c2b9966a2615873269804f9f 100644 (file)
@@ -7,6 +7,8 @@ package ssa
 import (
        "fmt"
        "math"
+       "os"
+       "path/filepath"
 )
 
 func applyRewrite(f *Func, rb func(*Block) bool, rv func(*Value, *Config) bool) {
@@ -357,3 +359,28 @@ func clobber(v *Value) bool {
        // Note: leave v.Block intact.  The Block field is used after clobber.
        return true
 }
+
+// logRule logs the use of the rule s. This will only be enabled if
+// rewrite rules were generated with the -log option, see gen/rulegen.go.
+func logRule(s string) {
+       if ruleFile == nil {
+               // Open a log file to write log to. We open in append
+               // mode because all.bash runs the compiler lots of times,
+               // and we want the concatenation of all of those logs.
+               // This means, of course, that users need to rm the old log
+               // to get fresh data.
+               // TODO: all.bash runs compilers in parallel. Need to synchronize logging somehow?
+               w, err := os.OpenFile(filepath.Join(os.Getenv("GOROOT"), "src", "rulelog"),
+                       os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
+               if err != nil {
+                       panic(err)
+               }
+               ruleFile = w
+       }
+       _, err := fmt.Fprintf(ruleFile, "rewrite %s\n", s)
+       if err != nil {
+               panic(err)
+       }
+}
+
+var ruleFile *os.File