Skip to content

Commit

Permalink
internal/cmd/deadcode: use compiler-style diagnostics by default
Browse files Browse the repository at this point in the history
The previous format is now relegated to an example -f template
in the documentation.

Updates golang/go#63501

Change-Id: I29548121431282edbda07e3a75c131ac33104a6f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/542375
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
adonovan committed Nov 15, 2023
1 parent 0eb9468 commit 7f1f4eb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
21 changes: 16 additions & 5 deletions internal/cmd/deadcode/deadcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"runtime/pprof"
Expand Down Expand Up @@ -336,14 +337,15 @@ func main() {
}
if len(functions) > 0 {
packages = append(packages, jsonPackage{
Name: fns[0].Pkg.Pkg.Name(),
Path: pkgpath,
Funcs: functions,
})
}
}

// Default format: functions grouped by package.
format := `{{println .Path}}{{range .Funcs}}{{printf "\t%s\n" .Name}}{{end}}{{println}}`
// Default line-oriented format: "a/b/c.go:1:2: unreachable func: T.f"
format := `{{range .Funcs}}{{printf "%s: unreachable func: %s\n" .Position .Name}}{{end}}`
if *formatFlag != "" {
format = *formatFlag
}
Expand Down Expand Up @@ -553,8 +555,16 @@ func isStaticCall(edge *callgraph.Edge) bool {
return edge.Site != nil && edge.Site.Common().StaticCallee() != nil
}

var cwd, _ = os.Getwd()

func toJSONPosition(posn token.Position) jsonPosition {
return jsonPosition{posn.Filename, posn.Line, posn.Column}
// Use cwd-relative filename if possible.
filename := posn.Filename
if rel, err := filepath.Rel(cwd, filename); err == nil && !strings.HasPrefix(rel, "..") {
filename = rel
}

return jsonPosition{filename, posn.Line, posn.Column}
}

func cond[T any](cond bool, t, f T) T {
Expand All @@ -578,8 +588,9 @@ type jsonFunction struct {
func (f jsonFunction) String() string { return f.Name }

type jsonPackage struct {
Path string
Funcs []jsonFunction
Name string // declared name
Path string // full import path
Funcs []jsonFunction // non-empty list of package's dead functions
}

func (p jsonPackage) String() string { return p.Path }
Expand Down
25 changes: 17 additions & 8 deletions internal/cmd/deadcode/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,28 @@ easier to compute the intersection of results across all runs.
The command supports three output formats.
With no flags, the command prints dead functions grouped by package.
With no flags, the command prints the name and location of each dead
function in the form of a typical compiler diagnostic, for example:
$ deadcode -f='{{range .Funcs}}{{println .Position}}{{end}}' -test ./gopls/...
gopls/internal/lsp/command.go:1206:6: unreachable func: openClientEditor
gopls/internal/lsp/template/parse.go:414:18: unreachable func: Parsed.WriteNode
gopls/internal/lsp/template/parse.go:419:18: unreachable func: wrNode.writeNode
With the -json flag, the command prints an array of Package
objects, as defined by the JSON schema (see below).
With the -f=template flag, the command executes the specified template
on each Package record. So, this template produces a result similar to the
default format:
-f='{{println .Path}}{{range .Funcs}}{{printf "\t%s\n" .Name}}{{end}}{{println}}'
on each Package record. So, this template shows dead functions grouped
by package:
And this template shows only the list of source positions of dead functions:
$ deadcode -f='{{println .Path}}{{range .Funcs}}{{printf "\t%s\n" .Name}}{{end}}{{println}}' -test ./gopls/...
golang.org/x/tools/gopls/internal/lsp
openClientEditor
-f='{{range .Funcs}}{{println .Position}}{{end}}'
golang.org/x/tools/gopls/internal/lsp/template
Parsed.WriteNode
wrNode.writeNode
# Why is a function not dead?
Expand Down Expand Up @@ -104,7 +112,8 @@ is static or dynamic, and its source line number. For example:
# JSON schema
type Package struct {
Path string // import path of package
Name string // declared name
Path string // full import path
Funcs []Function // list of dead functions within it
}
Expand Down
18 changes: 9 additions & 9 deletions internal/cmd/deadcode/testdata/generated.txtar
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Test of -generated flag output.

deadcode example.com
!want "main"
want "Dead1"
!want "Dead2"

deadcode -generated example.com
!want "main"
want "Dead1"
want "Dead2"
deadcode "-f={{range .Funcs}}{{$.Name}}.{{.Name}}{{end}}" example.com
!want "main.main"
want "main.Dead1"
!want "main.Dead2"

deadcode "-f={{range .Funcs}}{{$.Name}}.{{.Name}}{{end}}" -generated example.com
!want "main.main"
want "main.Dead1"
want "main.Dead2"

-- go.mod --
module example.com
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/deadcode/testdata/lineflag.txtar
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Test of line-oriented output.

deadcode `-f={{range .Funcs}}{{printf "%s.%s\n" $.Path .Name}}{{end}}` -filter= example.com
deadcode `-f={{range .Funcs}}{{printf "%s: %s.%s\n" .Position $.Path .Name}}{{end}}` -filter= example.com

want "example.com.T.Goodbye"
want "main.go:13:10: example.com.T.Goodbye"
!want "example.com.T.Hello"
want "example.com.unreferenced"
want "main.go:15:6: example.com.unreferenced"

want "fmt.Scanf"
want "fmt.Printf"
Expand Down

0 comments on commit 7f1f4eb

Please sign in to comment.