From d20506048ff0749fdbcb9bba3e018ad3cac66d3e Mon Sep 17 00:00:00 2001 From: Grant Murphy <grantcmurphy@gmail.com> Date: Sun, 6 Nov 2016 11:59:24 -0800 Subject: [PATCH 1/3] Update to dump specific context information Added output printers for comments, types, defs, and uses maps. --- tools.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/tools.go b/tools.go index 785783d..fa312be 100644 --- a/tools.go +++ b/tools.go @@ -36,6 +36,10 @@ func newUtils() *utilities { utils := make(map[string]command) utils["ast"] = dumpAst utils["callobj"] = dumpCallObj + utils["uses"] = dumpUses + utils["types"] = dumpTypes + utils["defs"] = dumpDefs + utils["comments"] = dumpComments return &utilities{utils, make([]string, 0)} } @@ -143,9 +147,9 @@ func dumpCallObj(files ...string) { var obj types.Object switch node := n.(type) { case *ast.Ident: - obj = context.info.Uses[node] + obj = context.info.ObjectOf(node) //context.info.Uses[node] case *ast.SelectorExpr: - obj = context.info.Uses[node.Sel] + obj = context.info.ObjectOf(node.Sel) //context.info.Uses[node.Sel] default: obj = nil } @@ -156,3 +160,39 @@ func dumpCallObj(files ...string) { }) } } + +func dumpUses(files ...string) { + for _, file := range files { + context := createContext(file) + for ident, obj := range context.info.Uses { + fmt.Printf("IDENT: %v, OBJECT: %v\n", ident, obj) + } + } +} + +func dumpTypes(files ...string) { + for _, file := range files { + context := createContext(file) + for expr, tv := range context.info.Types { + fmt.Printf("EXPR: %v, TYPE: %v\n", expr, tv) + } + } +} + +func dumpDefs(files ...string) { + for _, file := range files { + context := createContext(file) + for ident, obj := range context.info.Defs { + fmt.Printf("IDENT: %v, OBJ: %v\n", ident, obj) + } + } +} + +func dumpComments(files ...string) { + for _, file := range files { + context := createContext(file) + for _, group := range context.comments.Comments() { + fmt.Println(group.Text()) + } + } +} From 2c9d8fc4616c8674c7697553ef00c4c781c91d41 Mon Sep 17 00:00:00 2001 From: Grant Murphy <grantcmurphy@gmail.com> Date: Sun, 6 Nov 2016 12:04:52 -0800 Subject: [PATCH 2/3] Skip files if they don't exist --- tools.go | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/tools.go b/tools.go index fa312be..0151328 100644 --- a/tools.go +++ b/tools.go @@ -70,16 +70,23 @@ func (u *utilities) run(args ...string) { } } +func shouldSkip(path string) bool { + st, e := os.Stat(path) + if e != nil { + fmt.Fprintf(os.Stderr, "Skipping: %s - %s\n", path, e) + return true + } + if st.IsDir() { + fmt.Fprintf(os.Stderr, "Skipping: %s - directory\n", path) + return true + } + return false +} + func dumpAst(files ...string) { for _, arg := range files { // Ensure file exists and not a directory - st, e := os.Stat(arg) - if e != nil { - fmt.Fprintf(os.Stderr, "Skipping: %s - %s\n", arg, e) - continue - } - if st.IsDir() { - fmt.Fprintf(os.Stderr, "Skipping: %s - directory\n", arg) + if shouldSkip(arg) { continue } @@ -142,6 +149,9 @@ func printObject(obj types.Object) { func dumpCallObj(files ...string) { for _, file := range files { + if shouldSkip(file) { + continue + } context := createContext(file) ast.Inspect(context.root, func(n ast.Node) bool { var obj types.Object @@ -163,6 +173,9 @@ func dumpCallObj(files ...string) { func dumpUses(files ...string) { for _, file := range files { + if shouldSkip(file) { + continue + } context := createContext(file) for ident, obj := range context.info.Uses { fmt.Printf("IDENT: %v, OBJECT: %v\n", ident, obj) @@ -172,6 +185,9 @@ func dumpUses(files ...string) { func dumpTypes(files ...string) { for _, file := range files { + if shouldSkip(file) { + continue + } context := createContext(file) for expr, tv := range context.info.Types { fmt.Printf("EXPR: %v, TYPE: %v\n", expr, tv) @@ -181,6 +197,9 @@ func dumpTypes(files ...string) { func dumpDefs(files ...string) { for _, file := range files { + if shouldSkip(file) { + continue + } context := createContext(file) for ident, obj := range context.info.Defs { fmt.Printf("IDENT: %v, OBJ: %v\n", ident, obj) @@ -190,6 +209,9 @@ func dumpDefs(files ...string) { func dumpComments(files ...string) { for _, file := range files { + if shouldSkip(file) { + continue + } context := createContext(file) for _, group := range context.comments.Comments() { fmt.Println(group.Text()) From b02c0fa2fca5512ac3a8174e9e14f0a079c632c3 Mon Sep 17 00:00:00 2001 From: Grant Murphy <grantcmurphy@gmail.com> Date: Sun, 6 Nov 2016 12:15:32 -0800 Subject: [PATCH 3/3] Add imports dumper --- tools.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tools.go b/tools.go index 0151328..9bf03f3 100644 --- a/tools.go +++ b/tools.go @@ -40,6 +40,7 @@ func newUtils() *utilities { utils["types"] = dumpTypes utils["defs"] = dumpDefs utils["comments"] = dumpComments + utils["imports"] = dumpImports return &utilities{utils, make([]string, 0)} } @@ -218,3 +219,18 @@ func dumpComments(files ...string) { } } } + +func dumpImports(files ...string) { + for _, file := range files { + if shouldSkip(file) { + continue + } + context := createContext(file) + for _, pkg := range context.pkg.Imports() { + fmt.Println(pkg.Path(), pkg.Name()) + for _, name := range pkg.Scope().Names() { + fmt.Println(" => ", name) + } + } + } +}