1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file implements the visitor that computes the (line, column)-(line-column) range for each function.
20 "golang.org/x/tools/cover"
23 // funcOutput takes two file names as arguments, a coverage profile to read as input and an output
24 // file to write ("" means to write to standard output). The function reads the profile and produces
25 // as output the coverage data broken down by function, like this:
27 // fmt/format.go:30: init 100.0%
28 // fmt/format.go:57: clearflags 100.0%
30 // fmt/scan.go:1046: doScan 100.0%
31 // fmt/scan.go:1075: advance 96.2%
32 // fmt/scan.go:1119: doScanf 96.8%
33 // total: (statements) 91.9%
35 func funcOutput(profile, outputFile string) error {
36 profiles, err := cover.ParseProfiles(profile)
43 out = bufio.NewWriter(os.Stdout)
45 fd, err := os.Create(outputFile)
50 out = bufio.NewWriter(fd)
54 tabber := tabwriter.NewWriter(out, 1, 8, 1, '\t', 0)
57 var total, covered int64
58 for _, profile := range profiles {
59 fn := profile.FileName
60 file, err := findFile(fn)
64 funcs, err := findFuncs(file)
68 // Now match up functions and profile blocks.
69 for _, f := range funcs {
70 c, t := f.coverage(profile)
71 fmt.Fprintf(tabber, "%s:%d:\t%s\t%.1f%%\n", fn, f.startLine, f.name, 100.0*float64(c)/float64(t))
76 fmt.Fprintf(tabber, "total:\t(statements)\t%.1f%%\n", 100.0*float64(covered)/float64(total))
81 // findFuncs parses the file and returns a slice of FuncExtent descriptors.
82 func findFuncs(name string) ([]*FuncExtent, error) {
83 fset := token.NewFileSet()
84 parsedFile, err := parser.ParseFile(fset, name, nil, 0)
88 visitor := &FuncVisitor{
93 ast.Walk(visitor, visitor.astFile)
94 return visitor.funcs, nil
97 // FuncExtent describes a function's extent in the source by file and position.
98 type FuncExtent struct {
106 // FuncVisitor implements the visitor that builds the function position list for a file.
107 type FuncVisitor struct {
109 name string // Name of file.
114 // Visit implements the ast.Visitor interface.
115 func (v *FuncVisitor) Visit(node ast.Node) ast.Visitor {
116 switch n := node.(type) {
118 start := v.fset.Position(n.Pos())
119 end := v.fset.Position(n.End())
122 startLine: start.Line,
123 startCol: start.Column,
127 v.funcs = append(v.funcs, fe)
132 // coverage returns the fraction of the statements in the function that were covered, as a numerator and denominator.
133 func (f *FuncExtent) coverage(profile *cover.Profile) (num, den int64) {
134 // We could avoid making this n^2 overall by doing a single scan and annotating the functions,
135 // but the sizes of the data structures is never very large and the scan is almost instantaneous.
136 var covered, total int64
137 // The blocks are sorted, so we can stop counting as soon as we reach the end of the relevant block.
138 for _, b := range profile.Blocks {
139 if b.StartLine > f.endLine || (b.StartLine == f.endLine && b.StartCol >= f.endCol) {
140 // Past the end of the function.
143 if b.EndLine < f.startLine || (b.EndLine == f.startLine && b.EndCol <= f.startCol) {
144 // Before the beginning of the function
147 total += int64(b.NumStmt)
149 covered += int64(b.NumStmt)
153 total = 1 // Avoid zero denominator.
155 return covered, total
158 // findFile finds the location of the named file in GOROOT, GOPATH etc.
159 func findFile(file string) (string, error) {
160 dir, file := filepath.Split(file)
161 pkg, err := build.Import(dir, ".", build.FindOnly)
163 return "", fmt.Errorf("can't find %q: %v", file, err)
165 return filepath.Join(pkg.Dir, file), nil