Alan Donovan | aac7fb6 | 2023-07-03 18:26:41 -0400 | [diff] [blame] | 1 | // Copyright 2023 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 | /* |
| 6 | The deadcode command reports unreachable functions in Go programs. |
| 7 | |
| 8 | Usage: deadcode [flags] package... |
| 9 | |
| 10 | The deadcode command loads a Go program from source then uses Rapid |
| 11 | Type Analysis (RTA) to build a call graph of all the functions |
| 12 | reachable from the program's main function. Any functions that are not |
| 13 | reachable are reported as dead code, grouped by package. |
| 14 | |
| 15 | Packages are expressed in the notation of 'go list' (or other |
| 16 | underlying build system if you are using an alternative |
| 17 | golang.org/x/go/packages driver). Only executable (main) packages are |
| 18 | considered starting points for the analysis. |
| 19 | |
| 20 | The -test flag causes it to analyze test executables too. Tests |
| 21 | sometimes make use of functions that would otherwise appear to be dead |
| 22 | code, and public API functions reported as dead with -test indicate |
| 23 | possible gaps in your test coverage. Bear in mind that an Example test |
| 24 | function without an "Output:" comment is merely documentation: |
| 25 | it is dead code, and does not contribute coverage. |
| 26 | |
| 27 | The -filter flag restricts results to packages that match the provided |
| 28 | regular expression; its default value is the module name of the first |
| 29 | package. Use -filter= to display all results. |
| 30 | |
| 31 | Example: show all dead code within the gopls module: |
| 32 | |
| 33 | $ deadcode -test golang.org/x/tools/gopls/... |
| 34 | |
| 35 | The analysis can soundly analyze dynamic calls though func values, |
| 36 | interface methods, and reflection. However, it does not currently |
| 37 | understand the aliasing created by //go:linkname directives, so it |
| 38 | will fail to recognize that calls to a linkname-annotated function |
| 39 | with no body in fact dispatch to the function named in the annotation. |
| 40 | This may result in the latter function being spuriously reported as dead. |
| 41 | |
| 42 | In any case, just because a function is reported as dead does not mean |
| 43 | it is unconditionally safe to delete it. For example, a dead function |
| 44 | may be referenced (by another dead function), and a dead method may be |
| 45 | required to satisfy an interface (that is never called). |
| 46 | Some judgement is required. |
| 47 | |
| 48 | The analysis is valid only for a single GOOS/GOARCH/-tags configuration, |
| 49 | so a function reported as dead may be live in a different configuration. |
| 50 | Consider running the tool once for each configuration of interest. |
| 51 | Use the -line flag to emit a line-oriented output that makes it |
| 52 | easier to compute the intersection of results across all runs. |
| 53 | |
| 54 | THIS TOOL IS EXPERIMENTAL and its interface may change. |
| 55 | At some point it may be published at cmd/deadcode. |
| 56 | In the meantime, please give us feedback at github.com/golang/go/issues. |
| 57 | */ |
| 58 | package main |