2016-07-20 11:02:01 +01:00
|
|
|
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
2016-11-06 19:17:10 +00:00
|
|
|
"go/importer"
|
2016-07-20 11:02:01 +01:00
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2016-11-06 19:17:10 +00:00
|
|
|
"go/types"
|
2016-07-22 19:05:05 +01:00
|
|
|
"os"
|
2016-07-20 11:02:01 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type command func(args ...string)
|
|
|
|
type utilities struct {
|
|
|
|
commands map[string]command
|
|
|
|
call []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom commands / utilities to run instead of default analyzer
|
|
|
|
func newUtils() *utilities {
|
|
|
|
utils := make(map[string]command)
|
2016-11-06 19:17:10 +00:00
|
|
|
utils["ast"] = dumpAst
|
|
|
|
utils["callobj"] = dumpCallObj
|
2016-11-06 19:59:24 +00:00
|
|
|
utils["uses"] = dumpUses
|
|
|
|
utils["types"] = dumpTypes
|
|
|
|
utils["defs"] = dumpDefs
|
|
|
|
utils["comments"] = dumpComments
|
2016-11-06 20:15:32 +00:00
|
|
|
utils["imports"] = dumpImports
|
2016-07-20 11:02:01 +01:00
|
|
|
return &utilities{utils, make([]string, 0)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *utilities) String() string {
|
|
|
|
i := 0
|
|
|
|
keys := make([]string, len(u.commands))
|
|
|
|
for k := range u.commands {
|
|
|
|
keys[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return strings.Join(keys, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *utilities) Set(opt string) error {
|
|
|
|
if _, ok := u.commands[opt]; !ok {
|
|
|
|
return fmt.Errorf("valid tools are: %s", u.String())
|
|
|
|
|
|
|
|
}
|
|
|
|
u.call = append(u.call, opt)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *utilities) run(args ...string) {
|
|
|
|
for _, util := range u.call {
|
|
|
|
if cmd, ok := u.commands[util]; ok {
|
|
|
|
cmd(args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-06 20:04:52 +00:00
|
|
|
func shouldSkip(path string) bool {
|
|
|
|
st, e := os.Stat(path)
|
|
|
|
if e != nil {
|
2016-12-02 18:20:23 +00:00
|
|
|
// #nosec
|
2016-11-06 20:04:52 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Skipping: %s - %s\n", path, e)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if st.IsDir() {
|
2016-12-02 18:20:23 +00:00
|
|
|
// #nosec
|
2016-11-06 20:04:52 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Skipping: %s - directory\n", path)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-07-20 11:02:01 +01:00
|
|
|
func dumpAst(files ...string) {
|
|
|
|
for _, arg := range files {
|
2016-07-22 19:05:05 +01:00
|
|
|
// Ensure file exists and not a directory
|
2016-11-06 20:04:52 +00:00
|
|
|
if shouldSkip(arg) {
|
2016-07-22 19:05:05 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-07-20 11:02:01 +01:00
|
|
|
// Create the AST by parsing src.
|
|
|
|
fset := token.NewFileSet() // positions are relative to fset
|
|
|
|
f, err := parser.ParseFile(fset, arg, nil, 0)
|
|
|
|
if err != nil {
|
2016-12-02 18:20:23 +00:00
|
|
|
// #nosec
|
2016-07-22 19:05:05 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "Unable to parse file %s\n", err)
|
|
|
|
continue
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
|
2016-12-02 18:20:23 +00:00
|
|
|
// Print the AST. #nosec
|
2016-07-20 11:02:01 +01:00
|
|
|
ast.Print(fset, f)
|
|
|
|
}
|
|
|
|
}
|
2016-11-06 19:17:10 +00:00
|
|
|
|
|
|
|
type context struct {
|
|
|
|
fileset *token.FileSet
|
|
|
|
comments ast.CommentMap
|
|
|
|
info *types.Info
|
|
|
|
pkg *types.Package
|
|
|
|
config *types.Config
|
|
|
|
root *ast.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func createContext(filename string) *context {
|
|
|
|
fileset := token.NewFileSet()
|
2016-12-02 18:20:23 +00:00
|
|
|
root, e := parser.ParseFile(fileset, filename, nil, parser.ParseComments)
|
|
|
|
if e != nil {
|
|
|
|
// #nosec
|
|
|
|
fmt.Fprintf(os.Stderr, "Unable to parse file: %s. Reason: %s\n", filename, e)
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-06 19:17:10 +00:00
|
|
|
comments := ast.NewCommentMap(fileset, root, root.Comments)
|
|
|
|
info := &types.Info{
|
|
|
|
Types: make(map[ast.Expr]types.TypeAndValue),
|
|
|
|
Defs: make(map[*ast.Ident]types.Object),
|
|
|
|
Uses: make(map[*ast.Ident]types.Object),
|
|
|
|
Selections: make(map[*ast.SelectorExpr]*types.Selection),
|
|
|
|
Scopes: make(map[ast.Node]*types.Scope),
|
|
|
|
Implicits: make(map[ast.Node]types.Object),
|
|
|
|
}
|
|
|
|
config := types.Config{Importer: importer.Default()}
|
2016-12-02 18:20:23 +00:00
|
|
|
pkg, e := config.Check("main.go", fileset, []*ast.File{root}, info)
|
|
|
|
if e != nil {
|
|
|
|
// #nosec
|
|
|
|
fmt.Fprintf(os.Stderr, "Type check failed for file: %s. Reason: %s\n", filename, e)
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-06 19:17:10 +00:00
|
|
|
return &context{fileset, comments, info, pkg, &config, root}
|
|
|
|
}
|
|
|
|
|
|
|
|
func printObject(obj types.Object) {
|
|
|
|
fmt.Println("OBJECT")
|
|
|
|
if obj == nil {
|
|
|
|
fmt.Println("object is nil")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf(" Package = %v\n", obj.Pkg())
|
|
|
|
if obj.Pkg() != nil {
|
|
|
|
fmt.Println(" Path = ", obj.Pkg().Path())
|
|
|
|
fmt.Println(" Name = ", obj.Pkg().Name())
|
|
|
|
fmt.Println(" String = ", obj.Pkg().String())
|
|
|
|
}
|
|
|
|
fmt.Printf(" Name = %v\n", obj.Name())
|
|
|
|
fmt.Printf(" Type = %v\n", obj.Type())
|
|
|
|
fmt.Printf(" Id = %v\n", obj.Id())
|
|
|
|
}
|
|
|
|
|
2016-12-02 18:20:23 +00:00
|
|
|
func checkContext(ctx *context, file string) bool {
|
|
|
|
// #nosec
|
|
|
|
if ctx == nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Failed to create context for file: ", file)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-11-06 19:17:10 +00:00
|
|
|
func dumpCallObj(files ...string) {
|
|
|
|
|
|
|
|
for _, file := range files {
|
2016-11-06 20:04:52 +00:00
|
|
|
if shouldSkip(file) {
|
|
|
|
continue
|
|
|
|
}
|
2016-11-06 19:17:10 +00:00
|
|
|
context := createContext(file)
|
2016-12-02 18:20:23 +00:00
|
|
|
if !checkContext(context, file) {
|
|
|
|
return
|
|
|
|
}
|
2016-11-06 19:17:10 +00:00
|
|
|
ast.Inspect(context.root, func(n ast.Node) bool {
|
|
|
|
var obj types.Object
|
|
|
|
switch node := n.(type) {
|
|
|
|
case *ast.Ident:
|
2016-11-06 19:59:24 +00:00
|
|
|
obj = context.info.ObjectOf(node) //context.info.Uses[node]
|
2016-11-06 19:17:10 +00:00
|
|
|
case *ast.SelectorExpr:
|
2016-11-06 19:59:24 +00:00
|
|
|
obj = context.info.ObjectOf(node.Sel) //context.info.Uses[node.Sel]
|
2016-11-06 19:17:10 +00:00
|
|
|
default:
|
|
|
|
obj = nil
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
printObject(obj)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-11-06 19:59:24 +00:00
|
|
|
|
|
|
|
func dumpUses(files ...string) {
|
|
|
|
for _, file := range files {
|
2016-11-06 20:04:52 +00:00
|
|
|
if shouldSkip(file) {
|
|
|
|
continue
|
|
|
|
}
|
2016-11-06 19:59:24 +00:00
|
|
|
context := createContext(file)
|
2016-12-02 18:20:23 +00:00
|
|
|
if !checkContext(context, file) {
|
|
|
|
return
|
|
|
|
}
|
2016-11-06 19:59:24 +00:00
|
|
|
for ident, obj := range context.info.Uses {
|
|
|
|
fmt.Printf("IDENT: %v, OBJECT: %v\n", ident, obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpTypes(files ...string) {
|
|
|
|
for _, file := range files {
|
2016-11-06 20:04:52 +00:00
|
|
|
if shouldSkip(file) {
|
|
|
|
continue
|
|
|
|
}
|
2016-11-06 19:59:24 +00:00
|
|
|
context := createContext(file)
|
2016-12-02 18:20:23 +00:00
|
|
|
if !checkContext(context, file) {
|
|
|
|
return
|
|
|
|
}
|
2016-11-06 19:59:24 +00:00
|
|
|
for expr, tv := range context.info.Types {
|
|
|
|
fmt.Printf("EXPR: %v, TYPE: %v\n", expr, tv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpDefs(files ...string) {
|
|
|
|
for _, file := range files {
|
2016-11-06 20:04:52 +00:00
|
|
|
if shouldSkip(file) {
|
|
|
|
continue
|
|
|
|
}
|
2016-11-06 19:59:24 +00:00
|
|
|
context := createContext(file)
|
2016-12-02 18:20:23 +00:00
|
|
|
if !checkContext(context, file) {
|
|
|
|
return
|
|
|
|
}
|
2016-11-06 19:59:24 +00:00
|
|
|
for ident, obj := range context.info.Defs {
|
|
|
|
fmt.Printf("IDENT: %v, OBJ: %v\n", ident, obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpComments(files ...string) {
|
|
|
|
for _, file := range files {
|
2016-11-06 20:04:52 +00:00
|
|
|
if shouldSkip(file) {
|
|
|
|
continue
|
|
|
|
}
|
2016-11-06 19:59:24 +00:00
|
|
|
context := createContext(file)
|
2016-12-02 18:20:23 +00:00
|
|
|
if !checkContext(context, file) {
|
|
|
|
return
|
|
|
|
}
|
2016-11-06 19:59:24 +00:00
|
|
|
for _, group := range context.comments.Comments() {
|
|
|
|
fmt.Println(group.Text())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-06 20:15:32 +00:00
|
|
|
|
|
|
|
func dumpImports(files ...string) {
|
|
|
|
for _, file := range files {
|
|
|
|
if shouldSkip(file) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
context := createContext(file)
|
2016-12-02 18:20:23 +00:00
|
|
|
if !checkContext(context, file) {
|
|
|
|
return
|
|
|
|
}
|
2016-11-06 20:15:32 +00:00
|
|
|
for _, pkg := range context.pkg.Imports() {
|
|
|
|
fmt.Println(pkg.Path(), pkg.Name())
|
|
|
|
for _, name := range pkg.Scope().Names() {
|
|
|
|
fmt.Println(" => ", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|