Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

structlayout: usage hinting #1583

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions cmd/structlayout/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// structlayout displays the layout (field sizes and padding) of structs.
// Structlayout prints the memory layout of struct types. Structs may contain a
// combination of fields and padding for memory alignment. Go follows the field
// order as given in the respective code.
package main

import (
Expand All @@ -18,63 +20,62 @@ import (
)

var (
fJSON bool
fVersion bool
jsonFlag = flag.Bool("json", false, "Format data as JSON")
versionFlag = flag.Bool("version", false, "Print version and exit")
)

func init() {
flag.BoolVar(&fJSON, "json", false, "Format data as JSON")
flag.BoolVar(&fVersion, "version", false, "Print version and exit")
}

func main() {
log.SetFlags(0)
flag.Parse()

if fVersion {
if *versionFlag {
version.Print(version.Version, version.MachineVersion)
os.Exit(0)
}

if len(flag.Args()) != 2 {
flag.Usage()
fmt.Fprintln(flag.CommandLine.Output(), "Wrong number of arguments. Need a package and a type.")
fmt.Fprintln(flag.CommandLine.Output(), "OPTIONS:")
flag.PrintDefaults()
os.Exit(1)
}
matchPkg := flag.Arg(0)
matchType := flag.Arg(1)

// find matching packages
cfg := &packages.Config{
Mode: packages.NeedImports | packages.NeedExportFile | packages.NeedTypes | packages.NeedSyntax,
Tests: true,
}
pkgs, err := packages.Load(cfg, flag.Args()[0])
pkgs, err := packages.Load(cfg, matchPkg)
if err != nil {
log.Fatal(err)
}

// find matching types
for _, pkg := range pkgs {
typName := flag.Args()[1]

var typ types.Type
obj := pkg.Types.Scope().Lookup(typName)
obj := pkg.Types.Scope().Lookup(matchType)
if obj == nil {
continue
}
typ = obj.Type()

st, ok := typ.Underlying().(*types.Struct)
if !ok {
log.Fatal("identifier is not a struct type")
log.Fatalf("type %s is not a struct", typ)
}

fields := sizes(st, types.Unalias(typ).(*types.Named).Obj().Name(), 0, nil)
if fJSON {
if *jsonFlag {
emitJSON(fields)
} else {
emitText(fields)
}
return
}

log.Fatal("couldn't find type")
log.Fatalf("type %s.%s not found", matchPkg, matchType)
}

func emitJSON(fields []st.Field) {
Expand All @@ -89,6 +90,7 @@ func emitText(fields []st.Field) {
fmt.Println(field)
}
}

func sizes(typ *types.Struct, prefix string, base int64, out []st.Field) []st.Field {
s := gcsizes.ForArch(build.Default.GOARCH)
n := typ.NumFields()
Expand Down