| 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 checkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/ |
| 6 | |
| 7 | // This file defines helpers for printing analysis results. |
| 8 | // They should all be pure functions. |
| 9 | |
| 10 | import ( |
| 11 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 12 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 13 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 14 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 15 | |
| 16 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 17 | "golang.org/x/tools/internal/analysis/driverutilscip-go gomod golang.org/x/tools . `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 *Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#) PrintTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#PrintText().(w ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Writerscip-go gomod github.com/golang/go/src go1.25.0 io/Writer#, contextLines int) error { |
| 26 | return writeTextDiagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/writeTextDiagnostics().(w, g.Rootsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#Roots., contextLines) |
| 27 | } |
| 28 | |
| 29 | func writeTextDiagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/writeTextDiagnostics().(w ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Writerscip-go gomod github.com/golang/go/src go1.25.0 io/Writer#, roots []*Actionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/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 tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position# |
| 37 | end tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position# |
| 38 | *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/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(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#) |
| 45 | forEachscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/forEach().(roots, func(act *Actionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#) error { |
| 46 | if act.Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Err. != nil { |
| 47 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "%s: %v\n", act.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Analyzer..Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name., act.Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Err.) |
| 48 | } else if act.IsRootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#IsRoot. { |
| 49 | for _, diag := range act.Diagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Diagnostics. { |
| 50 | // We don't display Analyzer.Name/diag.Category |
| 51 | // as most users don't care. |
| 52 | |
| 53 | posn := act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(diag.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Pos.) |
| 54 | end := act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset..Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(diag.Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#End.) |
| 55 | k := key{posn, end, act.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Analyzer., diag.Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Message.} |
| 56 | if seen[k] { |
| 57 | continue // duplicate |
| 58 | } |
| 59 | seen[k] = true |
| 60 | |
| 61 | driverutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/.PrintPlainscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/PrintPlain().(buf, act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset., contextLines, diag) |
| 62 | } |
| 63 | } |
| 64 | return nil |
| 65 | }) |
| 66 | _, err := w.Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().(buf.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#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 *Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#) PrintJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#PrintJSON().(w ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Writerscip-go gomod github.com/golang/go/src go1.25.0 io/Writer#) error { |
| 74 | return writeJSONDiagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/writeJSONDiagnostics().(w, g.Rootsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Graph#Roots.) |
| 75 | } |
| 76 | |
| 77 | func writeJSONDiagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/writeJSONDiagnostics().(w ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Writerscip-go gomod github.com/golang/go/src go1.25.0 io/Writer#, roots []*Actionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#) error { |
| 78 | tree := make(driverutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/.JSONTreescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/JSONTree#) |
| 79 | forEachscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/forEach().(roots, func(act *Actionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#) error { |
| 80 | var diags []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Diagnosticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic# |
| 81 | if act.IsRootscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#IsRoot. { |
| 82 | diags = act.Diagnosticsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Diagnostics. |
| 83 | } |
| 84 | tree.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/JSONTree#Add().(act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#Fset., act.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Package..IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package#ID., act.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Analyzer..Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name., diags, act.Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/checker`/Action#Err.) |
| 85 | return nil |
| 86 | }) |
| 87 | return tree.Printscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/driverutil`/JSONTree#Print().(w) |
| 88 | } |