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.
|
|
|
|
|
2017-04-26 16:08:46 +01:00
|
|
|
package gas
|
2016-07-20 11:02:01 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2016-11-04 18:20:28 +00:00
|
|
|
"go/types"
|
2016-07-20 11:02:01 +01:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2016-11-07 17:13:20 +00:00
|
|
|
// MatchCallByPackage ensures that the specified package is imported,
|
|
|
|
// adjusts the name for any aliases and ignores cases that are
|
|
|
|
// initialization only imports.
|
2016-11-04 18:20:28 +00:00
|
|
|
//
|
|
|
|
// Usage:
|
2016-11-07 17:13:20 +00:00
|
|
|
// node, matched := MatchCallByPackage(n, ctx, "math/rand", "Read")
|
2016-11-04 18:20:28 +00:00
|
|
|
//
|
2016-11-07 17:13:20 +00:00
|
|
|
func MatchCallByPackage(n ast.Node, c *Context, pkg string, names ...string) (*ast.CallExpr, bool) {
|
|
|
|
|
2016-11-18 22:09:10 +00:00
|
|
|
importedName, found := GetImportedName(pkg, c)
|
|
|
|
if !found {
|
2016-11-07 17:13:20 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2016-11-18 17:57:34 +00:00
|
|
|
if callExpr, ok := n.(*ast.CallExpr); ok {
|
|
|
|
packageName, callName, err := GetCallInfo(callExpr, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2016-11-18 22:09:10 +00:00
|
|
|
if packageName == importedName {
|
2016-11-18 17:57:34 +00:00
|
|
|
for _, name := range names {
|
|
|
|
if callName == name {
|
|
|
|
return callExpr, true
|
2016-11-07 17:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-04 18:20:28 +00:00
|
|
|
}
|
2016-11-07 17:13:20 +00:00
|
|
|
return nil, false
|
2016-11-04 18:20:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 04:18:31 +00:00
|
|
|
// MatchCallByType ensures that the node is a call expression to a
|
|
|
|
// specific object type.
|
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
// node, matched := MatchCallByType(n, ctx, "bytes.Buffer", "WriteTo", "Write")
|
|
|
|
//
|
|
|
|
func MatchCallByType(n ast.Node, ctx *Context, requiredType string, calls ...string) (*ast.CallExpr, bool) {
|
2016-11-18 17:57:34 +00:00
|
|
|
if callExpr, ok := n.(*ast.CallExpr); ok {
|
|
|
|
typeName, callName, err := GetCallInfo(callExpr, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if typeName == requiredType {
|
|
|
|
for _, call := range calls {
|
|
|
|
if call == callName {
|
|
|
|
return callExpr, true
|
2016-11-18 04:18:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2017-07-19 22:17:00 +01:00
|
|
|
// MatchCompLit will match an ast.CompositeLit based on the supplied type
|
|
|
|
func MatchCompLit(n ast.Node, ctx *Context, required string) *ast.CompositeLit {
|
|
|
|
if complit, ok := n.(*ast.CompositeLit); ok {
|
|
|
|
typeOf := ctx.Info.TypeOf(complit)
|
|
|
|
if typeOf.String() == required {
|
|
|
|
return complit
|
|
|
|
}
|
2016-07-20 11:02:01 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-12 14:17:28 +01:00
|
|
|
// GetInt will read and return an integer value from an ast.BasicLit
|
2016-07-20 11:02:01 +01:00
|
|
|
func GetInt(n ast.Node) (int64, error) {
|
|
|
|
if node, ok := n.(*ast.BasicLit); ok && node.Kind == token.INT {
|
|
|
|
return strconv.ParseInt(node.Value, 0, 64)
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("Unexpected AST node type: %T", n)
|
|
|
|
}
|
|
|
|
|
2017-07-19 22:17:00 +01:00
|
|
|
// GetFloat will read and return a float value from an ast.BasicLit
|
2016-07-20 11:02:01 +01:00
|
|
|
func GetFloat(n ast.Node) (float64, error) {
|
|
|
|
if node, ok := n.(*ast.BasicLit); ok && node.Kind == token.FLOAT {
|
|
|
|
return strconv.ParseFloat(node.Value, 64)
|
|
|
|
}
|
|
|
|
return 0.0, fmt.Errorf("Unexpected AST node type: %T", n)
|
|
|
|
}
|
|
|
|
|
2017-07-19 22:17:00 +01:00
|
|
|
// GetChar will read and return a char value from an ast.BasicLit
|
2016-07-20 11:02:01 +01:00
|
|
|
func GetChar(n ast.Node) (byte, error) {
|
|
|
|
if node, ok := n.(*ast.BasicLit); ok && node.Kind == token.CHAR {
|
|
|
|
return node.Value[0], nil
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("Unexpected AST node type: %T", n)
|
|
|
|
}
|
|
|
|
|
2017-07-19 22:17:00 +01:00
|
|
|
// GetString will read and return a string value from an ast.BasicLit
|
2016-07-20 11:02:01 +01:00
|
|
|
func GetString(n ast.Node) (string, error) {
|
|
|
|
if node, ok := n.(*ast.BasicLit); ok && node.Kind == token.STRING {
|
|
|
|
return strconv.Unquote(node.Value)
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("Unexpected AST node type: %T", n)
|
|
|
|
}
|
2016-11-04 21:39:22 +00:00
|
|
|
|
|
|
|
// GetCallObject returns the object and call expression and associated
|
|
|
|
// object for a given AST node. nil, nil will be returned if the
|
|
|
|
// object cannot be resolved.
|
|
|
|
func GetCallObject(n ast.Node, ctx *Context) (*ast.CallExpr, types.Object) {
|
|
|
|
switch node := n.(type) {
|
|
|
|
case *ast.CallExpr:
|
|
|
|
switch fn := node.Fun.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
return node, ctx.Info.Uses[fn]
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
return node, ctx.Info.Uses[fn.Sel]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-11-18 17:57:34 +00:00
|
|
|
|
|
|
|
// GetCallInfo returns the package or type and name associated with a
|
|
|
|
// call expression.
|
|
|
|
func GetCallInfo(n ast.Node, ctx *Context) (string, string, error) {
|
|
|
|
switch node := n.(type) {
|
|
|
|
case *ast.CallExpr:
|
|
|
|
switch fn := node.Fun.(type) {
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
switch expr := fn.X.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
if expr.Obj != nil && expr.Obj.Kind == ast.Var {
|
|
|
|
t := ctx.Info.TypeOf(expr)
|
|
|
|
if t != nil {
|
|
|
|
return t.String(), fn.Sel.Name, nil
|
|
|
|
}
|
2017-07-19 22:17:00 +01:00
|
|
|
return "undefined", fn.Sel.Name, fmt.Errorf("missing type info")
|
2016-11-18 17:57:34 +00:00
|
|
|
}
|
2017-07-19 22:17:00 +01:00
|
|
|
return expr.Name, fn.Sel.Name, nil
|
2016-11-18 17:57:34 +00:00
|
|
|
}
|
2016-11-18 22:09:10 +00:00
|
|
|
case *ast.Ident:
|
|
|
|
return ctx.Pkg.Name(), fn.Name, nil
|
2016-11-18 17:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", "", fmt.Errorf("unable to determine call info")
|
|
|
|
}
|
2016-11-18 22:09:10 +00:00
|
|
|
|
|
|
|
// GetImportedName returns the name used for the package within the
|
|
|
|
// code. It will resolve aliases and ignores initalization only imports.
|
|
|
|
func GetImportedName(path string, ctx *Context) (string, bool) {
|
|
|
|
importName, imported := ctx.Imports.Imported[path]
|
|
|
|
if !imported {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, initonly := ctx.Imports.InitOnly[path]; initonly {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
if alias, ok := ctx.Imports.Aliased[path]; ok {
|
|
|
|
importName = alias
|
|
|
|
}
|
|
|
|
return importName, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetImportPath resolves the full import path of an identifer based on
|
|
|
|
// the imports in the current context.
|
|
|
|
func GetImportPath(name string, ctx *Context) (string, bool) {
|
2017-07-19 22:17:00 +01:00
|
|
|
for path := range ctx.Imports.Imported {
|
2016-11-18 22:09:10 +00:00
|
|
|
if imported, ok := GetImportedName(path, ctx); ok && imported == name {
|
|
|
|
return path, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
2016-12-02 18:20:23 +00:00
|
|
|
|
|
|
|
// GetLocation returns the filename and line number of an ast.Node
|
|
|
|
func GetLocation(n ast.Node, ctx *Context) (string, int) {
|
|
|
|
fobj := ctx.FileSet.File(n.Pos())
|
|
|
|
return fobj.Name(), fobj.Line(n.Pos())
|
|
|
|
}
|