Skip to content

Commit f2692ba

Browse files
adonovangopherbot
authored andcommitted
go/analysis/internal/analysisflags: print Diagnostic.Related too
+ script test Change-Id: Ic4c4dd6afcbe0268848ce37ce450000863b565e9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/692515 Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent f7d99c1 commit f2692ba

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

go/analysis/internal/analysisflags/flags.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -318,24 +318,32 @@ var vetLegacyFlags = map[string]string{
318318
// If contextLines is nonnegative, it also prints the
319319
// offending line plus this many lines of context.
320320
func PrintPlain(out io.Writer, fset *token.FileSet, contextLines int, diag analysis.Diagnostic) {
321-
posn := fset.Position(diag.Pos)
322-
fmt.Fprintf(out, "%s: %s\n", posn, diag.Message)
323-
324-
// show offending line plus N lines of context.
325-
if contextLines >= 0 {
326-
posn := fset.Position(diag.Pos)
327-
end := fset.Position(diag.End)
328-
if !end.IsValid() {
329-
end = posn
330-
}
331-
data, _ := os.ReadFile(posn.Filename)
332-
lines := strings.Split(string(data), "\n")
333-
for i := posn.Line - contextLines; i <= end.Line+contextLines; i++ {
334-
if 1 <= i && i <= len(lines) {
335-
fmt.Fprintf(out, "%d\t%s\n", i, lines[i-1])
321+
print := func(pos, end token.Pos, message string) {
322+
posn := fset.Position(pos)
323+
fmt.Fprintf(out, "%s: %s\n", posn, message)
324+
325+
// show offending line plus N lines of context.
326+
if contextLines >= 0 {
327+
end := fset.Position(end)
328+
if !end.IsValid() {
329+
end = posn
330+
}
331+
// TODO(adonovan): highlight the portion of the line indicated
332+
// by pos...end using ASCII art, terminal colors, etc?
333+
data, _ := os.ReadFile(posn.Filename)
334+
lines := strings.Split(string(data), "\n")
335+
for i := posn.Line - contextLines; i <= end.Line+contextLines; i++ {
336+
if 1 <= i && i <= len(lines) {
337+
fmt.Fprintf(out, "%d\t%s\n", i, lines[i-1])
338+
}
336339
}
337340
}
338341
}
342+
343+
print(diag.Pos, diag.End, diag.Message)
344+
for _, rel := range diag.Related {
345+
print(rel.Pos, rel.End, "\t"+rel.Message)
346+
}
339347
}
340348

341349
// A JSONTree is a mapping from package ID to analysis name to result.

go/analysis/internal/checker/fix_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func TestMain(m *testing.M) {
4242
markerAnalyzer,
4343
noendAnalyzer,
4444
renameAnalyzer,
45+
relatedAnalyzer,
4546
)
4647
panic("unreachable")
4748
}
@@ -587,6 +588,25 @@ var noendAnalyzer = &analysis.Analyzer{
587588
},
588589
}
589590

591+
var relatedAnalyzer = &analysis.Analyzer{
592+
Name: "related",
593+
Doc: "reports a Diagnostic with RelatedInformaiton",
594+
Run: func(pass *analysis.Pass) (any, error) {
595+
decl := pass.Files[0].Decls[0]
596+
pass.Report(analysis.Diagnostic{
597+
Pos: decl.Pos(),
598+
End: decl.Pos() + 1,
599+
Message: "decl starts here",
600+
Related: []analysis.RelatedInformation{{
601+
Message: "decl ends here",
602+
Pos: decl.End() - 1,
603+
End: decl.End(),
604+
}},
605+
})
606+
return nil, nil
607+
},
608+
}
609+
590610
// panics asserts that f() panics with with a value whose printed form matches the regexp want.
591611
func panics(t *testing.T, want string, f func()) {
592612
defer func() {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Test plain output.
2+
#
3+
# File slashes assume non-Windows.
4+
skip GOOS=windows
5+
6+
checker -related example.com/p
7+
stderr p/p.go:3:1: decl starts here
8+
stderr p/p.go:4:1: decl ends here
9+
10+
checker -related -c=0 example.com/p
11+
stderr p/p.go:3:1: decl starts here
12+
stderr 3 func f\(bar int\) {
13+
stderr p/p.go:4:1: decl ends here
14+
stderr 4 }
15+
exit 3
16+
17+
-- go.mod --
18+
module example.com
19+
go 1.22
20+
21+
-- p/p.go --
22+
package p
23+
24+
func f(bar int) {
25+
}

0 commit comments

Comments
 (0)