Merge pull request #73 from GoASTScanner/tools

Add some more useful debugging tools
This commit is contained in:
Grant Murphy 2016-11-06 17:35:08 -08:00 committed by GitHub
commit 238d1e0692

View file

@ -36,6 +36,11 @@ func newUtils() *utilities {
utils := make(map[string]command) utils := make(map[string]command)
utils["ast"] = dumpAst utils["ast"] = dumpAst
utils["callobj"] = dumpCallObj utils["callobj"] = dumpCallObj
utils["uses"] = dumpUses
utils["types"] = dumpTypes
utils["defs"] = dumpDefs
utils["comments"] = dumpComments
utils["imports"] = dumpImports
return &utilities{utils, make([]string, 0)} return &utilities{utils, make([]string, 0)}
} }
@ -66,16 +71,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) { func dumpAst(files ...string) {
for _, arg := range files { for _, arg := range files {
// Ensure file exists and not a directory // Ensure file exists and not a directory
st, e := os.Stat(arg) if shouldSkip(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)
continue continue
} }
@ -138,14 +150,17 @@ func printObject(obj types.Object) {
func dumpCallObj(files ...string) { func dumpCallObj(files ...string) {
for _, file := range files { for _, file := range files {
if shouldSkip(file) {
continue
}
context := createContext(file) context := createContext(file)
ast.Inspect(context.root, func(n ast.Node) bool { ast.Inspect(context.root, func(n ast.Node) bool {
var obj types.Object var obj types.Object
switch node := n.(type) { switch node := n.(type) {
case *ast.Ident: case *ast.Ident:
obj = context.info.Uses[node] obj = context.info.ObjectOf(node) //context.info.Uses[node]
case *ast.SelectorExpr: case *ast.SelectorExpr:
obj = context.info.Uses[node.Sel] obj = context.info.ObjectOf(node.Sel) //context.info.Uses[node.Sel]
default: default:
obj = nil obj = nil
} }
@ -156,3 +171,66 @@ 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)
}
}
}
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)
}
}
}
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)
}
}
}
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())
}
}
}
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)
}
}
}
}