compiler: emit all used files in DebugInfo.Documents

This commit is contained in:
Evgenii Stratonikov 2020-08-10 13:10:35 +03:00
parent 057e1c6e3c
commit 40fa7c0f6e
5 changed files with 25 additions and 10 deletions

View file

@ -3,6 +3,7 @@ package compiler
import ( import (
"errors" "errors"
"go/ast" "go/ast"
"go/token"
"go/types" "go/types"
"strings" "strings"
@ -163,6 +164,16 @@ func (c *codegen) visitPkg(pkg *types.Package, seen map[string]bool) {
c.packages = append(c.packages, pkgPath) c.packages = append(c.packages, pkgPath)
} }
func (c *codegen) fillDocumentInfo() {
fset := c.buildInfo.program.Fset
fset.Iterate(func(f *token.File) bool {
filePath := f.Position(f.Pos(0)).Filename
c.docIndex[filePath] = len(c.documents)
c.documents = append(c.documents, filePath)
return true
})
}
func (c *codegen) analyzeFuncUsage() funcUsage { func (c *codegen) analyzeFuncUsage() funcUsage {
usage := funcUsage{} usage := funcUsage{}

View file

@ -77,6 +77,11 @@ type codegen struct {
// packages contains packages in the order they were loaded. // packages contains packages in the order they were loaded.
packages []string packages []string
// documents contains paths to all files used by the program.
documents []string
// docIndex maps file path to an index in documents array.
docIndex map[string]int
// Label table for recording jump destinations. // Label table for recording jump destinations.
l []int l []int
} }
@ -1541,6 +1546,7 @@ func (c *codegen) newLambda(u uint16, lit *ast.FuncLit) {
func (c *codegen) compile(info *buildInfo, pkg *loader.PackageInfo) error { func (c *codegen) compile(info *buildInfo, pkg *loader.PackageInfo) error {
c.mainPkg = pkg c.mainPkg = pkg
c.analyzePkgOrder() c.analyzePkgOrder()
c.fillDocumentInfo()
funUsage := c.analyzeFuncUsage() funUsage := c.analyzeFuncUsage()
// Bring all imported functions into scope. // Bring all imported functions into scope.
@ -1588,6 +1594,7 @@ func newCodegen(info *buildInfo, pkg *loader.PackageInfo) *codegen {
labels: map[labelWithType]uint16{}, labels: map[labelWithType]uint16{},
typeInfo: &pkg.Info, typeInfo: &pkg.Info,
constMap: map[string]types.TypeAndValue{}, constMap: map[string]types.TypeAndValue{},
docIndex: map[string]int{},
sequencePoints: make(map[string][]DebugSeqPoint), sequencePoints: make(map[string][]DebugSeqPoint),
} }

View file

@ -10,7 +10,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
@ -159,12 +158,6 @@ func CompileAndSave(src string, o *Options) ([]byte, error) {
return b, nil return b, nil
} }
p, err := filepath.Abs(src)
if err != nil {
return b, err
}
di.Documents = append(di.Documents, p)
if o.DebugInfo != "" { if o.DebugInfo != "" {
data, err := json.Marshal(di) data, err := json.Marshal(di)
if err != nil { if err != nil {

View file

@ -94,6 +94,7 @@ func (c *codegen) saveSequencePoint(n ast.Node) {
end := fset.Position(n.End()) end := fset.Position(n.End())
c.sequencePoints[c.scope.name] = append(c.sequencePoints[c.scope.name], DebugSeqPoint{ c.sequencePoints[c.scope.name] = append(c.sequencePoints[c.scope.name], DebugSeqPoint{
Opcode: c.prog.Len(), Opcode: c.prog.Len(),
Document: c.docIndex[start.Filename],
StartLine: start.Line, StartLine: start.Line,
StartCol: start.Offset, StartCol: start.Offset,
EndLine: end.Line, EndLine: end.Line,
@ -106,6 +107,7 @@ func (c *codegen) emitDebugInfo(contract []byte) *DebugInfo {
MainPkg: c.mainPkg.Pkg.Name(), MainPkg: c.mainPkg.Pkg.Name(),
Hash: hash.Hash160(contract), Hash: hash.Hash160(contract),
Events: []EventDebugInfo{}, Events: []EventDebugInfo{},
Documents: c.documents,
} }
if c.initEndOffset > 0 { if c.initEndOffset > 0 {
d.Methods = append(d.Methods, MethodDebugInfo{ d.Methods = append(d.Methods, MethodDebugInfo{

View file

@ -249,6 +249,8 @@ func TestSequencePoints(t *testing.T) {
d := c.emitDebugInfo(buf) d := c.emitDebugInfo(buf)
require.NotNil(t, d) require.NotNil(t, d)
require.Equal(t, d.Documents, []string{"foo.go"})
// Main func has 2 return on 4-th and 6-th lines. // Main func has 2 return on 4-th and 6-th lines.
ps := d.Methods[0].SeqPoints ps := d.Methods[0].SeqPoints
require.Equal(t, 2, len(ps)) require.Equal(t, 2, len(ps))