2022-11-05 18:25:29 -04:00
|
|
|
// Package source provides utilities for handling source-code.
|
2020-02-22 12:12:14 -05:00
|
|
|
package source // import "gotest.tools/v3/internal/source"
|
2018-02-28 10:11:02 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-09-22 09:38:19 -04:00
|
|
|
"errors"
|
2018-02-28 10:11:02 -05:00
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/format"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FormattedCallExprArg returns the argument from an ast.CallExpr at the
|
|
|
|
// index in the call stack. The argument is formatted using FormatNode.
|
|
|
|
func FormattedCallExprArg(stackIndex int, argPos int) (string, error) {
|
|
|
|
args, err := CallExprArgs(stackIndex + 1)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-11-14 06:28:16 -05:00
|
|
|
if argPos >= len(args) {
|
|
|
|
return "", errors.New("failed to find expression")
|
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
return FormatNode(args[argPos])
|
|
|
|
}
|
|
|
|
|
2018-11-14 06:28:16 -05:00
|
|
|
// CallExprArgs returns the ast.Expr slice for the args of an ast.CallExpr at
|
|
|
|
// the index in the call stack.
|
|
|
|
func CallExprArgs(stackIndex int) ([]ast.Expr, error) {
|
2022-09-22 09:38:19 -04:00
|
|
|
_, filename, line, ok := runtime.Caller(stackIndex + 1)
|
2018-11-14 06:28:16 -05:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("failed to get call stack")
|
|
|
|
}
|
2022-09-22 09:38:19 -04:00
|
|
|
debug("call stack position: %s:%d", filename, line)
|
2018-11-14 06:28:16 -05:00
|
|
|
|
2022-09-22 09:38:19 -04:00
|
|
|
fileset := token.NewFileSet()
|
|
|
|
astFile, err := parser.ParseFile(fileset, filename, nil, parser.AllErrors)
|
2018-11-14 06:28:16 -05:00
|
|
|
if err != nil {
|
2022-09-22 09:38:19 -04:00
|
|
|
return nil, fmt.Errorf("failed to parse source file %s: %w", filename, err)
|
2018-11-14 06:28:16 -05:00
|
|
|
}
|
|
|
|
|
2022-09-22 09:38:19 -04:00
|
|
|
expr, err := getCallExprArgs(fileset, astFile, line)
|
2018-02-28 10:11:02 -05:00
|
|
|
if err != nil {
|
2022-09-22 09:38:19 -04:00
|
|
|
return nil, fmt.Errorf("call from %s:%d: %w", filename, line, err)
|
2018-02-28 10:11:02 -05:00
|
|
|
}
|
2022-09-22 09:38:19 -04:00
|
|
|
return expr, nil
|
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
|
2022-09-22 09:38:19 -04:00
|
|
|
func getNodeAtLine(fileset *token.FileSet, astFile ast.Node, lineNum int) (ast.Node, error) {
|
2018-11-14 06:28:16 -05:00
|
|
|
if node := scanToLine(fileset, astFile, lineNum); node != nil {
|
|
|
|
return node, nil
|
2018-02-28 10:11:02 -05:00
|
|
|
}
|
2018-11-14 06:28:16 -05:00
|
|
|
if node := scanToDeferLine(fileset, astFile, lineNum); node != nil {
|
|
|
|
node, err := guessDefer(node)
|
|
|
|
if err != nil || node != nil {
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
}
|
2022-09-22 09:38:19 -04:00
|
|
|
return nil, nil
|
2018-02-28 10:11:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func scanToLine(fileset *token.FileSet, node ast.Node, lineNum int) ast.Node {
|
2018-11-14 06:28:16 -05:00
|
|
|
var matchedNode ast.Node
|
|
|
|
ast.Inspect(node, func(node ast.Node) bool {
|
|
|
|
switch {
|
|
|
|
case node == nil || matchedNode != nil:
|
|
|
|
return false
|
2022-09-22 09:38:19 -04:00
|
|
|
case fileset.Position(node.Pos()).Line == lineNum:
|
2018-11-14 06:28:16 -05:00
|
|
|
matchedNode = node
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return matchedNode
|
2018-02-28 10:11:02 -05:00
|
|
|
}
|
|
|
|
|
2022-09-22 09:38:19 -04:00
|
|
|
func getCallExprArgs(fileset *token.FileSet, astFile ast.Node, line int) ([]ast.Expr, error) {
|
|
|
|
node, err := getNodeAtLine(fileset, astFile, line)
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return nil, err
|
|
|
|
case node == nil:
|
|
|
|
return nil, fmt.Errorf("failed to find an expression")
|
2022-03-01 09:50:32 -05:00
|
|
|
}
|
2020-02-22 12:12:14 -05:00
|
|
|
|
2022-09-22 09:38:19 -04:00
|
|
|
debug("found node: %s", debugFormatNode{node})
|
2018-02-28 10:11:02 -05:00
|
|
|
|
|
|
|
visitor := &callExprVisitor{}
|
|
|
|
ast.Walk(visitor, node)
|
|
|
|
if visitor.expr == nil {
|
|
|
|
return nil, errors.New("failed to find call expression")
|
|
|
|
}
|
2018-11-14 06:28:16 -05:00
|
|
|
debug("callExpr: %s", debugFormatNode{visitor.expr})
|
2018-02-28 10:11:02 -05:00
|
|
|
return visitor.expr.Args, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type callExprVisitor struct {
|
|
|
|
expr *ast.CallExpr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *callExprVisitor) Visit(node ast.Node) ast.Visitor {
|
|
|
|
if v.expr != nil || node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-14 06:28:16 -05:00
|
|
|
debug("visit: %s", debugFormatNode{node})
|
2018-02-28 10:11:02 -05:00
|
|
|
|
2018-11-14 06:28:16 -05:00
|
|
|
switch typed := node.(type) {
|
|
|
|
case *ast.CallExpr:
|
|
|
|
v.expr = typed
|
|
|
|
return nil
|
|
|
|
case *ast.DeferStmt:
|
|
|
|
ast.Walk(v, typed.Call.Fun)
|
2018-02-28 10:11:02 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// FormatNode using go/format.Node and return the result as a string
|
|
|
|
func FormatNode(node ast.Node) (string, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := format.Node(buf, token.NewFileSet(), node)
|
|
|
|
return buf.String(), err
|
|
|
|
}
|
|
|
|
|
2018-11-14 06:28:16 -05:00
|
|
|
var debugEnabled = os.Getenv("GOTESTTOOLS_DEBUG") != ""
|
2018-02-28 10:11:02 -05:00
|
|
|
|
|
|
|
func debug(format string, args ...interface{}) {
|
|
|
|
if debugEnabled {
|
|
|
|
fmt.Fprintf(os.Stderr, "DEBUG: "+format+"\n", args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type debugFormatNode struct {
|
|
|
|
ast.Node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n debugFormatNode) String() string {
|
2022-09-22 09:38:19 -04:00
|
|
|
if n.Node == nil {
|
|
|
|
return "none"
|
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
out, err := FormatNode(n.Node)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Sprintf("failed to format %s: %s", n.Node, err)
|
|
|
|
}
|
2018-11-14 06:28:16 -05:00
|
|
|
return fmt.Sprintf("(%T) %s", n.Node, out)
|
2018-02-28 10:11:02 -05:00
|
|
|
}
|