github.com/april1989/origin-go-tools@v0.0.32/cmd/cover/cover.go (about) 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 package main 6 7 import ( 8 "bytes" 9 "flag" 10 "fmt" 11 "go/ast" 12 "go/parser" 13 "go/printer" 14 "go/token" 15 "io" 16 "io/ioutil" 17 "log" 18 "os" 19 "path/filepath" 20 "sort" 21 "strconv" 22 "strings" 23 ) 24 25 const usageMessage = "" + 26 `Usage of 'go tool cover': 27 Given a coverage profile produced by 'go test': 28 go test -coverprofile=c.out 29 30 Open a web browser displaying annotated source code: 31 go tool cover -html=c.out 32 33 Write out an HTML file instead of launching a web browser: 34 go tool cover -html=c.out -o coverage.html 35 36 Display coverage percentages to stdout for each function: 37 go tool cover -func=c.out 38 39 Finally, to generate modified source code with coverage annotations 40 (what go test -cover does): 41 go tool cover -mode=set -var=CoverageVariableName program.go 42 ` 43 44 func usage() { 45 fmt.Fprintln(os.Stderr, usageMessage) 46 fmt.Fprintln(os.Stderr, "Flags:") 47 flag.PrintDefaults() 48 fmt.Fprintln(os.Stderr, "\n Only one of -html, -func, or -mode may be set.") 49 os.Exit(2) 50 } 51 52 var ( 53 mode = flag.String("mode", "", "coverage mode: set, count, atomic") 54 varVar = flag.String("var", "GoCover", "name of coverage variable to generate") 55 output = flag.String("o", "", "file for output; default: stdout") 56 htmlOut = flag.String("html", "", "generate HTML representation of coverage profile") 57 funcOut = flag.String("func", "", "output coverage profile information for each function") 58 ) 59 60 var profile string // The profile to read; the value of -html or -func 61 62 var counterStmt func(*File, ast.Expr) ast.Stmt 63 64 const ( 65 atomicPackagePath = "sync/atomic" 66 atomicPackageName = "_cover_atomic_" 67 ) 68 69 func main() { 70 flag.Usage = usage 71 flag.Parse() 72 73 // Usage information when no arguments. 74 if flag.NFlag() == 0 && flag.NArg() == 0 { 75 flag.Usage() 76 } 77 78 err := parseFlags() 79 if err != nil { 80 fmt.Fprintln(os.Stderr, err) 81 fmt.Fprintln(os.Stderr, `For usage information, run "go tool cover -help"`) 82 os.Exit(2) 83 } 84 85 // Generate coverage-annotated source. 86 if *mode != "" { 87 annotate(flag.Arg(0)) 88 return 89 } 90 91 // Output HTML or function coverage information. 92 if *htmlOut != "" { 93 err = htmlOutput(profile, *output) 94 } else { 95 err = funcOutput(profile, *output) 96 } 97 98 if err != nil { 99 fmt.Fprintf(os.Stderr, "cover: %v\n", err) 100 os.Exit(2) 101 } 102 } 103 104 // parseFlags sets the profile and counterStmt globals and performs validations. 105 func parseFlags() error { 106 profile = *htmlOut 107 if *funcOut != "" { 108 if profile != "" { 109 return fmt.Errorf("too many options") 110 } 111 profile = *funcOut 112 } 113 114 // Must either display a profile or rewrite Go source. 115 if (profile == "") == (*mode == "") { 116 return fmt.Errorf("too many options") 117 } 118 119 if *mode != "" { 120 switch *mode { 121 case "set": 122 counterStmt = setCounterStmt 123 case "count": 124 counterStmt = incCounterStmt 125 case "atomic": 126 counterStmt = atomicCounterStmt 127 default: 128 return fmt.Errorf("unknown -mode %v", *mode) 129 } 130 131 if flag.NArg() == 0 { 132 return fmt.Errorf("missing source file") 133 } else if flag.NArg() == 1 { 134 return nil 135 } 136 } else if flag.NArg() == 0 { 137 return nil 138 } 139 return fmt.Errorf("too many arguments") 140 } 141 142 // Block represents the information about a basic block to be recorded in the analysis. 143 // Note: Our definition of basic block is based on control structures; we don't break 144 // apart && and ||. We could but it doesn't seem important enough to bother. 145 type Block struct { 146 startByte token.Pos 147 endByte token.Pos 148 numStmt int 149 } 150 151 // File is a wrapper for the state of a file used in the parser. 152 // The basic parse tree walker is a method of this type. 153 type File struct { 154 fset *token.FileSet 155 name string // Name of file. 156 astFile *ast.File 157 blocks []Block 158 atomicPkg string // Package name for "sync/atomic" in this file. 159 } 160 161 // Visit implements the ast.Visitor interface. 162 func (f *File) Visit(node ast.Node) ast.Visitor { 163 switch n := node.(type) { 164 case *ast.BlockStmt: 165 // If it's a switch or select, the body is a list of case clauses; don't tag the block itself. 166 if len(n.List) > 0 { 167 switch n.List[0].(type) { 168 case *ast.CaseClause: // switch 169 for _, n := range n.List { 170 clause := n.(*ast.CaseClause) 171 clause.Body = f.addCounters(clause.Pos(), clause.End(), clause.Body, false) 172 } 173 return f 174 case *ast.CommClause: // select 175 for _, n := range n.List { 176 clause := n.(*ast.CommClause) 177 clause.Body = f.addCounters(clause.Pos(), clause.End(), clause.Body, false) 178 } 179 return f 180 } 181 } 182 n.List = f.addCounters(n.Lbrace, n.Rbrace+1, n.List, true) // +1 to step past closing brace. 183 case *ast.IfStmt: 184 ast.Walk(f, n.Body) 185 if n.Else == nil { 186 return nil 187 } 188 // The elses are special, because if we have 189 // if x { 190 // } else if y { 191 // } 192 // we want to cover the "if y". To do this, we need a place to drop the counter, 193 // so we add a hidden block: 194 // if x { 195 // } else { 196 // if y { 197 // } 198 // } 199 switch stmt := n.Else.(type) { 200 case *ast.IfStmt: 201 block := &ast.BlockStmt{ 202 Lbrace: n.Body.End(), // Start at end of the "if" block so the covered part looks like it starts at the "else". 203 List: []ast.Stmt{stmt}, 204 Rbrace: stmt.End(), 205 } 206 n.Else = block 207 case *ast.BlockStmt: 208 stmt.Lbrace = n.Body.End() // Start at end of the "if" block so the covered part looks like it starts at the "else". 209 default: 210 panic("unexpected node type in if") 211 } 212 ast.Walk(f, n.Else) 213 return nil 214 case *ast.SelectStmt: 215 // Don't annotate an empty select - creates a syntax error. 216 if n.Body == nil || len(n.Body.List) == 0 { 217 return nil 218 } 219 case *ast.SwitchStmt: 220 // Don't annotate an empty switch - creates a syntax error. 221 if n.Body == nil || len(n.Body.List) == 0 { 222 return nil 223 } 224 case *ast.TypeSwitchStmt: 225 // Don't annotate an empty type switch - creates a syntax error. 226 if n.Body == nil || len(n.Body.List) == 0 { 227 return nil 228 } 229 } 230 return f 231 } 232 233 // unquote returns the unquoted string. 234 func unquote(s string) string { 235 t, err := strconv.Unquote(s) 236 if err != nil { 237 log.Fatalf("cover: improperly quoted string %q\n", s) 238 } 239 return t 240 } 241 242 // addImport adds an import for the specified path, if one does not already exist, and returns 243 // the local package name. 244 func (f *File) addImport(path string) string { 245 // Does the package already import it? 246 for _, s := range f.astFile.Imports { 247 if unquote(s.Path.Value) == path { 248 if s.Name != nil { 249 return s.Name.Name 250 } 251 return filepath.Base(path) 252 } 253 } 254 newImport := &ast.ImportSpec{ 255 Name: ast.NewIdent(atomicPackageName), 256 Path: &ast.BasicLit{ 257 Kind: token.STRING, 258 Value: fmt.Sprintf("%q", path), 259 }, 260 } 261 impDecl := &ast.GenDecl{ 262 Tok: token.IMPORT, 263 Specs: []ast.Spec{ 264 newImport, 265 }, 266 } 267 // Make the new import the first Decl in the file. 268 astFile := f.astFile 269 astFile.Decls = append(astFile.Decls, nil) 270 copy(astFile.Decls[1:], astFile.Decls[0:]) 271 astFile.Decls[0] = impDecl 272 astFile.Imports = append(astFile.Imports, newImport) 273 274 // Now refer to the package, just in case it ends up unused. 275 // That is, append to the end of the file the declaration 276 // var _ = _cover_atomic_.AddUint32 277 reference := &ast.GenDecl{ 278 Tok: token.VAR, 279 Specs: []ast.Spec{ 280 &ast.ValueSpec{ 281 Names: []*ast.Ident{ 282 ast.NewIdent("_"), 283 }, 284 Values: []ast.Expr{ 285 &ast.SelectorExpr{ 286 X: ast.NewIdent(atomicPackageName), 287 Sel: ast.NewIdent("AddUint32"), 288 }, 289 }, 290 }, 291 }, 292 } 293 astFile.Decls = append(astFile.Decls, reference) 294 return atomicPackageName 295 } 296 297 var slashslash = []byte("//") 298 299 // initialComments returns the prefix of content containing only 300 // whitespace and line comments. Any +build directives must appear 301 // within this region. This approach is more reliable than using 302 // go/printer to print a modified AST containing comments. 303 // 304 func initialComments(content []byte) []byte { 305 // Derived from go/build.Context.shouldBuild. 306 end := 0 307 p := content 308 for len(p) > 0 { 309 line := p 310 if i := bytes.IndexByte(line, '\n'); i >= 0 { 311 line, p = line[:i], p[i+1:] 312 } else { 313 p = p[len(p):] 314 } 315 line = bytes.TrimSpace(line) 316 if len(line) == 0 { // Blank line. 317 end = len(content) - len(p) 318 continue 319 } 320 if !bytes.HasPrefix(line, slashslash) { // Not comment line. 321 break 322 } 323 } 324 return content[:end] 325 } 326 327 func annotate(name string) { 328 fset := token.NewFileSet() 329 content, err := ioutil.ReadFile(name) 330 if err != nil { 331 log.Fatalf("cover: %s: %s", name, err) 332 } 333 parsedFile, err := parser.ParseFile(fset, name, content, parser.ParseComments) 334 if err != nil { 335 log.Fatalf("cover: %s: %s", name, err) 336 } 337 parsedFile.Comments = trimComments(parsedFile, fset) 338 339 file := &File{ 340 fset: fset, 341 name: name, 342 astFile: parsedFile, 343 } 344 if *mode == "atomic" { 345 file.atomicPkg = file.addImport(atomicPackagePath) 346 } 347 ast.Walk(file, file.astFile) 348 fd := os.Stdout 349 if *output != "" { 350 var err error 351 fd, err = os.Create(*output) 352 if err != nil { 353 log.Fatalf("cover: %s", err) 354 } 355 } 356 fd.Write(initialComments(content)) // Retain '// +build' directives. 357 file.print(fd) 358 // After printing the source tree, add some declarations for the counters etc. 359 // We could do this by adding to the tree, but it's easier just to print the text. 360 file.addVariables(fd) 361 } 362 363 // trimComments drops all but the //go: comments, some of which are semantically important. 364 // We drop all others because they can appear in places that cause our counters 365 // to appear in syntactically incorrect places. //go: appears at the beginning of 366 // the line and is syntactically safe. 367 func trimComments(file *ast.File, fset *token.FileSet) []*ast.CommentGroup { 368 var comments []*ast.CommentGroup 369 for _, group := range file.Comments { 370 var list []*ast.Comment 371 for _, comment := range group.List { 372 if strings.HasPrefix(comment.Text, "//go:") && fset.Position(comment.Slash).Column == 1 { 373 list = append(list, comment) 374 } 375 } 376 if list != nil { 377 comments = append(comments, &ast.CommentGroup{List: list}) 378 } 379 } 380 return comments 381 } 382 383 func (f *File) print(w io.Writer) { 384 printer.Fprint(w, f.fset, f.astFile) 385 } 386 387 // intLiteral returns an ast.BasicLit representing the integer value. 388 func (f *File) intLiteral(i int) *ast.BasicLit { 389 node := &ast.BasicLit{ 390 Kind: token.INT, 391 Value: fmt.Sprint(i), 392 } 393 return node 394 } 395 396 // index returns an ast.BasicLit representing the number of counters present. 397 func (f *File) index() *ast.BasicLit { 398 return f.intLiteral(len(f.blocks)) 399 } 400 401 // setCounterStmt returns the expression: __count[23] = 1. 402 func setCounterStmt(f *File, counter ast.Expr) ast.Stmt { 403 return &ast.AssignStmt{ 404 Lhs: []ast.Expr{counter}, 405 Tok: token.ASSIGN, 406 Rhs: []ast.Expr{f.intLiteral(1)}, 407 } 408 } 409 410 // incCounterStmt returns the expression: __count[23]++. 411 func incCounterStmt(f *File, counter ast.Expr) ast.Stmt { 412 return &ast.IncDecStmt{ 413 X: counter, 414 Tok: token.INC, 415 } 416 } 417 418 // atomicCounterStmt returns the expression: atomic.AddUint32(&__count[23], 1) 419 func atomicCounterStmt(f *File, counter ast.Expr) ast.Stmt { 420 return &ast.ExprStmt{ 421 X: &ast.CallExpr{ 422 Fun: &ast.SelectorExpr{ 423 X: ast.NewIdent(f.atomicPkg), 424 Sel: ast.NewIdent("AddUint32"), 425 }, 426 Args: []ast.Expr{&ast.UnaryExpr{ 427 Op: token.AND, 428 X: counter, 429 }, 430 f.intLiteral(1), 431 }, 432 }, 433 } 434 } 435 436 // newCounter creates a new counter expression of the appropriate form. 437 func (f *File) newCounter(start, end token.Pos, numStmt int) ast.Stmt { 438 counter := &ast.IndexExpr{ 439 X: &ast.SelectorExpr{ 440 X: ast.NewIdent(*varVar), 441 Sel: ast.NewIdent("Count"), 442 }, 443 Index: f.index(), 444 } 445 stmt := counterStmt(f, counter) 446 f.blocks = append(f.blocks, Block{start, end, numStmt}) 447 return stmt 448 } 449 450 // addCounters takes a list of statements and adds counters to the beginning of 451 // each basic block at the top level of that list. For instance, given 452 // 453 // S1 454 // if cond { 455 // S2 456 // } 457 // S3 458 // 459 // counters will be added before S1 and before S3. The block containing S2 460 // will be visited in a separate call. 461 // TODO: Nested simple blocks get unnecessary (but correct) counters 462 func (f *File) addCounters(pos, blockEnd token.Pos, list []ast.Stmt, extendToClosingBrace bool) []ast.Stmt { 463 // Special case: make sure we add a counter to an empty block. Can't do this below 464 // or we will add a counter to an empty statement list after, say, a return statement. 465 if len(list) == 0 { 466 return []ast.Stmt{f.newCounter(pos, blockEnd, 0)} 467 } 468 // We have a block (statement list), but it may have several basic blocks due to the 469 // appearance of statements that affect the flow of control. 470 var newList []ast.Stmt 471 for { 472 // Find first statement that affects flow of control (break, continue, if, etc.). 473 // It will be the last statement of this basic block. 474 var last int 475 end := blockEnd 476 for last = 0; last < len(list); last++ { 477 end = f.statementBoundary(list[last]) 478 if f.endsBasicSourceBlock(list[last]) { 479 extendToClosingBrace = false // Block is broken up now. 480 last++ 481 break 482 } 483 } 484 if extendToClosingBrace { 485 end = blockEnd 486 } 487 if pos != end { // Can have no source to cover if e.g. blocks abut. 488 newList = append(newList, f.newCounter(pos, end, last)) 489 } 490 newList = append(newList, list[0:last]...) 491 list = list[last:] 492 if len(list) == 0 { 493 break 494 } 495 pos = list[0].Pos() 496 } 497 return newList 498 } 499 500 // hasFuncLiteral reports the existence and position of the first func literal 501 // in the node, if any. If a func literal appears, it usually marks the termination 502 // of a basic block because the function body is itself a block. 503 // Therefore we draw a line at the start of the body of the first function literal we find. 504 // TODO: what if there's more than one? Probably doesn't matter much. 505 func hasFuncLiteral(n ast.Node) (bool, token.Pos) { 506 if n == nil { 507 return false, 0 508 } 509 var literal funcLitFinder 510 ast.Walk(&literal, n) 511 return literal.found(), token.Pos(literal) 512 } 513 514 // statementBoundary finds the location in s that terminates the current basic 515 // block in the source. 516 func (f *File) statementBoundary(s ast.Stmt) token.Pos { 517 // Control flow statements are easy. 518 switch s := s.(type) { 519 case *ast.BlockStmt: 520 // Treat blocks like basic blocks to avoid overlapping counters. 521 return s.Lbrace 522 case *ast.IfStmt: 523 found, pos := hasFuncLiteral(s.Init) 524 if found { 525 return pos 526 } 527 found, pos = hasFuncLiteral(s.Cond) 528 if found { 529 return pos 530 } 531 return s.Body.Lbrace 532 case *ast.ForStmt: 533 found, pos := hasFuncLiteral(s.Init) 534 if found { 535 return pos 536 } 537 found, pos = hasFuncLiteral(s.Cond) 538 if found { 539 return pos 540 } 541 found, pos = hasFuncLiteral(s.Post) 542 if found { 543 return pos 544 } 545 return s.Body.Lbrace 546 case *ast.LabeledStmt: 547 return f.statementBoundary(s.Stmt) 548 case *ast.RangeStmt: 549 found, pos := hasFuncLiteral(s.X) 550 if found { 551 return pos 552 } 553 return s.Body.Lbrace 554 case *ast.SwitchStmt: 555 found, pos := hasFuncLiteral(s.Init) 556 if found { 557 return pos 558 } 559 found, pos = hasFuncLiteral(s.Tag) 560 if found { 561 return pos 562 } 563 return s.Body.Lbrace 564 case *ast.SelectStmt: 565 return s.Body.Lbrace 566 case *ast.TypeSwitchStmt: 567 found, pos := hasFuncLiteral(s.Init) 568 if found { 569 return pos 570 } 571 return s.Body.Lbrace 572 } 573 // If not a control flow statement, it is a declaration, expression, call, etc. and it may have a function literal. 574 // If it does, that's tricky because we want to exclude the body of the function from this block. 575 // Draw a line at the start of the body of the first function literal we find. 576 // TODO: what if there's more than one? Probably doesn't matter much. 577 found, pos := hasFuncLiteral(s) 578 if found { 579 return pos 580 } 581 return s.End() 582 } 583 584 // endsBasicSourceBlock reports whether s changes the flow of control: break, if, etc., 585 // or if it's just problematic, for instance contains a function literal, which will complicate 586 // accounting due to the block-within-an expression. 587 func (f *File) endsBasicSourceBlock(s ast.Stmt) bool { 588 switch s := s.(type) { 589 case *ast.BlockStmt: 590 // Treat blocks like basic blocks to avoid overlapping counters. 591 return true 592 case *ast.BranchStmt: 593 return true 594 case *ast.ForStmt: 595 return true 596 case *ast.IfStmt: 597 return true 598 case *ast.LabeledStmt: 599 return f.endsBasicSourceBlock(s.Stmt) 600 case *ast.RangeStmt: 601 return true 602 case *ast.SwitchStmt: 603 return true 604 case *ast.SelectStmt: 605 return true 606 case *ast.TypeSwitchStmt: 607 return true 608 case *ast.ExprStmt: 609 // Calls to panic change the flow. 610 // We really should verify that "panic" is the predefined function, 611 // but without type checking we can't and the likelihood of it being 612 // an actual problem is vanishingly small. 613 if call, ok := s.X.(*ast.CallExpr); ok { 614 if ident, ok := call.Fun.(*ast.Ident); ok && ident.Name == "panic" && len(call.Args) == 1 { 615 return true 616 } 617 } 618 } 619 found, _ := hasFuncLiteral(s) 620 return found 621 } 622 623 // funcLitFinder implements the ast.Visitor pattern to find the location of any 624 // function literal in a subtree. 625 type funcLitFinder token.Pos 626 627 func (f *funcLitFinder) Visit(node ast.Node) (w ast.Visitor) { 628 if f.found() { 629 return nil // Prune search. 630 } 631 switch n := node.(type) { 632 case *ast.FuncLit: 633 *f = funcLitFinder(n.Body.Lbrace) 634 return nil // Prune search. 635 } 636 return f 637 } 638 639 func (f *funcLitFinder) found() bool { 640 return token.Pos(*f) != token.NoPos 641 } 642 643 // Sort interface for []block1; used for self-check in addVariables. 644 645 type block1 struct { 646 Block 647 index int 648 } 649 650 type blockSlice []block1 651 652 func (b blockSlice) Len() int { return len(b) } 653 func (b blockSlice) Less(i, j int) bool { return b[i].startByte < b[j].startByte } 654 func (b blockSlice) Swap(i, j int) { b[i], b[j] = b[j], b[i] } 655 656 // offset translates a token position into a 0-indexed byte offset. 657 func (f *File) offset(pos token.Pos) int { 658 return f.fset.Position(pos).Offset 659 } 660 661 // addVariables adds to the end of the file the declarations to set up the counter and position variables. 662 func (f *File) addVariables(w io.Writer) { 663 // Self-check: Verify that the instrumented basic blocks are disjoint. 664 t := make([]block1, len(f.blocks)) 665 for i := range f.blocks { 666 t[i].Block = f.blocks[i] 667 t[i].index = i 668 } 669 sort.Sort(blockSlice(t)) 670 for i := 1; i < len(t); i++ { 671 if t[i-1].endByte > t[i].startByte { 672 fmt.Fprintf(os.Stderr, "cover: internal error: block %d overlaps block %d\n", t[i-1].index, t[i].index) 673 // Note: error message is in byte positions, not token positions. 674 fmt.Fprintf(os.Stderr, "\t%s:#%d,#%d %s:#%d,#%d\n", 675 f.name, f.offset(t[i-1].startByte), f.offset(t[i-1].endByte), 676 f.name, f.offset(t[i].startByte), f.offset(t[i].endByte)) 677 } 678 } 679 680 // Declare the coverage struct as a package-level variable. 681 fmt.Fprintf(w, "\nvar %s = struct {\n", *varVar) 682 fmt.Fprintf(w, "\tCount [%d]uint32\n", len(f.blocks)) 683 fmt.Fprintf(w, "\tPos [3 * %d]uint32\n", len(f.blocks)) 684 fmt.Fprintf(w, "\tNumStmt [%d]uint16\n", len(f.blocks)) 685 fmt.Fprintf(w, "} {\n") 686 687 // Initialize the position array field. 688 fmt.Fprintf(w, "\tPos: [3 * %d]uint32{\n", len(f.blocks)) 689 690 // A nice long list of positions. Each position is encoded as follows to reduce size: 691 // - 32-bit starting line number 692 // - 32-bit ending line number 693 // - (16 bit ending column number << 16) | (16-bit starting column number). 694 for i, block := range f.blocks { 695 start := f.fset.Position(block.startByte) 696 end := f.fset.Position(block.endByte) 697 fmt.Fprintf(w, "\t\t%d, %d, %#x, // [%d]\n", start.Line, end.Line, (end.Column&0xFFFF)<<16|(start.Column&0xFFFF), i) 698 } 699 700 // Close the position array. 701 fmt.Fprintf(w, "\t},\n") 702 703 // Initialize the position array field. 704 fmt.Fprintf(w, "\tNumStmt: [%d]uint16{\n", len(f.blocks)) 705 706 // A nice long list of statements-per-block, so we can give a conventional 707 // valuation of "percent covered". To save space, it's a 16-bit number, so we 708 // clamp it if it overflows - won't matter in practice. 709 for i, block := range f.blocks { 710 n := block.numStmt 711 if n > 1<<16-1 { 712 n = 1<<16 - 1 713 } 714 fmt.Fprintf(w, "\t\t%d, // %d\n", n, i) 715 } 716 717 // Close the statements-per-block array. 718 fmt.Fprintf(w, "\t},\n") 719 720 // Close the struct initialization. 721 fmt.Fprintf(w, "}\n") 722 }