...
Run Format

Source file src/cmd/cover/func.go

Documentation: cmd/cover

     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.
     4  
     5  // This file implements the visitor that computes the (line, column)-(line-column) range for each function.
     6  
     7  package main
     8  
     9  import (
    10  	"bufio"
    11  	"fmt"
    12  	"go/ast"
    13  	"go/build"
    14  	"go/parser"
    15  	"go/token"
    16  	"os"
    17  	"path/filepath"
    18  	"text/tabwriter"
    19  )
    20  
    21  // funcOutput takes two file names as arguments, a coverage profile to read as input and an output
    22  // file to write ("" means to write to standard output). The function reads the profile and produces
    23  // as output the coverage data broken down by function, like this:
    24  //
    25  //	fmt/format.go:30:	init			100.0%
    26  //	fmt/format.go:57:	clearflags		100.0%
    27  //	...
    28  //	fmt/scan.go:1046:	doScan			100.0%
    29  //	fmt/scan.go:1075:	advance			96.2%
    30  //	fmt/scan.go:1119:	doScanf			96.8%
    31  //	total:		(statements)			91.9%
    32  
    33  func funcOutput(profile, outputFile string) error {
    34  	profiles, err := ParseProfiles(profile)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	var out *bufio.Writer
    40  	if outputFile == "" {
    41  		out = bufio.NewWriter(os.Stdout)
    42  	} else {
    43  		fd, err := os.Create(outputFile)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		defer fd.Close()
    48  		out = bufio.NewWriter(fd)
    49  	}
    50  	defer out.Flush()
    51  
    52  	tabber := tabwriter.NewWriter(out, 1, 8, 1, '\t', 0)
    53  	defer tabber.Flush()
    54  
    55  	var total, covered int64
    56  	for _, profile := range profiles {
    57  		fn := profile.FileName
    58  		file, err := findFile(fn)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		funcs, err := findFuncs(file)
    63  		if err != nil {
    64  			return err
    65  		}
    66  		// Now match up functions and profile blocks.
    67  		for _, f := range funcs {
    68  			c, t := f.coverage(profile)
    69  			fmt.Fprintf(tabber, "%s:%d:\t%s\t%.1f%%\n", fn, f.startLine, f.name, percent(c, t))
    70  			total += t
    71  			covered += c
    72  		}
    73  	}
    74  	fmt.Fprintf(tabber, "total:\t(statements)\t%.1f%%\n", percent(covered, total))
    75  
    76  	return nil
    77  }
    78  
    79  // findFuncs parses the file and returns a slice of FuncExtent descriptors.
    80  func findFuncs(name string) ([]*FuncExtent, error) {
    81  	fset := token.NewFileSet()
    82  	parsedFile, err := parser.ParseFile(fset, name, nil, 0)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	visitor := &FuncVisitor{
    87  		fset:    fset,
    88  		name:    name,
    89  		astFile: parsedFile,
    90  	}
    91  	ast.Walk(visitor, visitor.astFile)
    92  	return visitor.funcs, nil
    93  }
    94  
    95  // FuncExtent describes a function's extent in the source by file and position.
    96  type FuncExtent struct {
    97  	name      string
    98  	startLine int
    99  	startCol  int
   100  	endLine   int
   101  	endCol    int
   102  }
   103  
   104  // FuncVisitor implements the visitor that builds the function position list for a file.
   105  type FuncVisitor struct {
   106  	fset    *token.FileSet
   107  	name    string // Name of file.
   108  	astFile *ast.File
   109  	funcs   []*FuncExtent
   110  }
   111  
   112  // Visit implements the ast.Visitor interface.
   113  func (v *FuncVisitor) Visit(node ast.Node) ast.Visitor {
   114  	switch n := node.(type) {
   115  	case *ast.FuncDecl:
   116  		if n.Body == nil {
   117  			// Do not count declarations of assembly functions.
   118  			break
   119  		}
   120  		start := v.fset.Position(n.Pos())
   121  		end := v.fset.Position(n.End())
   122  		fe := &FuncExtent{
   123  			name:      n.Name.Name,
   124  			startLine: start.Line,
   125  			startCol:  start.Column,
   126  			endLine:   end.Line,
   127  			endCol:    end.Column,
   128  		}
   129  		v.funcs = append(v.funcs, fe)
   130  	}
   131  	return v
   132  }
   133  
   134  // coverage returns the fraction of the statements in the function that were covered, as a numerator and denominator.
   135  func (f *FuncExtent) coverage(profile *Profile) (num, den int64) {
   136  	// We could avoid making this n^2 overall by doing a single scan and annotating the functions,
   137  	// but the sizes of the data structures is never very large and the scan is almost instantaneous.
   138  	var covered, total int64
   139  	// The blocks are sorted, so we can stop counting as soon as we reach the end of the relevant block.
   140  	for _, b := range profile.Blocks {
   141  		if b.StartLine > f.endLine || (b.StartLine == f.endLine && b.StartCol >= f.endCol) {
   142  			// Past the end of the function.
   143  			break
   144  		}
   145  		if b.EndLine < f.startLine || (b.EndLine == f.startLine && b.EndCol <= f.startCol) {
   146  			// Before the beginning of the function
   147  			continue
   148  		}
   149  		total += int64(b.NumStmt)
   150  		if b.Count > 0 {
   151  			covered += int64(b.NumStmt)
   152  		}
   153  	}
   154  	return covered, total
   155  }
   156  
   157  // findFile finds the location of the named file in GOROOT, GOPATH etc.
   158  func findFile(file string) (string, error) {
   159  	dir, file := filepath.Split(file)
   160  	pkg, err := build.Import(dir, ".", build.FindOnly)
   161  	if err != nil {
   162  		return "", fmt.Errorf("can't find %q: %v", file, err)
   163  	}
   164  	return filepath.Join(pkg.Dir, file), nil
   165  }
   166  
   167  func percent(covered, total int64) float64 {
   168  	if total == 0 {
   169  		total = 1 // Avoid zero denominator.
   170  	}
   171  	return 100.0 * float64(covered) / float64(total)
   172  }
   173  

View as plain text