From 4622486b626d22465cf8248b39f3e5a169ec8f25 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Fri, 9 Aug 2013 12:55:21 +1000 Subject: [PATCH] go.tools/cmd/cover: add -func mode to print per-function coverage fmt/format.go: init 100.0% fmt/format.go: clearflags 100.0% fmt/format.go: init 100.0% fmt/format.go: computePadding 84.6% fmt/format.go: writePadding 83.3% ... total: (statements) 91.3% Fixes golang/go#5985. R=golang-dev, dave, gri CC=golang-dev https://golang.org/cl/12454043 --- cmd/cover/cover.go | 42 +++++----- cmd/cover/func.go | 157 +++++++++++++++++++++++++++++++++++++ cmd/cover/html.go | 178 +++--------------------------------------- cmd/cover/profile.go | 182 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 375 insertions(+), 184 deletions(-) create mode 100644 cmd/cover/func.go create mode 100644 cmd/cover/profile.go diff --git a/cmd/cover/cover.go b/cmd/cover/cover.go index 26be08344d..2238f6d38d 100644 --- a/cmd/cover/cover.go +++ b/cmd/cover/cover.go @@ -16,7 +16,6 @@ package main import ( - "bytes" "flag" "fmt" "go/ast" @@ -24,7 +23,6 @@ import ( "go/printer" "go/token" "io" - "io/ioutil" "log" "os" "sort" @@ -38,8 +36,12 @@ a web browser displaying annotated source code: go tool cover -html=c.out The same, but write the generated HTML to a file instead of starting a browser: go tool cover -html=c.out -o coverage.html +Write to standard output coverage percentages for each function: + go tool cover -func=c.out Generate modified source code with coverage annotations (what go test -cover does): go tool cover -mode=set -var=CoverageVariableName program.go + +Only one of -html, -func, or -mode may be set. ` func usage() { @@ -52,8 +54,9 @@ func usage() { var ( mode = flag.String("mode", "", "coverage mode: set, count, atomic") varVar = flag.String("var", "GoCover", "name of coverage variable to generate") - output = flag.String("o", "", "file for output (static HTML or annotated Go source); default: stdout") + output = flag.String("o", "", "file for output; default: stdout") htmlOut = flag.String("html", "", "generate HTML representation of coverage profile") + funcOut = flag.String("func", "", "output coverage profile information for each function") ) var profile string // The profile to read; the value of -html but stored separately for future flexibility. @@ -68,19 +71,31 @@ const ( func main() { flag.Usage = usage flag.Parse() + profile = *htmlOut + if *funcOut != "" { + if profile != "" { + flag.Usage() + } + profile = *funcOut + } // Must either display a profile or rewrite Go source. if (profile == "") == (*mode == "") { flag.Usage() } - // Generate HTML. - if *htmlOut != "" { + // Generate HTML or function coverage information. + if profile != "" { if flag.NArg() != 0 { flag.Usage() } - err := htmlOutput(profile, *output) + var err error + if *htmlOut != "" { + err = htmlOutput(profile, *output) + } else { + err = funcOutput(profile, *output) + } if err != nil { fmt.Fprintf(os.Stderr, "cover: %v\n", err) os.Exit(2) @@ -254,22 +269,13 @@ func (f *File) addImport(path string) string { } func cover(name string) { - fs := token.NewFileSet() - f, err := os.Open(name) - if err != nil { - log.Fatalf("cover: %s: %s", name, err) - } - defer f.Close() - data, err := ioutil.ReadAll(f) - if err != nil { - log.Fatalf("cover: %s: %s", name, err) - } - parsedFile, err := parser.ParseFile(fs, name, bytes.NewReader(data), 0) + fset := token.NewFileSet() + parsedFile, err := parser.ParseFile(fset, name, nil, 0) if err != nil { log.Fatalf("cover: %s: %s", name, err) } file := &File{ - fset: fs, + fset: fset, name: name, astFile: parsedFile, } diff --git a/cmd/cover/func.go b/cmd/cover/func.go new file mode 100644 index 0000000000..7b4753e586 --- /dev/null +++ b/cmd/cover/func.go @@ -0,0 +1,157 @@ +// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file implements the visitor that computes the (line, column)-(line-column) range for each function. + +package main + +import ( + "bufio" + "fmt" + "go/ast" + "go/parser" + "go/token" + "os" + "text/tabwriter" +) + +// funcOutput takes two file names as arguments, a coverage profile to read as input and an output +// file to write ("" means to write to standard output). The function reads the profile and produces +// as output the coverage data broken down by function, like this: +// +// fmt/format.go: init 100.0% +// fmt/format.go: computePadding 84.6% +// ... +// fmt/scan.go: doScan 100.0% +// fmt/scan.go: advance 96.2% +// fmt/scan.go: doScanf 96.8% +// total: (statements) 91.4% + +func funcOutput(profile, outputFile string) error { + pf, err := os.Open(profile) + if err != nil { + return err + } + defer pf.Close() + + profiles, err := ParseProfiles(pf) + if err != nil { + return err + } + + var out *bufio.Writer + if outputFile == "" { + out = bufio.NewWriter(os.Stdout) + } else { + fd, err := os.Create(outputFile) + if err != nil { + return err + } + defer fd.Close() + out = bufio.NewWriter(fd) + } + defer out.Flush() + + tabber := tabwriter.NewWriter(out, 1, 8, 1, '\t', 0) + defer tabber.Flush() + + var total, covered int64 + for fn, profile := range profiles { + file, err := findFile(fn) + if err != nil { + return err + } + funcs, err := findFuncs(file) + if err != nil { + return err + } + // Now match up functions and profile blocks. + for _, f := range funcs { + c, t := f.coverage(profile) + fmt.Fprintf(tabber, "%s:\t%s\t%.1f%%\n", fn, f.name, 100.0*float64(c)/float64(t)) + total += t + covered += c + } + } + fmt.Fprintf(tabber, "total:\t(statements)\t%.1f%%\n", 100.0*float64(covered)/float64(total)) + + return nil +} + +// findFuncs parses the file and returns a slice of FuncExtent descriptors. +func findFuncs(name string) ([]*FuncExtent, error) { + fset := token.NewFileSet() + parsedFile, err := parser.ParseFile(fset, name, nil, 0) + if err != nil { + return nil, err + } + visitor := &FuncVisitor{ + fset: fset, + name: name, + astFile: parsedFile, + } + ast.Walk(visitor, visitor.astFile) + return visitor.funcs, nil +} + +// FuncExtent describes a function's extent in the source by file and position. +type FuncExtent struct { + name string + startLine int + startCol int + endLine int + endCol int +} + +// FuncVisitor implements the visitor that builds the function position list for a file. +type FuncVisitor struct { + fset *token.FileSet + name string // Name of file. + astFile *ast.File + funcs []*FuncExtent +} + +// Visit implements the ast.Visitor interface. +func (v *FuncVisitor) Visit(node ast.Node) ast.Visitor { + switch n := node.(type) { + case *ast.FuncDecl: + start := v.fset.Position(n.Pos()) + end := v.fset.Position(n.End()) + fe := &FuncExtent{ + name: n.Name.Name, + startLine: start.Line, + startCol: start.Column, + endLine: end.Line, + endCol: end.Column, + } + v.funcs = append(v.funcs, fe) + } + return v +} + +// coverage returns the fraction of the statements in the function that were covered, as a numerator and denominator. +func (f *FuncExtent) coverage(profile *Profile) (num, den int64) { + // We could avoid making this n^2 overall by doing a single scan and annotating the functions, + // but the sizes of the data structures is never very large and the scan is almost instantaneous. + var covered, total int64 + // The blocks are sorted, so we can stop counting as soon as we reach the end of the relevant block. + for _, b := range profile.Blocks { + if b.StartLine > f.endLine || (b.StartLine == f.endLine && b.StartCol >= f.endCol) { + // Past the end of the function. + break + } + if b.EndLine < f.startLine || (b.EndLine == f.startLine && b.EndCol <= f.startCol) { + // Before the beginning of the function + continue + } + total += int64(b.NumStmt) + if b.Count > 0 { + covered += int64(b.NumStmt) + } + } + if total == 0 { + total = 1 // Avoid zero denominator. + } + return covered, total +} diff --git a/cmd/cover/html.go b/cmd/cover/html.go index 113009b99a..d41685445b 100644 --- a/cmd/cover/html.go +++ b/cmd/cover/html.go @@ -8,7 +8,6 @@ import ( "bufio" "bytes" "fmt" - "go/build" "html/template" "io" "io/ioutil" @@ -16,11 +15,7 @@ import ( "os" "os/exec" "path/filepath" - "regexp" "runtime" - "sort" - "strconv" - "strings" ) // htmlOutput reads the profile data from profile and generates an HTML @@ -44,17 +39,16 @@ func htmlOutput(profile, outfile string) error { if profile.Mode == "set" { d.Set = true } - dir, file := filepath.Split(fn) - pkg, err := build.Import(dir, ".", build.FindOnly) + file, err := findFile(fn) if err != nil { - return fmt.Errorf("can't find %q: %v", fn, err) + return err } - src, err := ioutil.ReadFile(filepath.Join(pkg.Dir, file)) + src, err := ioutil.ReadFile(file) if err != nil { return fmt.Errorf("can't read %q: %v", fn, err) } var buf bytes.Buffer - err = htmlGen(&buf, src, profile.Tokens(src)) + err = htmlGen(&buf, src, profile.Boundaries(src)) if err != nil { return err } @@ -92,171 +86,23 @@ func htmlOutput(profile, outfile string) error { return nil } -// Profile represents the profiling data for a specific file. -type Profile struct { - Mode string - Blocks []ProfileBlock -} - -// ProfileBlock represents a single block of profiling data. -type ProfileBlock struct { - StartLine, StartCol int - EndLine, EndCol int - NumStmt, Count int -} - -// ParseProfiles parses profile data from the given Reader and returns a -// Profile for each file. -func ParseProfiles(r io.Reader) (map[string]*Profile, error) { - files := make(map[string]*Profile) - buf := bufio.NewReader(r) - // First line is "mode: foo", where foo is "set", "count", or "atomic". - // Rest of file is in the format - // encoding/base64/base64.go:34.44,37.40 3 1 - // where the fields are: name.go:line.column,line.column numberOfStatements count - s := bufio.NewScanner(buf) - mode := "" - for s.Scan() { - line := s.Text() - if mode == "" { - const p = "mode: " - if !strings.HasPrefix(line, p) || line == p { - return nil, fmt.Errorf("bad mode line: %v", line) - } - mode = line[len(p):] - continue - } - m := lineRe.FindStringSubmatch(line) - if m == nil { - return nil, fmt.Errorf("line %q doesn't match expected format: %v", m, lineRe) - } - fn := m[1] - p := files[fn] - if p == nil { - p = &Profile{Mode: mode} - files[fn] = p - } - p.Blocks = append(p.Blocks, ProfileBlock{ - StartLine: toInt(m[2]), - StartCol: toInt(m[3]), - EndLine: toInt(m[4]), - EndCol: toInt(m[5]), - NumStmt: toInt(m[6]), - Count: toInt(m[7]), - }) - } - if err := s.Err(); err != nil { - return nil, err - } - for _, p := range files { - sort.Sort(blocksByStart(p.Blocks)) - } - return files, nil -} - -type blocksByStart []ProfileBlock - -func (b blocksByStart) Len() int { return len(b) } -func (b blocksByStart) Swap(i, j int) { b[i], b[j] = b[j], b[i] } -func (b blocksByStart) Less(i, j int) bool { - return b[i].StartLine < b[j].StartLine || b[i].StartLine == b[j].StartLine && b[i].StartCol < b[j].StartCol -} - -var lineRe = regexp.MustCompile(`^(.+):([0-9]+).([0-9]+),([0-9]+).([0-9]+) ([0-9]+) ([0-9]+)$`) - -func toInt(s string) int { - i, err := strconv.ParseInt(s, 10, 64) - if err != nil { - panic(err) - } - return int(i) -} - -// Token represents the position in a source file of an opening or closing -// tag. These are used to colorize the source. -type Token struct { - Pos int - Start bool - Count int - Norm float64 // count normalized to 0-1 -} - -// Tokens returns a Profile as a set of Tokens within the provided src. -func (p *Profile) Tokens(src []byte) (tokens []Token) { - // Find maximum counts. - max := 0 - for _, b := range p.Blocks { - if b.Count > max { - max = b.Count - } - } - // Divisor for normalization. - divisor := math.Log(float64(max)) - - // tok returns a Token, populating the Norm field with a normalized Count. - tok := func(pos int, start bool, count int) Token { - t := Token{Pos: pos, Start: start, Count: count} - if !start || count == 0 { - return t - } - if max <= 1 { - t.Norm = 0.8 // "set" mode; use cov8 - } else if count > 0 { - t.Norm = math.Log(float64(count)) / divisor - } - return t - } - - line, col := 1, 2 - for si, bi := 0, 0; si < len(src) && bi < len(p.Blocks); { - b := p.Blocks[bi] - if b.StartLine == line && b.StartCol == col { - tokens = append(tokens, tok(si, true, b.Count)) - } - if b.EndLine == line && b.EndCol == col { - tokens = append(tokens, tok(si, false, 0)) - bi++ - continue // Don't advance through src; maybe the next block starts here. - } - if src[si] == '\n' { - line++ - col = 0 - } - col++ - si++ - } - sort.Sort(tokensByPos(tokens)) - return -} - -type tokensByPos []Token - -func (t tokensByPos) Len() int { return len(t) } -func (t tokensByPos) Swap(i, j int) { t[i], t[j] = t[j], t[i] } -func (t tokensByPos) Less(i, j int) bool { - if t[i].Pos == t[j].Pos { - return !t[i].Start && t[j].Start - } - return t[i].Pos < t[j].Pos -} - // htmlGen generates an HTML coverage report with the provided filename, // source code, and tokens, and writes it to the given Writer. -func htmlGen(w io.Writer, src []byte, tokens []Token) error { +func htmlGen(w io.Writer, src []byte, boundaries []Boundary) error { dst := bufio.NewWriter(w) for i := range src { - for len(tokens) > 0 && tokens[0].Pos == i { - t := tokens[0] - if t.Start { + for len(boundaries) > 0 && boundaries[0].Offset == i { + b := boundaries[0] + if b.Start { n := 0 - if t.Count > 0 { - n = int(math.Floor(t.Norm*9)) + 1 + if b.Count > 0 { + n = int(math.Floor(b.Norm*9)) + 1 } - fmt.Fprintf(dst, ``, n, t.Count) + fmt.Fprintf(dst, ``, n, b.Count) } else { dst.WriteString("") } - tokens = tokens[1:] + boundaries = boundaries[1:] } switch b := src[i]; b { case '>': diff --git a/cmd/cover/profile.go b/cmd/cover/profile.go new file mode 100644 index 0000000000..caa9712f30 --- /dev/null +++ b/cmd/cover/profile.go @@ -0,0 +1,182 @@ +// Copyright 2013 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "bufio" + "fmt" + "go/build" + "io" + "math" + "path/filepath" + "regexp" + "sort" + "strconv" + "strings" +) + +// Profile represents the profiling data for a specific file. +type Profile struct { + Mode string + Blocks []ProfileBlock +} + +// ProfileBlock represents a single block of profiling data. +type ProfileBlock struct { + StartLine, StartCol int + EndLine, EndCol int + NumStmt, Count int +} + +// ParseProfiles parses profile data from the given Reader and returns a +// Profile for each file. +func ParseProfiles(r io.Reader) (map[string]*Profile, error) { + files := make(map[string]*Profile) + buf := bufio.NewReader(r) + // First line is "mode: foo", where foo is "set", "count", or "atomic". + // Rest of file is in the format + // encoding/base64/base64.go:34.44,37.40 3 1 + // where the fields are: name.go:line.column,line.column numberOfStatements count + s := bufio.NewScanner(buf) + mode := "" + for s.Scan() { + line := s.Text() + if mode == "" { + const p = "mode: " + if !strings.HasPrefix(line, p) || line == p { + return nil, fmt.Errorf("bad mode line: %v", line) + } + mode = line[len(p):] + continue + } + m := lineRe.FindStringSubmatch(line) + if m == nil { + return nil, fmt.Errorf("line %q doesn't match expected format: %v", m, lineRe) + } + fn := m[1] + p := files[fn] + if p == nil { + p = &Profile{Mode: mode} + files[fn] = p + } + p.Blocks = append(p.Blocks, ProfileBlock{ + StartLine: toInt(m[2]), + StartCol: toInt(m[3]), + EndLine: toInt(m[4]), + EndCol: toInt(m[5]), + NumStmt: toInt(m[6]), + Count: toInt(m[7]), + }) + } + if err := s.Err(); err != nil { + return nil, err + } + for _, p := range files { + sort.Sort(blocksByStart(p.Blocks)) + } + return files, nil +} + +type blocksByStart []ProfileBlock + +func (b blocksByStart) Len() int { return len(b) } +func (b blocksByStart) Swap(i, j int) { b[i], b[j] = b[j], b[i] } +func (b blocksByStart) Less(i, j int) bool { + bi, bj := b[i], b[j] + return bi.StartLine < bj.StartLine || bi.StartLine == bj.StartLine && bi.StartCol < bj.StartCol +} + +var lineRe = regexp.MustCompile(`^(.+):([0-9]+).([0-9]+),([0-9]+).([0-9]+) ([0-9]+) ([0-9]+)$`) + +func toInt(s string) int { + i64, err := strconv.ParseInt(s, 10, 64) + if err != nil { + panic(err) + } + const maxInt = int64(int(^uint(0) >> 1)) + if i64 > maxInt { + i64 = maxInt + } + return int(i64) +} + +// Boundary represents the position in a source file of the beginning or end of a +// block as reported by the coverage profile. In HTML mode, it will correspond to +// the opening or closing of a tag and will be used to colorize the source +type Boundary struct { + Offset int // Location as a byte offset in the source file. + Start bool // Is this the start of a block? + Count int // Event count from the cover profile. + Norm float64 // Count normalized to [0..1]. +} + +// Boundaries returns a Profile as a set of Boundary objects within the provided src. +func (p *Profile) Boundaries(src []byte) (boundaries []Boundary) { + // Find maximum count. + max := 0 + for _, b := range p.Blocks { + if b.Count > max { + max = b.Count + } + } + // Divisor for normalization. + divisor := math.Log(float64(max)) + + // boundary returns a Boundary, populating the Norm field with a normalized Count. + boundary := func(offset int, start bool, count int) Boundary { + b := Boundary{Offset: offset, Start: start, Count: count} + if !start || count == 0 { + return b + } + if max <= 1 { + b.Norm = 0.8 // Profile is in"set" mode; we want a heat map. Use cov8 in the CSS. + } else if count > 0 { + b.Norm = math.Log(float64(count)) / divisor + } + return b + } + + line, col := 1, 2 // TODO: Why is this 2? + for si, bi := 0, 0; si < len(src) && bi < len(p.Blocks); { + b := p.Blocks[bi] + if b.StartLine == line && b.StartCol == col { + boundaries = append(boundaries, boundary(si, true, b.Count)) + } + if b.EndLine == line && b.EndCol == col { + boundaries = append(boundaries, boundary(si, false, 0)) + bi++ + continue // Don't advance through src; maybe the next block starts here. + } + if src[si] == '\n' { + line++ + col = 0 + } + col++ + si++ + } + sort.Sort(boundariesByPos(boundaries)) + return +} + +type boundariesByPos []Boundary + +func (b boundariesByPos) Len() int { return len(b) } +func (b boundariesByPos) Swap(i, j int) { b[i], b[j] = b[j], b[i] } +func (b boundariesByPos) Less(i, j int) bool { + if b[i].Offset == b[j].Offset { + return !b[i].Start && b[j].Start + } + return b[i].Offset < b[j].Offset +} + +// findFile finds the location of the named file in GOROOT, GOPATH etc. +func findFile(file string) (string, error) { + dir, file := filepath.Split(file) + pkg, err := build.Import(dir, ".", build.FindOnly) + if err != nil { + return "", fmt.Errorf("can't find %q: %v", file, err) + } + return filepath.Join(pkg.Dir, file), nil +}