From 8fc1f80844007250293bf98af8072637e0d24b20 Mon Sep 17 00:00:00 2001 From: madneal Date: Sat, 13 Jul 2024 13:36:45 +0800 Subject: [PATCH 1/3] add end position for JSON analysis output --- go/analysis/internal/analysisflags/flags.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go/analysis/internal/analysisflags/flags.go b/go/analysis/internal/analysisflags/flags.go index ff14ff58f9c..b18a6a6ff1f 100644 --- a/go/analysis/internal/analysisflags/flags.go +++ b/go/analysis/internal/analysisflags/flags.go @@ -363,11 +363,10 @@ type JSONSuggestedFix struct { } // A JSONDiagnostic describes the JSON schema of an analysis.Diagnostic. -// -// TODO(matloob): include End position if present. type JSONDiagnostic struct { Category string `json:"category,omitempty"` Posn string `json:"posn"` // e.g. "file.go:line:column" + EndPosn string `json:"end_posn,omitempty"` Message string `json:"message"` SuggestedFixes []JSONSuggestedFix `json:"suggested_fixes,omitempty"` Related []JSONRelatedInformation `json:"related,omitempty"` @@ -375,10 +374,9 @@ type JSONDiagnostic struct { // A JSONRelated describes a secondary position and message related to // a primary diagnostic. -// -// TODO(adonovan): include End position if present. type JSONRelatedInformation struct { Posn string `json:"posn"` // e.g. "file.go:line:column" + EndPosn string `json:"end_posn,omitempty"` Message string `json:"message"` } @@ -414,12 +412,14 @@ func (tree JSONTree) Add(fset *token.FileSet, id, name string, diags []analysis. for _, r := range f.Related { related = append(related, JSONRelatedInformation{ Posn: fset.Position(r.Pos).String(), + EndPosn: fset.Position(r.End).String(), Message: r.Message, }) } jdiag := JSONDiagnostic{ Category: f.Category, Posn: fset.Position(f.Pos).String(), + EndPosn: fset.Position(f.End).String(), Message: f.Message, SuggestedFixes: fixes, Related: related, From b9d688bab56dc7e62ece118703f8a05fe292b63b Mon Sep 17 00:00:00 2001 From: madneal Date: Sat, 27 Jul 2024 15:54:21 +0800 Subject: [PATCH 2/3] fix for the unittest --- go/analysis/unitchecker/unitchecker_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/go/analysis/unitchecker/unitchecker_test.go b/go/analysis/unitchecker/unitchecker_test.go index 54d8fa81851..68bd439fc5f 100644 --- a/go/analysis/unitchecker/unitchecker_test.go +++ b/go/analysis/unitchecker/unitchecker_test.go @@ -105,6 +105,7 @@ func _() { "findcall": \[ \{ "posn": "([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go:4:11", + "end_posn": "-", "message": "call of MyFunc123\(...\)", "suggested_fixes": \[ \{ @@ -130,6 +131,7 @@ func _() { "assign": \[ \{ "posn": "([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?c/c.go:5:5", + "end_posn": "-", "message": "self-assignment of i to i", "suggested_fixes": \[ \{ From 9bb63dd381d48e50d6575828dcd32b0cafeed6d1 Mon Sep 17 00:00:00 2001 From: madneal Date: Fri, 2 Aug 2024 16:14:35 +0800 Subject: [PATCH 3/3] ignore end_posn field when not present --- go/analysis/internal/analysisflags/flags.go | 13 +++++++++---- go/analysis/unitchecker/unitchecker_test.go | 2 -- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/go/analysis/internal/analysisflags/flags.go b/go/analysis/internal/analysisflags/flags.go index b18a6a6ff1f..8b3e0798250 100644 --- a/go/analysis/internal/analysisflags/flags.go +++ b/go/analysis/internal/analysisflags/flags.go @@ -410,20 +410,25 @@ func (tree JSONTree) Add(fset *token.FileSet, id, name string, diags []analysis. } var related []JSONRelatedInformation for _, r := range f.Related { - related = append(related, JSONRelatedInformation{ + relatedInfo := JSONRelatedInformation{ Posn: fset.Position(r.Pos).String(), - EndPosn: fset.Position(r.End).String(), Message: r.Message, - }) + } + if r.End != token.NoPos { + relatedInfo.EndPosn = fset.Position(r.End).String() + } + related = append(related, relatedInfo) } jdiag := JSONDiagnostic{ Category: f.Category, Posn: fset.Position(f.Pos).String(), - EndPosn: fset.Position(f.End).String(), Message: f.Message, SuggestedFixes: fixes, Related: related, } + if f.End != token.NoPos { + jdiag.EndPosn = fset.Position(f.End).String() + } diagnostics = append(diagnostics, jdiag) } v = diagnostics diff --git a/go/analysis/unitchecker/unitchecker_test.go b/go/analysis/unitchecker/unitchecker_test.go index 68bd439fc5f..54d8fa81851 100644 --- a/go/analysis/unitchecker/unitchecker_test.go +++ b/go/analysis/unitchecker/unitchecker_test.go @@ -105,7 +105,6 @@ func _() { "findcall": \[ \{ "posn": "([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?a/a.go:4:11", - "end_posn": "-", "message": "call of MyFunc123\(...\)", "suggested_fixes": \[ \{ @@ -131,7 +130,6 @@ func _() { "assign": \[ \{ "posn": "([/._\-a-zA-Z0-9]+[\\/]fake[\\/])?c/c.go:5:5", - "end_posn": "-", "message": "self-assignment of i to i", "suggested_fixes": \[ \{