| 1 | // Copyright 2024 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 checker |
| 6 | |
| 7 | // This file defines helpers for printing analysis results. |
| 8 | // They should all be pure functions. |
| 9 | |
| 10 | import ( |
| 11 | "bytes" |
| 12 | "fmt" |
| 13 | "go/token" |
| 14 | "io" |
| 15 | |
| 16 | "golang.org/x/tools/go/analysis" |
| 17 | "golang.org/x/tools/internal/analysis/driverutil" |
| 18 | ) |
| 19 | |
| 20 | // PrintText emits diagnostics as plain text to w. |
| 21 | // |
| 22 | // If contextLines is nonnegative, it also prints the |
| 23 | // offending line, plus that many lines of context |
| 24 | // before and after the line. |
| 25 | func (g *Graph) PrintText(w io.Writer, contextLines int) error { |
| 26 | return writeTextDiagnostics(w, g.Roots, contextLines) |
| 27 | } |
| 28 | |
| 29 | func writeTextDiagnostics(w io.Writer, roots []*Action, contextLines int) error { |
| 30 | // De-duplicate diagnostics by position (not token.Pos) to |
| 31 | // avoid double-reporting in source files that belong to |
| 32 | // multiple packages, such as foo and foo.test. |
| 33 | // (We cannot assume that such repeated files were parsed |
| 34 | // only once and use syntax nodes as the key.) |
| 35 | type key struct { |
| 36 | pos token.Position |
| 37 | end token.Position |
| 38 | *analysis.Analyzer |
| 39 | message string |
| 40 | } |
| 41 | seen := make(map[key]bool) |
| 42 | |
| 43 | // TODO(adonovan): opt: plumb errors back from PrintPlain and avoid buffer. |
| 44 | buf := new(bytes.Buffer) |
| 45 | forEach(roots, func(act *Action) error { |
| 46 | if act.Err != nil { |
| 47 | fmt.Fprintf(w, "%s: %v\n", act.Analyzer.Name, act.Err) |
| 48 | } else if act.IsRoot { |
| 49 | for _, diag := range act.Diagnostics { |
| 50 | // We don't display Analyzer.Name/diag.Category |
| 51 | // as most users don't care. |
| 52 | |
| 53 | posn := act.Package.Fset.Position(diag.Pos) |
| 54 | end := act.Package.Fset.Position(diag.End) |
| 55 | k := key{posn, end, act.Analyzer, diag.Message} |
| 56 | if seen[k] { |
| 57 | continue // duplicate |
| 58 | } |
| 59 | seen[k] = true |
| 60 | |
| 61 | driverutil.PrintPlain(buf, act.Package.Fset, contextLines, diag) |
| 62 | } |
| 63 | } |
| 64 | return nil |
| 65 | }) |
| 66 | _, err := w.Write(buf.Bytes()) |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | // PrintJSON emits diagnostics in JSON form to w. |
| 71 | // Diagnostics are shown only for the root nodes, |
| 72 | // but errors (if any) are shown for all dependencies. |
| 73 | func (g *Graph) PrintJSON(w io.Writer) error { |
| 74 | return writeJSONDiagnostics(w, g.Roots) |
| 75 | } |
| 76 | |
| 77 | func writeJSONDiagnostics(w io.Writer, roots []*Action) error { |
| 78 | tree := make(driverutil.JSONTree) |
| 79 | forEach(roots, func(act *Action) error { |
| 80 | var diags []analysis.Diagnostic |
| 81 | if act.IsRoot { |
| 82 | diags = act.Diagnostics |
| 83 | } |
| 84 | tree.Add(act.Package.Fset, act.Package.ID, act.Analyzer.Name, diags, act.Err) |
| 85 | return nil |
| 86 | }) |
| 87 | return tree.Print(w) |
| 88 | } |