printSource prints an annotated source listing, include all functions with samples that match the regexp rpt.options.symbol. The sources are sorted by function name and then by filename to eliminate potential nondeterminism.
(w io.Writer, rpt *Report)
| 41 | // The sources are sorted by function name and then by filename to |
| 42 | // eliminate potential nondeterminism. |
| 43 | func printSource(w io.Writer, rpt *Report) error { |
| 44 | o := rpt.options |
| 45 | g := rpt.newGraph(nil) |
| 46 | |
| 47 | // Identify all the functions that match the regexp provided. |
| 48 | // Group nodes for each matching function. |
| 49 | var functions graph.Nodes |
| 50 | functionNodes := make(map[string]graph.Nodes) |
| 51 | for _, n := range g.Nodes { |
| 52 | if !o.Symbol.MatchString(n.Info.Name) { |
| 53 | continue |
| 54 | } |
| 55 | if functionNodes[n.Info.Name] == nil { |
| 56 | functions = append(functions, n) |
| 57 | } |
| 58 | functionNodes[n.Info.Name] = append(functionNodes[n.Info.Name], n) |
| 59 | } |
| 60 | functions.Sort(graph.NameOrder) |
| 61 | |
| 62 | if len(functionNodes) == 0 { |
| 63 | return fmt.Errorf("no matches found for regexp: %s", o.Symbol) |
| 64 | } |
| 65 | |
| 66 | sourcePath := o.SourcePath |
| 67 | if sourcePath == "" { |
| 68 | wd, err := os.Getwd() |
| 69 | if err != nil { |
| 70 | return fmt.Errorf("could not stat current dir: %v", err) |
| 71 | } |
| 72 | sourcePath = wd |
| 73 | } |
| 74 | reader := newSourceReader(sourcePath, o.TrimPath) |
| 75 | |
| 76 | fmt.Fprintf(w, "Total: %s\n", rpt.formatValue(rpt.total)) |
| 77 | for _, fn := range functions { |
| 78 | name := fn.Info.Name |
| 79 | |
| 80 | // Identify all the source files associated to this function. |
| 81 | // Group nodes for each source file. |
| 82 | var sourceFiles graph.Nodes |
| 83 | fileNodes := make(map[string]graph.Nodes) |
| 84 | for _, n := range functionNodes[name] { |
| 85 | if n.Info.File == "" { |
| 86 | continue |
| 87 | } |
| 88 | if fileNodes[n.Info.File] == nil { |
| 89 | sourceFiles = append(sourceFiles, n) |
| 90 | } |
| 91 | fileNodes[n.Info.File] = append(fileNodes[n.Info.File], n) |
| 92 | } |
| 93 | |
| 94 | if len(sourceFiles) == 0 { |
| 95 | fmt.Fprintf(w, "No source information for %s\n", name) |
| 96 | continue |
| 97 | } |
| 98 | |
| 99 | sourceFiles.Sort(graph.FileOrder) |
| 100 |
no test coverage detected
searching dependent graphs…