go/gcexportdata: add -package flag to diagnostic tool

...to show information about indirectly mentioned packages.

Change-Id: Ib74b56493861bf41d9720760e76ace186efae2ea
Reviewed-on: https://go-review.googlesource.com/121195
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Alan Donovan 2018-06-27 10:16:43 -04:00
parent 56e9b8e653
commit 16699b2d33
1 changed files with 21 additions and 3 deletions

View File

@ -20,11 +20,13 @@ import (
"golang.org/x/tools/go/types/typeutil" "golang.org/x/tools/go/types/typeutil"
) )
var packageFlag = flag.String("package", "", "alternative package to print")
func main() { func main() {
log.SetPrefix("gcexportdata: ") log.SetPrefix("gcexportdata: ")
log.SetFlags(0) log.SetFlags(0)
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintln(os.Stderr, "usage: gcexportdata file.a") fmt.Fprintln(os.Stderr, "usage: gcexportdata [-package path] file.a")
} }
flag.Parse() flag.Parse()
if flag.NArg() != 1 { if flag.NArg() != 1 {
@ -44,11 +46,27 @@ func main() {
} }
// Decode the package. // Decode the package.
const primary = "<primary>"
imports := make(map[string]*types.Package) imports := make(map[string]*types.Package)
fset := token.NewFileSet() fset := token.NewFileSet()
pkg, err := gcexportdata.Read(r, fset, imports, "dummy") pkg, err := gcexportdata.Read(r, fset, imports, primary)
if err != nil { if err != nil {
log.Fatal("%s: %s", filename, err) log.Fatalf("%s: %s", filename, err)
}
// Optionally select an indirectly mentioned package.
if *packageFlag != "" {
pkg = imports[*packageFlag]
if pkg == nil {
fmt.Fprintf(os.Stderr, "export data file %s does not mention %s; has:\n",
filename, *packageFlag)
for p := range imports {
if p != primary {
fmt.Fprintf(os.Stderr, "\t%s\n", p)
}
}
os.Exit(1)
}
} }
// Print all package-level declarations, including non-exported ones. // Print all package-level declarations, including non-exported ones.